This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • 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: 547
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic