@Entity和@embeddable有什么区别? [英] What is difference between @Entity and @embeddable

查看:174
本文介绍了@Entity和@embeddable有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 第一个创建类作为一个实体

    在每个类添加之前添加了@entity和@embeddable注解之间的区别? ,第二个从另一个表插入列?

  2. 第一个创建类作为表,而第二个嵌入另一个类?
  3. 第一集标准作为一个类,第二个定义表类型
  4. 该类的第一个创建表,第二个将某些东西嵌入到不同的类中第一个定义表属性,第二个创建两个表的联合


解决方案

@Entity 注解超过一个类定义它,它有一个明显的单独的存在。因此,我们可以运行数据库查询,而不依赖于任何其他类。 @Embeddable 类的注释定义了它,它没有独立的存在。因此,我们不能运行数据库查询,而不依赖于其他类。这是一个更好地理解它的例子:

  @Entity 
用户
- long id
- 字符串名称
- 字符串电子邮件
@Embedded
- UserDetails userDetail

@Embeddable
UserDetails
- 日期dateOfBirth
- 字符串性
- 字符串地址
- 字符串婚姻状态

在这里你可以看到没有 User UserDetails 没用。



通常,在OOP中,我们首先设计类,然后设计数据库实体。对于某些类(如上例中的UserDetails类),我们不希望在DB中拥有单独的表,其独立存在是无意义的。在这些情况下,我们将该类标记为可嵌入。



通常,可嵌入类与嵌入它们的实体共享同一个表

The difference between @entity and @embeddable annotation when each one is added before class declaration?

  1. the first create class as an entity, second insert column from another table?
  2. the first create class as an table, while second is embedded in another class?
  3. the first sets standard as a class, second define table type
  4. the first create table for that class, second embed something into different class
  5. the first define table property, second create union of two tables

解决方案

@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

Here you can see without having a User, UserDetails is useless.

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有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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