I am studying JPA in Spring application and I have some doubts related to the @Entity annotation.
So I have a model class like this:
@Entity @Table(name= “T_CUSTOMER”) public class Customer { @Id @Column(name=“cust_id”) private Long id; @Column(name=“first_name”) private String firstName; @Transient private User currentUser; ........................... ........................... ........................... }
Ok, I know that the @Entity annotation is on the class level and it means that the fields of the object that are instances of this class are to be mapped to the field of the T_CUSTOMER database table.
But why in JPA it is mandatory to use @Entity annotation and I cannot only use the @Table annotation to map a model object to a specific database table? It have some other meaning\behavior that actually I am missing?
What am I missing? What is the exact meaning of the @Entity annotation?
The @Entity annotation specifies that the class is an entity and is mapped to a database table. The @Table annotation specifies the name of the database table to be used for mapping.
The JPA specification requires the @Entity annotation. It identifies a class as an entity class. You can use the name attribute of the @Entity annotation to define the name of the entity. It has to be unique for the persistence unit, and you use it to reference the entity in your JPQL queries.
An entity is a lightweight persistence domain object. Typically, an entity represents a table in a relational database, and each entity instance corresponds to a row in that table. The primary programming artifact of an entity is the entity class, although entities can use helper classes.
@Entity annotation marks this class as an entity. @Table annotation specifies the table name where data of this entity is to be persisted. If you don't use @Table annotation, hibernate will use the class name as the table name by default.
@Entity
annotation defines that a class can be mapped to a table. And that is it, it is just a marker, like for example Serializable
interface.
And why @Entity
annotation is mandatory? ... well, it is the way how JPA is designed. When you create a new entity you have to do at least two things
annotated it with @Entity
create an id field and annotate it with @Id
Anything else is optional, for example table name is derived from entity class name (and therefore @Table
annotation can be optional), table's columns are derived from entities variables (and therefore @Column
annotation can be optional), and so on ...
JPA is trying to provide a fast and easy start to developers who want to learn/use this API, and giving developers option to configure as few things as possible to make something functional is one of the ways how this API wants to achieve this "easy to use/learn" goal. Hence the @Entity
annotation (together with @Id
annotation) is the minimum you have to do in order to create an entity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With