Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is @Entity in Spring JPA?

Specifically, I am referring to javax.persistence.Entity.

Based on the documentation it shows when I hover the mouse over, in VS Code it states that:

Specifies that the class is an entity. This annotation is applied to the entity class.

What does it mean for Spring JPA that a class is an Entity?

like image 621
Alexandros Kourtis Avatar asked Aug 14 '20 14:08

Alexandros Kourtis


2 Answers

a class of type Entity indicates a class that, at an abstract level, is correlated with a table in the database. Each object instantiated by this class indicates a tuple of the table itself, containing the information of the latter. I recommend that you find out about what object relational mapping is. I also recommend this page that talks about (Spring docs) of ORM on Spring

like image 51
Otid0 Avatar answered Oct 31 '22 06:10

Otid0


I did some searching around to find an answer. I decided to post it anyway, I looked up docs.oracle.com before posting my question.

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.

An entity class must follow these requirements.

  • The class must be annotated with the javax.persistence.Entity annotation.
  • The class must have a public or protected, no-argument constructor. The class may have other constructors.
  • The class must not be declared final. No methods or persistent instance variables must be declared final.
  • If an entity instance is passed by value as a detached object, such as through a session bean’s remote business interface, the class must implement the Serializable interface.
  • Entities may extend both entity and non-entity classes, and non-entity classes may extend entity classes.
  • Persistent instance variables must be declared private, protected, or package-private and can be accessed directly only by the entity class’s methods. Clients must access the entity’s state through accessor or business methods.

Another interesting resource is this youtube video

TL;DR: @Entity annotation defines that a class can be mapped to a table.

like image 28
Alexandros Kourtis Avatar answered Oct 31 '22 05:10

Alexandros Kourtis