I'm new to JPA and I'm trying to map a legacy database. The files load correctly individually but the relationships are not working correctly. Any help would be appreciated.
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)
);
edit 1: add the ChildKey and ParentKey classes.