These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Bi-directional one-to-one association: 2 ends consistency?
PostPosted: Fri Oct 03, 2008 8:36 am 
Newbie

Joined: Fri Oct 03, 2008 8:23 am
Posts: 4
Hello,

I would like to better understand the programming semantics of the one-to-one bi-directional association.

For example:

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
unique="true"
not-null="true"/>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<one-to-one name="person"
property-ref="address"/>
</class>


Then, I have two java classes :

Person { Address address; .... }
Address { Person person; .... }


The program can make the two properties inconsistent.
In that case, which value the Hibernate takes into account ? :
Person.address or Address.person.


What is the easiest way to ensure the consistency between two opposite ends?

Thanks in advance your kind responses.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 03, 2008 1:25 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Quote:
What is the easiest way to ensure the consistency between two opposite ends?


The short answer is "not screwing the data", in the same way you would not do it when working directly with a database.

You must decide whether the Address in your model can exist independently or not. In other words, does it makes sense, in your program, that an Address be added to the database, not linked to any particular person (for further use, for example).

If Address is not an "independent entity", then putting mapping not-null="true" ,and enforcing the same on the database will help protect your data.

If you want more safeguards on the Java code side, you can try defining Adddress as a "depending composite element" (in the documentation: 8.2 Collections of dependent objects), rather than an entity.

_________________
Gonzalo Díaz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.