Hi Enrico.
Do you have an embedded id or id class defined for TblPdtbnf? Section
2.1.4 of the JPA spec says the following regarding composite primary
keys:
<spec>A composite primary key must correspond to either a single
persistent field or property or to a set of
such fields or properties as described below. A primary key class must
be defined to represent a composite
primary key. Composite primary keys typically arise when mapping from
legacy databases when the
database key is comprised of several columns. The EmbeddedId and
IdClass annotations are used
to denote composite primary keys. See sections 9.1.14 and 9.1.15. </spec>
If you do not have an embedded id or id class defined, defining one
may correct your problem. The spec has some good examples of their
usage.
-Jeremy
On Sat, Jun 14, 2008 at 7:46 AM, Enrico Goosen
<[EMAIL PROTECTED]> wrote:
>
> 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.
>
>