在Spring JPA中,@Entity是什么?

huangapple go评论83阅读模式
英文:

What is @Entity in Spring JPA?

问题

具体来说,我指的是javax.persistence.Entity。

根据文档,在我将鼠标悬停在上面时,它在VS Code中显示:

> 指定该类是一个实体。此注解应用于实体类。

对于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?

答案1

得分: 18

一个类型为Entity的类表示一个在抽象层面上与数据库中的表相关联的类。该类实例化的每个对象表示表本身的一个元组,包含后者的信息。我建议您了解什么是对象关系映射。我还推荐阅读此页面,其中讨论了Spring框架上的ORM(对象关系映射)。

英文:

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

答案2

得分: 3

我搜索了一些内容以找到答案。我还是决定发帖,
在发布我的问题之前,我查阅了
docs.oracle.com

> 实体是轻量级持久化领域对象。通常,实体表示关系数据库中的一个表,每个实体实例对应该表中的一行。实体的主要编程构件是实体类,尽管实体可以使用帮助类。

实体类必须满足以下要求。

> - 该类必须使用javax.persistence.Entity注解进行注解。
> - 该类必须具有公共或受保护的无参数构造函数。该类可以具有其他构造函数。
> - 该类不能被声明为final。不能声明任何方法或持久化实例变量为final。
> - 如果实体实例按值传递作为分离对象,例如通过会话Bean的远程业务接口传递,该类必须实现Serializable接口。
> - 实体可以扩展实体和非实体类,非实体类可以扩展实体类。
> - 持久化实例变量必须声明为private、protected或包私有,并且只能通过实体类的方法直接访问。客户端必须通过访问器或业务方法访问实体的状态。

另一个有趣的资源是这个 YouTube 视频

TL;DR:@Entity 注解定义了一个类可以映射到一个表。

英文:

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.

huangapple
  • 本文由 发表于 2020年8月14日 22:07:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63414381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定