Spring JPA 中的@Entity 是什么?

What is @Entity in Spring JPA?

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

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

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

对于 Spring JPA 来说,class 是一个实体是什么意思?

我四处搜索以找到答案。我决定 post 无论如何, 我抬头 docs.oracle.com 在 post 我的问题之前。

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.

实体 class 必须遵循这些要求。

  • 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.

另一个有趣的资源是 youtube video

TL;DR: @Entity 注解定义了 class 可以映射到 table.

实体类型的

a class 表示 class,在抽象级别上,它与数据库中的 table 相关。 这个 class 实例化的每个对象表示 table 本身的一个元组,包含后者的信息。 我建议您找出 object relational mapping 是什么。 我还推荐 this page 在 Spring

上谈论 ORM 的(Spring 文档)