JPA @Entity批注的确切含义是什么? [英] What is the exact meaning of the JPA @Entity annotation?
问题描述
我正在研究Spring应用程序中的JPA,并且对与 @Entity 批注有关的一些疑问.
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;
...........................
...........................
...........................
}
好吧,我知道 @Entity 注释在类级别上,这意味着作为此类实例的对象的字段将映射到 T_CUSTOMER 数据库表.
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.
但是为什么在JPA中必须使用 @Entity 注释,而我不能仅使用 @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?
我想念什么? @Entity 批注的确切含义是什么?
What am I missing? What is the exact meaning of the @Entity annotation?
推荐答案
@Entity
注释定义可以将类映射到表.就是这样,它只是一个标记,例如Serializable
界面.
@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.
为什么必须使用@Entity
批注? ……这就是JPA的设计方式.创建新实体时,您至少需要做两件事
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
-
用
@Entity
创建一个id字段并用@Id
create an id field and annotate it with @Id
其他任何内容都是可选的,例如表名称是从实体类名称派生的(因此,@Table
注释可以是可选的),表的列是从实体变量派生的(因此,@Column
注释可以是可选的),以及等等...
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试图为想要学习/使用此API的开发人员提供一个快速简便的开始,并且为开发人员提供配置尽可能少的东西以使其具有功能性的选项是此API想要实现的方法之一这个易于使用/学习"的目标.因此,@Entity
注释(与@Id
注释一起)是创建实体所需要做的最少工作.
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.
这篇关于JPA @Entity批注的确切含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!