Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJB 3.0 - entity relationship persistence problem

 
Dmitry Danilov
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of them needs the "mappedBy" attribute set, so that the two relationships know that they point to each other.

Mark
 
Dmitry Danilov
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dmitry Danilov
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic