1

假设我有以下实体:

@Entity
@Table(name = "A")
public class A implements Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  @OneToMany
  @JoinTable(name="A_B", joinColumns = @JoinColumn(name = "A_ID"),
    inverseJoinColumns = @JoinColumn(name = "B_ID"))
  private List<B> bList1;

  @OneToMany
  @JoinTable(name="A_B", joinColumns = @JoinColumn(name = "A_ID"),
    inverseJoinColumns = @JoinColumn(name = "B_ID"))
  private List<B> bList2;

  // getters and setters

}

@Entity
@Table(name = "B") 
public class B implements Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  //some other data
  //getters and setters
}

@JoinTable 结构类似于:

A_B (a_id, b_id, discriminator);

是否可以使用一些注释来指定鉴别器列,基于哪个 bList1 和 bList2 将被填充?

4

1 回答 1

1

发布代码是不可能的,因为即使您可以在检索时指定@Where("discriminator='list1'")@Where("discriminator='list2'")在集合上进行过滤,但是休眠在插入/更新时仍然不维护鉴别器。

你可以试着做这个工作(我没有测试过这个):

@ManyToAny(metaColumn = @Column( name = "discriminator" ) )
@AnyMetaDef(idType = "long", metaType = "string", metaValues = {
    @MetaValue( value = "list1", targetEntity = B.class ) } )
private List<B> bList1;

@ManyToAny(metaColumn = @Column( name = "discriminator" ) )
@AnyMetaDef(idType = "long", metaType = "string", metaValues = {
    @MetaValue( value = "list2", targetEntity = B.class ) } )
private List<B> bList2;

或者您只需为具有鉴别器值的可连接件创建一个类,并在 list1 和 2 的属性中处理它。

于 2012-05-18T11:40:44.357 回答