帮助在JPA 2.0中映射复合外键

Jar*_*son 6 orm jpa

我是JPA的新手,我正在尝试映射遗留数据库.文件单独正确加载但关系无法正常工作.任何帮助,将不胜感激.

Java的

@Entity
@IdClass(ParentKey.class)
public class Parent {
    @Id
    @Column(name="code")
    private String code;

    @Id
    @Column(name="id")
    private int id;

    @OneToMany(mappedBy="parent")
    private List<Child> children = new ArrayList<Child>();
}

public class ParentKey {
    private String code;
    private int id;
}

@Entity
@IdClass(ChildKey.class)
public class Child {
    @Id
    @JoinColumns({
        @JoinColumn(name="code")
        @JoinColumn(name="id")
    })
    private Parent parent;

    @Id
    @Column(name="index")
    private int index;
}

public class ChildKey {
    private String code;
    private int id;
    private int index;
}
Run Code Online (Sandbox Code Playgroud)

SQL

create table Parent(
  code char(4) not null,
  id int not null,
  primary key(code,id)
);

create table Child(
  code char(4) not null,
  id int not null,
  index int not null,
  primary key(code, id, index),
  foreign key(code, id) references Parent(code,id)
);
Run Code Online (Sandbox Code Playgroud)

编辑1:添加ChildKey和ParentKey类.

Dat*_*eus 2

以下是复合身份 1-N 关系的 DataNucleus 文档的链接。可以帮助您确定问题所在。首先,您没有在 Child 上定义 IdClass

http://www.datanucleus.org/products/accessplatform_3_0/jpa/orm/compound_identity.html#1_N_coll_bi