EclipseLink:实体的PK的键映射,其中实体的PK是映射中的关键

时间:2015-12-04 19:31:16

标签: java eclipselink

我要做的是让Object3实体的主键包含其父Object1(@ManyToOne)和Object2,它是Object3在Object1(property1)中与其配对的映射中的键。这可能与EclipseLink有关,可能带有注释吗?

我希望有意义,总结一下:Object3的PK是(Object1,Object2)

@Entity
public class Object1{
  ...
  private Map<Object2,Object3> property1;
  ...
}

@Entity
public class Object3{
  @Column(name = "c1")
  private int number;
  @ManyToOne
  @JoinColumn(name = "Object1")
  private Object1 object1;
  ...
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

以这种方式看待它 - 您要将什么传递给em.find并获取特定的Object3实例? Object3需要映射到其主键的所有外键字段,或者它不是一个独立的实体。类似的东西:

  @Entity
  @IdClass(Object3PK.class)
  public class Object3{
    @Column(name = "c1")
    private int number;
    @Id
    @ManyToOne
    @JoinColumn(name = "Object1")
    private Object1 object1;
    @Id
    @ManyToOne
    @JoinColumn(name = "Object2")
    private Object2 object2;
    ...
  }

  public class Object3PK{
     private int object1;
     private int object2;
  }

在上面,Object3PK必须使用与Object1和Object2中的主键相同的类型。这被描述为here

在Object1中,您可以使用指向Object1 ManyToOne映射的标准OneToMany映射。缺少的唯一部分是MapKey

  @Entity
  public class Object1{
    ...
    @OneToMany(mappedBy="object1")
    @MapKey(name="object2")
    private Map<Object2,Object3> property1;
    ...
  }