top of page

Mapping Entity Class Names to SQL Table Names with JPA

Keral Patel

Introduction:

  • Creating JPA entities that follow best practices for efficient mapping.

  • Creating DTOs from entities and Map Struct mappers using convenient visual tools.

  • Generating differential SQL to update your schema in accordance with your changes in entities.

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.

Using @Table:


The easiest way to set a custom SQL table name is to annotate the entity with @javax.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";

}


Conclusion:


@Table annotation allows you to specify the details of the table that will be used to persist the entity in the database.


 
 
 

Recent Posts

See All

Comentarios


bottom of page