Справка Сопоставление составного внешнего ключа в JPA 2.0

Я новичок в 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;
}

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)
);

редактировать 1: добавьте классы ChildKey и ParentKey.

6
задан Jared Pearson 19 April 2011 в 19:54
поделиться