问题描述
在类声明前添加@Entity和@Embeddable注解的区别?
The difference between @Entity and @Embeddable annotation when each one is added before class declaration?
- 第一个创建类作为实体,第二个从另一个表插入列?
- 第一个将类创建为表,而第二个嵌入到另一个类中?
- 第一个将标准设置为一个类,第二个定义表类型
- 第一个为该类创建表,第二个将某些内容嵌入到不同的类中
- 第一个定义表属性,第二个创建两个表的并集
推荐答案
@Entity
对一个类的注解定义,它有一个独特的独立存在.因此,我们可以运行数据库查询,而无需依赖任何其他类.@Embeddable
类上的注解定义,它不具有独立存在性.因此我们不能在不依赖其他类的情况下运行数据库查询.这是一个更好地理解它的示例:
@Entity
annotation over a class defines that, it has a distinct separate existence. Thus we can run DB queries, without being dependent on any other class. @Embeddable
annotation over a class defines that, it does not have independent existence. Thus we cannot run DB queries, without depending on other class. Here is an example to understand it better:
@Entity
User
-- long id
-- String name
-- String email
@Embedded
-- UserDetails userDetail
@Embeddable
UserDetails
-- Date dateOfBirth
-- String sex
-- String address
-- String maritalStatus
这里不用User
也能看到,UserDetails
没用.
Here you can see without having a User
, UserDetails
is useless.
一般来说,在OOP中,我们首先设计类,然后设计数据库实体.对于某些类(如上例中的 UserDetails 类),我们不希望在 DB 中有单独的表,它们的独立存在是没有意义的.在这些情况下,我们将类标记为可嵌入.
Generally, in OOP, we first design the classes and then we design database entities. For some classes (like UserDetails class in the above example), we do not want to have separate tables in DB, where their independent existence is meaningless. In those cases, we mark the class as embeddable.
通常,可嵌入的类与嵌入它们的实体共享同一张表
Typically, embeddable classes share the same table as the Entity in which they are embedded
这篇关于@Entity 和 @Embeddable 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!