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.