• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Circular reference on inheritance and one-to-one mapping

 
Corneliu Bogdan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have problems configuring some relationships. I have class SimplePrice and class RolledPrice, the second inherits the first.
The configuration file is like this:



What happens is that I have a new instance "a" of type "RolledPrice" and I assign to the "contractPrice" property a "SimplePrice" instance (that's already persisted, with the id 100 for example), after that I call "hibernateTemplate.persist(a)". Later when I retrieve the "RolledPrice" instance added before (for example now it has id 1001), it has a reference to itself in the property "contractPrice", instead of a "SimplePrice" instance with the id 100.

This happens during a JUnit test, using Spring and in-memory HSQLDB.

Could someone tell me what am I doing wrong?

Thank you,
 
Javid Jamae
Author
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't thought through it thoroughly, but you are possibly having an issue with the way that Hibernate manages one-to-one relationships; it keeps matching primary keys in both tables.

The other way to do a one-to-one mapping in Hibernate is to use a many-to-one mapping with a unique constraint on the many side. Have you tried this?
 
Corneliu Bogdan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Thank you very much for your idea. I've change the "one-to-one" to

<many-to-one class="org.crb.entities.SimplePrice" name="contractPrice" unique="true" lazy="false"/>

It works like a charm.

All the best,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic