Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
741 views
in Technique[技术] by (71.8m points)

hibernate - How to define @OneToMany in parent entity when child has composite PK?

My Parent class has two child classes: Child and ParentHobby. The Child class has a singular PK and the @OneToMany mapping on it works. The problem is that I don't know how to map it on the ParentHobby class, which has a composite PK.

Parent:

//this works
@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", fetch = FetchType.EAGER)
private List<Child> childList;

//this DOES NOT work
@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent", fetch = FetchType.EAGER)
private List<ParentHobby> hobbyList;

Child:

@Entity
@Table(name="CHILD")
public class Child {


    @Id
    @SequenceGenerator(name="CHILD_SEQ", sequenceName="CHILD_DB_SEQ", allocationSize = 1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="CHILD_SEQ")
    @Column(name="CHILD_ID")
    private long childID;

    @JoinColumn(name = "PARENT_ID", referencedColumnName = "PARENT_ID", insertable = true, updatable = true)
    @ManyToOne(optional = true)
    private Parent parent;

ParentHobby:

@Entity @Table(name="PARENT_HOBBY") public class ParentHobby {

@EmbeddedId
private ParentHobbyPK id;

ParentHobbyPK:

@Embeddable
public class ParentHobbyPK {

    @JoinColumn(name = "PARENT_ID", referencedColumnName = "PARENT_ID", insertable = true, updatable = true)
    @ManyToOne(optional = true)
    private Parent parent;

    @Column(name="HOBBY_ID")
    private String hobbyID;

The exception I get at compile time is:

mappedBy reference an unknown target entity property: ParentHobby.parent in Parent.hobbyList

How can I define a @OneToMany relationship in a parent entity when the child has a composite primary key?

SIMILAR:

@OneToMany relationship with Composite key

Hibernate Entity mapping when Foreign key is part of the composite primary key?

JPA composite key @OneToMany

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to use a derived identity.

ParentHobbyPK should look like this:

@Embeddable
public class ParentHobbyPK {
    @Column(name="HOBBY_ID")
    private String hobbyID;
    private long parentID; // corresponds to the PK type of Parent
}

ParentHobby should look like this (the important thing being the @MapsId annotation):

@Entity
@Table(name="PARENT_HOBBY")
public class ParentHobby {
    @EmbeddedId
    private ParentHobbyPK id;

    @MapsId("parentID") // maps parentID attribute of the embedded ID
    @ManyToOne(optional = true)
    @JoinColumn(name = "PARENT_ID", referencedColumnName = "PARENT_ID", insertable = true, updatable = true)
    private Parent parent;

    ...
}

Derived identity is discussed in JPA 2.1 spec, section 2.4.1.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...