Define An Entity Class: JPA For Beginners
Define An Entity Class: JPA For Beginners
When you start learning and using Hibernate and JPA, the number of
annotations might be overwhelming. But as long as you rely on the
defaults, you can implement your persistence layer using only a small
subset of them.
And if you want to dive deeper into JPA and make sure you have a
solid understanding of all the basic concepts, I recommend enrolling
in my JPA for Beginners online course.
@Entity
The JPA specification requires the @Entity annotation. It identifies a
class as an entity class.
@Entity
public class Author { ... }
Attributes
www.thoughts-on-java.org
JPA & Hibernate: Key Annotations
@Table
By default, each entity class maps a database table with the same
name in the default schema of your database. You can customize this
mapping using the name, schema, and catalog attributes of the @Table
annotation.
@Entity
@Table(name = "AUTHORS", schema = "STORE")
public class Author {
Attributes
www.thoughts-on-java.org
JPA & Hibernate: Key Annotations
Basic Column Mappings
By default, all JPA implementations map each entity attribute to a
database column with the same name and a compatible type. The
following annotations enable you to perform basic customizations of
these mappings. You can, for example, change the name of the
column, adapt the type mapping, identify primary key attributes, and
generate unique values for them.
@Column
Let’s start with the @Column annotation. It is an optional annotation
that enables you to customize the mapping between the entity
attribute and the database column.
@Entity
public class Book {
...
}
Attributes
www.thoughts-on-java.org
JPA & Hibernate: Key Annotations
Specifies the table that contains the mapped
column.
@Id
JPA and Hibernate require you to specify at least one primary key
attribute for each entity. You can do that by annotating an attribute
with the @Id annotation.
@Entity
public class Author {
@Id
private Long id;
...
}
www.thoughts-on-java.org
JPA & Hibernate: Key Annotations
@GeneratedValue
When we’re talking about primary keys, we also need to talk about
sequences and auto-incremented database columns. These are the 2
most common database features to generate unique primary key
values.
@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
...
}
Attributes
GenerationType.SEQUENCE
GenerationType.IDENTITY
www.thoughts-on-java.org
JPA & Hibernate: Key Annotations
@Enumerated
The @Enumerated annotation enables you to define how anenum
attribute gets persisted in the database. By default, all JPA
implementations map the ordinal value of the enum to a numeric
database column.
@Entity
public class Author {
@Enumerated(EnumType.STRING)
private AuthorStatus status;
...
}
Attributes
EnumType.STRING
EnumType.ORDINAL
@Temporal
If you’re still using java.util.Date or java.util.Calendar as your
attribute types, you need to annotate the attribute with @Temporal.
Using this annotation, you can define if the attribute shall be mapped
as an SQL DATE, TIME, or TIMESTAMP.
www.thoughts-on-java.org
JPA & Hibernate: Key Annotations
@Entity
public class Author {
@Temporal(TemporalType.DATE)
private Date dateOfBirth;
...
}
Attributes
TemporalType.DATE
TemporalType.TIME
TemporalType.TIMESTAMP
@Lob
Using JPA’s @Lobannotation, you can map a BLOB (binary large
object) to a byte[] and a CLOB (character large object) to a String.
Your persistence provider then fetches the whole BLOB or CLOB
when it initializes the entity attribute.
@Entity
public class Book {
@Lob
private byte[] cover;
...
}
www.thoughts-on-java.org
JPA & Hibernate: Key Annotations
In addition to that, Hibernate also supports mappings to java.sql.Blob
and java.sql.Clob. These are not as easy to use a byte[] or a String, but
they can provide better performance. I explained that mapping in
great detail in Mapping BLOBs and CLOBs with Hibernate and JPA.
Association Mappings
You can also map associations between your entities. In the table
model, these are modeled as foreign key columns. These associations
are mapped as attributes of the type of the associated entity or
a Collection of associated entities, in your domain model.
In both cases, you need to describe the association mapping. You can
do that using a @ManyToMany,@ManyToOne, @OneToMany, or
@OneToOne annotation.
@ManyToMany
Many-to-many associations are very common in relational table
models. In your domain model, you can map this association in a uni-
or bidirectional way using attributes of type List,Set or Map, and a
@ManyToMany annotations.
@Entity
@Table(name = "BOOKS")
public class Book {
@ManyToMany
private Set<Author> authors;
...
}
Attributes
www.thoughts-on-java.org
JPA & Hibernate: Key Annotations
cascade The operations that shall be cascaded to the associated
entities.
@ManyToMany(mappedBy = "authors")
private Set<Book> books;
...
}
@ManyToOne
Let’s take a closer look at the @ManyToOne annotation. It defines the
owning side of a bidirectional many-to-one/one-to-many
www.thoughts-on-java.org
JPA & Hibernate: Key Annotations
association. You do that on the entity that maps the database table
that contains the foreign key column.
@Entity
public class Book {
@ManyToOne(fetch = FetchType.LAZY)
private Publisher publisher;
...
}
Attributes
@OneToMany
Similar to the referencing side of a bidirectional many-to-many
association, you can reference the name of the attribute that owns
the association in the mappedBy attribute. That tells your persistence
provider that this is the referencing side of a bidirectional
association, and it reuses the association mapping defined by the
owning side.
@Entity
public class Publisher {
...
}
www.thoughts-on-java.org
JPA & Hibernate: Key Annotations
Attributes
@OneToOne
One-to-one associations are only rarely used in relational table
models. You can map them using a@OneToOne annotation.
@Entity
public class Manuscript {
@OneToOne(fetch = FetchType.LAZY)
private Book book;
...
}
Attributes
www.thoughts-on-java.org
JPA & Hibernate: Key Annotations
optional Indicates if this association is mandatory
@Entity
public class Book {
@OneToOne(mappedBy = "book")
private Manuscript manuscript;
...
}
www.thoughts-on-java.org