Hibernate and JPA column mapping with annotations

JPA column mapping explained

By default, properties defined in an @Entity annotated POJO (plain old Java object) map to a corresponding column in a database table, according to the following criteria:

  • By default, the table name is the name of the POJO.
  • By default, the column name is the name of the property.
  • Hibernate and JPA match the column and property’s data types.

However, if you want to customize the way a field maps to a database column, you can decorate a property with the @Column annotation.

JPA column mapping example

Take this JavaBean for example:

✂️ ✂️ ✂️
import javax.persistence.Column;
public class Player {

  private Long id;
  private String password;

  @Column (name="login_name")
  private String loginName;

✂️ ✂️ ✂️

The underlying database table that supports this JavaBean will possess the following characteristics:

  • Be named player.
  • Have a column named id of type integer.
  • Have a column named password of type varchar.
  • Have a column named login_name of type varchar.

Notice how the name property of the column annotation overrides the name of the underlying table in the database.

Hibernate JPA Column Annotation

The JPA @Column annotation can alter property to column mappings.

JPA @Column annotation properties

Along with the name property, the JPA @Column annotation can further customize the way a field maps to the database, through the use of the following properties:

  • updatable :: boolean – whether the column can be used with SQL UPDATE statements.
  • unique :: boolean – whether the column represents a unique database key.
  • table :: String – the name of the database table that hosts the JPA mapped column.
  • precision :: int – decimal precision for the column.
  • nullable :: boolean – whether the column can contain null values.
  • length :: int – how many characters the database column must support.
  • insertable :: boolean – whether the column should allow SQL INSERT statements.
  • columnDefinition :: String – the name of an SQL fragment that contains DDL for the column.

Advanced Hibernate and JPA column mapping

Here is a slightly more advanced JPA entity that uses a variety of annotations to describe how fields should map to database columns:

✂️ ✂️ ✂️
@Table(name="player", schema="hibernate_examples")
public class Player {

private Long id;

@Column(name="handle", unique=true)
private String name;
private String emailAddress;
private Boolean verified;

private String password;

private String encryptedPassword;
✂️ ✂️ ✂️

When you apply the full suite of annotations to this class, here’s what happens:

  • The entity maps to a table named player in the hibernate_examples database.
  • The id field is the primary key, which is uniquely generated by the database.
  • The name field is unique in the database and mapped to a column named handle.
  • The password field cannot be null.
  • The encryptedPassword field is not mapped to a database column.

Transient JPA annotation

Notice the JPA column mapping example above uses a @Transient annotation on the encryptedPassword field.

The @Transient annotation tells the JPA provider not to persist that field to the database. When this application runs, there is no column created for the encryptedPassword field, and data from the encryptedPassword field is left out of any SQL inserts or updates.

Hibernate and JPA greatly simplifies data persistence tasks for the developer. The @Column and @Transient JPA annotations are two examples of how JPA makes that happen.

App Architecture
Software Quality
Cloud Computing