wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes EJB 3.0 - entity relationship persistence problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "EJB 3.0 - entity relationship persistence problem" Watch "EJB 3.0 - entity relationship persistence problem" New topic
Author

EJB 3.0 - entity relationship persistence problem

Dmitry Danilov
Ranch Hand

Joined: Jan 27, 2004
Posts: 38
Hi guys,

I have one nasty trouble persisting entity relationship. Here are my classes:

1. OneToMany class:


2. ManyToOne class


Before the object is persisted, all properties are populated except for both ids - autogenerated PKs, and internalId in the 2nd class - the foreign key - also null.
When I try to persist the object, I get
Seems like the ID value is not propagated down and INTERNAL_ID is never updated when the second class is persisted.

Could someone HELP please? I've been struggling with for more than 5 days already... I'm pretty sure it's something small and silly...

P.S. Weblogic 10, persistence provider - Kodo
[ April 08, 2008: Message edited by: Dmitry Danilov ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

One of them needs the "mappedBy" attribute set, so that the two relationships know that they point to each other.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Dmitry Danilov
Ranch Hand

Joined: Jan 27, 2004
Posts: 38
Hi Mark,
Thanks very much for replying.

I already have one "mappedBy" in the @OneToMany annotation in the 1st class, do you mean I need it somewhere else as well?
[ April 09, 2008: Message edited by: Dmitry Danilov ]
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
You have two mappings for the same column (INTERNAL_ID) both the Basic and the OneToOne. Ideally remove the Basic mapping/attribute. Otherwise you could try making the Basic read-only (insertable=false, updateable=false) to ensure the column is written from the OneToOne.

You could also ensure the internalId is set when you set the OneToOne, to do this you will need to first persist the target and call flush to assign the id. You could also use table sequencing and then you would not require calling flush.


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Dmitry Danilov
Ranch Hand

Joined: Jan 27, 2004
Posts: 38
Thanks James.
It seems I've found the root of the evil - Kodo. It does not automatically propagate changes from one field in bidirectional relation to the other field unless you explicitly add Kodo's config property "kodo.InverseManager = true" in persistence.xml.

That problem is gone now, but another emerged - "Attempt to set column "ER_CORPORATE_EVENT_VERSIONS.INTERNAL_ID" to two different values: (class java.lang.Long)"268", (null)"null" This can occur when you fail to set both sides of a two-sided relation between objects, or when you map different fields to the same column, but you do not keep the values of these fields in synch."

I'm at a loss again. What might be the cause of this? I just don't see any obvious errors in the code...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB 3.0 - entity relationship persistence problem
 
Similar Threads
java.sql.BatchUpdateException: ORA-02049
Saving database records problem
Issue while trying to map extended class
JPA - need help with query
Duplicated rows on the database