JPA @Entity 注释的确切含义是什么? [英] What is the exact meaning of the JPA @Entity annotation?

查看:37
本文介绍了JPA @Entity 注释的确切含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究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 注释是在类级别上的,这意味着作为该类实例的对象的字段将映射到 @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

  1. @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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆