Spring JPA-@OneToMany为每个关系创建单独的表

时间:2019-04-30 12:04:28

标签: hibernate jpa orm spring-data-jpa

我想创建一个名为

的类型
@Entity
class TestType {
   @Id
   private Long id;
   private String field1;
   private String field2;
}

@Entity
class Agregator{
   @Id
   private Long id;
   private String name;
   @OneToMany
   private List<TestType> newTypes;
   @OneToMany
   private List<TestType> oldTypes;
}

我想拥有3张桌子:

  • 聚合器
  • 具有agregator_id列的agregator_new_types;
  • 具有agregator_id列的agregator_old_types;

我需要如何定义关系以获得此类表和列?

1 个答案:

答案 0 :(得分:1)

@Embeddable
class TestType {
   @Id
   private Long id;
   private String field1;
   private String field2;
}

@Entity
class Agregator{
   @Id
   private Long id;
   private String name;
   @ElementCollection
   private List<TestType> newTypes;
   @ElementCollection
   private List<TestType> oldTypes;
}