6

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.

Jared Pearson
  • 487
  • 1
  • 5
  • 18

3 Answers3

2

Here's a link to DataNucleus docs for compound identity 1-N relation. May help you identify what is wrong. For a start you have no IdClass defined on Child

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

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
  • I forgot to add the @IdClass to my example the first time but it has now been corrected. Thanks for the link but it doesn't have an example of a primary key that includes a foreign key. I need a more complex example that represents the relationship given in the example above. – Jared Pearson Apr 19 '11 at 19:55
  • Yes it does. Address.PK has Account as part of its PK. Your ChildKey is incorrect – DataNucleus Apr 20 '11 at 07:48
  • You are correct; I didn't see it when I read through it. I ended up using OpenJPA's reverse mapping tool and it gave me the solution. Thanks for your help. – Jared Pearson Apr 20 '11 at 12:06
2

Here is what OpenJPA created using its ReverseMapping Tool and it appears to be working correctly.

@Entity
@Table(name="PARENT")
@IdClass(ParentId.class)
public class Parent {
  @OneToMany(targetEntity=Child.class, mappedBy="parent", cascade=CascadeType.MERGE)
  private Set childs = new HashSet();

  @Id
  @Column(length=4)
  private String code;

  @Id
  private int id;

  //omitted getters, setters
}

public class ParentId implements Serializable {
  public String code;
  public int id;

  //omitted getters, setters, toString, equals, hashcode
}

@Entity
@Table(name="CHILD")
@IdClass(ChildId.class)
public class Child {
  @Id
  @Column(length=4)
  private String code;

  @Id
  private int id;

  @Id
  private int index;

  @ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.MERGE)
  @JoinColumns({@JoinColumn(name="code"), @JoinColumn(name="id")})
  private Parent parent;

  //omitted getters, setters
}

public class ChildId implements Serializable {
  public String code;
  public int id;
  public int index;

  //omitted getters, setters, toString, equals, hashcode
}
Jared Pearson
  • 487
  • 1
  • 5
  • 18
0

See also http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#JPA_2.0 (add @Id to @ManyToOne field)

Grigory Kislin
  • 16,647
  • 10
  • 125
  • 197