Defining JPA @entités
Defining JPA @entités
1. Introduction
In this tutorial, we’ll learn about the basics of entities, along with various
annotations that define and customize an entity in JPA.
2. Entity
Entities in JPA are nothing but POJOs representing data that can be
persisted in the database. An entity represents a table stored in a database.
Every instance of an entity represents a row in the table.
}Copy
To do this, we should define an entity so that JPA is aware of it.
So let’s define it by making use of the @Entity annotation. We must specify
this annotation at the class level. We must also ensure that the entity has
a no-arg constructor and a primary key:
@Entity
public class Student {
}Copy
The entity name defaults to the name of the class. We can change its name
using the name element:
@Entity(name="student")
public class Student {
}Copy
We can also mention the schema using the schema element:
@Entity
@Table(name="STUDENT", schema="SCHOOL")
public class Student {
}Copy
Schema name helps to distinguish one set of tables from another.
If we don’t use the @Table annotation, the name of the table will be the
name of the entity.
@Transient
private Integer age;
@Transient
private Integer age;
@Temporal(TemporalType.DATE)
private Date birthDate;
@Transient
private Integer age;
@Temporal(TemporalType.DATE)
private Date birthDate;
@Enumerated(EnumType.STRING)
private Gender gender;
3. Conclusion
In this article, we learned what JPA entities are and how to create them. We
also learned about the different annotations we can use to customize the
entity further.