java - @OneToMany and composite primary keys? -


i'm using hibernate annotations (in spring), , have object has ordered, many-to-one relationship child object has composite primary key, 1 component of foreign key id of parent object.

the structure looks this:

+=============+                 +================+ | parentobj   |                 | objectchild    | +-------------+ 1          0..* +----------------+ | id (pk)     |-----------------| parentid       | | ...         |                 | name           | +=============+                 | pos            |                                 | ...            |                                 +================+ 

i've tried variety of combinations of annotations, none of seem work. closest i've been able come with:

@entity public class parentobject {     @column(nullable=false, updatable=false)     @id @generatedvalue(generator="...")     private string id;      @onetomany(mappedby="parent", fetch=fetchtype.eager, cascade={cascadetype.all})     @indexcolumn(name = "pos", base=0)     private list<objectchild> attrs;      ... }  @entity public class childobject {     @embeddable     public static class pk implements serializable {         @column(nullable=false, updatable=false)         private string parentid;          @column(nullable=false, updatable=false)         private string name;          @column(nullable=false, updatable=false)         private int pos;          @override         public string tostring() {             return new formatter().format("%s.%s[%d]", parentid, name, pos).tostring();         }          ...     }      @embeddedid     private pk pk;      @manytoone     @joincolumn(name="parentid")     private parentobject parent;      ... } 

i arrived @ after long bout of experimentation in of other attempts yielded entities hibernate couldn't load various reasons.

update: comments; have made progress. i've made few tweaks , think it's closer (i've updated code above). now, however, issue on insert. parent object seems save fine, child objects not saving, , i've been able determine hibernate not filling out parentid part of (composite) primary key of child objects, i'm getting not-unique error:

org.hibernate.nonuniqueobjectexception:    different object same identifier value associated     session: [org.kpruden.objectchild#null.attr1[0]] 

i'm populating name , pos attributes in own code, of course don't know parent id, because hasn't been saved yet. ideas on how convince hibernate fill out?

thanks!

the manning book java persistence hibernate has example outlining how in section 7.2. fortunately, if don't own book, can see source code example of downloading jpa version of caveat emptor sample project (direct link here) , examining classes category , categorizeditem in auction.model package.

i'll summarize key annotations below. let me know if it's still no-go.

parentobject:

@entity public class parentobject {    @id @generatedvalue    @column(name = "parentid", nullable=false, updatable=false)    private long id;     @onetomany(mappedby="parent", fetch=fetchtype.eager)    @indexcolumn(name = "pos", base=0)    private list<objectchild> attrs;     public long getid () { return id; }    public list<objectchild> getattrs () { return attrs; } } 

childobject:

@entity public class childobject {    @embeddable    public static class pk implements serializable {        @column(name = "parentid", nullable=false, updatable=false)        private long objectid;         @column(nullable=false, updatable=false)        private string name;         @column(nullable=false, updatable=false)        private int pos;        ...    }     @embeddedid    private pk id;     @manytoone    @joincolumn(name="parentid", insertable = false, updatable = false)    @org.hibernate.annotations.foreignkey(name = "fk_child_object_parentid")    private parentobject parent;     public pk getid () { return id; }    public parentobject getparent () { return parent; } } 

Comments