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

问题描述 投票:0回答:1

我想创建一个名为的类型

@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_new_types,列为agregator_id;
  • agregator_old_types,列为agregator_id;

我如何定义关系来获取这样的表和列?

hibernate jpa orm spring-data-jpa
1个回答
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;
}