• 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

one - one mapping is being enforced with unique="true"

 
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

While learning the concepts of associations in ORM using Hibernate, i'm trying with an example 1-1 unidirection mapping between two entities.

ClassA


ClassB


Now I persisted tables initially as below

TABLE-A
+-------------+-------------+
| ID | B |
+-------------+-------------+
| 1 | null |
| 2 | null |
+-------------+-------------+

TABLE - B
+-------------+
| ID |
+-------------+
| 123 |
+-------------+

and then trying to map the association, by adding the key "123" as a foreign key to TABLE - A for the first record (PK = 1) and then tried to add "123" as a foreign Key again to another record with PK = 2. Which would effectively yield to Many to One, unidirectional. I used "unique = true" on <many-to-one> side of ClassA mapping file, with the hope that, Hibernate would enfore One - One. But it seems, it ignored it and still letting me map Many classA to one ClassB.

Is there anything wrong, I'm doing here. How do I enforce one - one mapping.
 
Kumar Raja
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I was able to enforce "unique" ness by adding another attribute "not-null = true" on ClassA mapping. But this would require me to add ClassB to ClassA, at the time of persisting ClassA entity.

But what if I want to associate ClassB to ClassA at a later point. Can't "unique = true" works in that scenario
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic