aspose file tools*
The moose likes Object Relational Mapping and the fly likes one - one mapping is being enforced with unique= Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "one - one mapping is being enforced with unique="true"" Watch "one - one mapping is being enforced with unique="true"" New topic
Author

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

Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 516
    
    2

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.


Regards
KumarRaja

Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 516
    
    2

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: one - one mapping is being enforced with unique="true"
 
Similar Threads
Many-to-many composite ID
Legacy and Hibernate1-2-M Mapping
many-to-one relationship to non-PK field
How could I map this if possible?
Save objects problem