• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Circular reference on inheritance and one-to-one mapping

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Author
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic