@embeddable vs @entity用于映射集合

sha*_*ltc 17 java orm spring hibernate jpa

这一定是天真的,但我怀疑何时使用@Entity@Embeddable.

说我有一个User和一个Notification班级.

 @Entity
 public class User{
     //other properties
     @onetomany
     private List<Notification> notifications;
 }

 @Entity
 public class Notification{
     //properties
 }
Run Code Online (Sandbox Code Playgroud)

据我所知,将有类表UserNotification,以及映射第三个表.如果我这样做怎么办?

 @Entity
 public class User {
     //other properties
     @ElementCollection
     private List<Notification> notifications;
 }

 @Embeddable
 public class Notification{
     //properties
 }
Run Code Online (Sandbox Code Playgroud)

我知道这不会创建一个表Notification.但我仍然可以存储我的通知对象.我浏览了文档,但有些疑惑:

  1. 是否基于我是否希望将B类视为单独的表格?
  2. 是否存在创建表和可嵌入对象的性能差异?
  3. 除了直接查询表之外,我可以用除了可嵌入对象之外的其他对象做什么?

笔记

对于读这个问题的人来说,这个问题也可能对你有帮助.

dee*_*aut 9

  1. 是否基于我是否希望将B类视为一个单独的表?

是的,当您使用时@Embedded,您将该@Embeddable实体嵌入到@Entity类中,这使得它可以在同一个@Entity类表中添加嵌入实体的列.

  1. 是否存在创建表和可嵌入对象的性能差异?

使用时@Embedded,对于表创建,需要一个查询,也用于插入和选择行.但是,如果你不使用它,则需要多个查询,因此,使用@Embedded产生更多性能,我们可以说.

  1. 除了直接查询表之外,我可以用除了可嵌入对象之外的其他对象做什么?

删除相应的嵌入实体可能是,但可能存在完整性约束违规.