Partner – DBSchema – NPI (tag = SQL)
announcement - icon

DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.

The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.

And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.

>> Take a look at DBSchema

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

In this short tutorial, we’ll learn how to set SQL table names using JPA.

We’ll cover how JPA generates the default names and how to provide custom ones.

2. Default Table Names

The JPA default table name generation is specific to its implementation.

For instance, in Hibernate the default table name is the name of the class with the first letter capitalized. It’s determined through the ImplicitNamingStrategy contract.

But we can change this behaviour by implementing a PhysicalNamingStrategy interface.

3. Using @Table

The easiest way to set a custom SQL table name is to annotate the entity with @jakarta.persistence.Table and define its name parameter:

@Entity
@Table(name = "ARTICLES")
public class Article {
    // ...
}

We can also store the table name in a static final variable:

@Entity
@Table(name = Article.TABLE_NAME)
public class Article {
    public static final String TABLE_NAME= "ARTICLES";
    // ...
}

4. Overwriting the Table Name in JPQL Queries

By default in JPQL queries, we use the entity class name:

select * from Article

But we can change it by defining the name parameter in the @jakarta.persistence.Entity annotation:

@Entity(name = "MyArticle")

Then we’d change our JPQL query to:

select * from MyArticle

5. Conclusion

In this article, we’ve learned how JPA generates default table names and how to set SQL table names using JPA.

As always all source code is available over on GitHub.

Course – LSD (cat=Persistence)

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

>> CHECK OUT THE COURSE
Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – Persistence (eBook) (cat=Persistence)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.