ok. I can reproduce your problem. Let me take a look. -f
--- On Sat, 6/14/08, Enrico Goosen <[EMAIL PROTECTED]> wrote: > From: Enrico Goosen <[EMAIL PROTECTED]> > Subject: Re: @OneToMany/@ManyToOne, Bidirectional, Composite Key > To: [email protected] > Date: Saturday, June 14, 2008, 5:46 AM > Hi Fay, > > The primary key for TblScmpdt is database generated. > Here's the mapping: > > @TableGenerator(name="baseGenerator",schema="EBSTATUS",table="TBL_KEYGEN",pkColumnName="PRIMARY_KEY_COLUMN" > > ,valueColumnName="LAST_USED_ID",pkColumnValue="TBL_SCMPDT_ID",allocationSize=100) > @Id > @GeneratedValue(strategy=GenerationType.TABLE,generator="baseGenerator") > @Column(name = "SCMPDT_ID",nullable=false) > private Integer scmpdtId; > > As I mentioned in my previous post, the code works (no > exceptions), but its > flawed because of the bug in JPA when performing a cascade > persist on a > OneToMany entity where the child (many) entity has a > composite key. > > I should simply be able to do the following: > > TblScmpdt tblScmpdt = new TblScmpdt(); //new > (non-persistent) parent entity > //set fields on tblScmpdt ... > TblPdtbnf tblPdtbnf = new TblPdtbnf(); //new > (non-persistent) child entity > //set fields on tblPdtbnf ... > tblScmpdt.addTblpdtbnf(tblPdtbnf); //see method below, > which sets the parent > referrence on child > > //TblScmpdt method: > public void addTblPdtbnf(TblPdtbnf tblPdtbnf) { > tblPdtbnf.setTblScmpdt(this); //need to set both sides of > bidirectional > relationship > getTblPdtbnfs().add(tblPdtbnf); > } > //Now I should be able to persist parent and cascade > persist child > automatically according to JPA docs > tblScmpdt = em.merge(tblScmpdt); > > ...but unfortunately, the above doesn't work. > I have to merge/persist the parent first > then get the > ID of the newly > persisted parent and set it on the new child > then add > the child to the > parent > then merge the parent again. Tedious! > > Do you understand the problem? > > Regarding TblPdtbnfcde, this is a OneToOne reference on > TblPdtbnf. > TblPdtbnf's composite primary key is made up of > TblPdtbnfcde.pdtbnfId and > TblScmpdt.scmpdtId > > By the way, cascade persist works fine on my other > OneToMany classes where > there isn't a composite key. > So this is definitely a bug. > > > > Enrico, > > What is the primary key field in the TblScmpdt entity? > Did you set > primary key for tblScmpdt before calling merge? I made an > integer primary > key field in TblScmpdt, and set the primary key to 1 in > tblScmpdt before > calling merge, and your test code works fine for me (I also > omit > TblPdtbnfcde in your test code for lack of detailed > information). If you > already set the primary key and still get the error, please > specify more > detail of your TblScmpdt class. > > > -f > > -- > View this message in context: > http://www.nabble.com/%40OneToMany-%40ManyToOne%2C-Bidirectional%2C-Composite-Key-BUG-tp17801245p17839130.html > Sent from the OpenJPA Users mailing list archive at > Nabble.com.
