-->
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.  [ 7 posts ] 
Author Message
 Post subject: Mapping two class with a many-to-one on a common third
PostPosted: Wed Jun 28, 2006 5:45 pm 
Newbie

Joined: Wed Jun 28, 2006 5:29 pm
Posts: 8
Hi,
I am using hibernate 3.0 and
I wonder what is the correct mapping to take advantage of transitive persistence on a many-to-one relationship of two classes pointing on a third.

Basically,
a Person has 1..n address(es)
a company has 1..n address(es)

I'd like to have a single table for all addresses
I am only looking for an uni-directional relationship :
that is : given a person or a company get the adress set...

I've end-up with something like
Code:
<class name="Person" >
  <!-- ... skip id and other stuff ... -->
<set name="addresses" where="objectType='Person'" >
            <key column="extId" />
            <one-to-many class="Address" />
        </set>
</class>

<class name="Company" >
  <!-- ... skip id and other stuff ... -->
<set name="addresses" where="objectType='Company'" >
            <key column="extId" />
            <one-to-many class="Address" />
        </set>
</class>

<class name="Address">
    <!-- ... skip id and other stuff ... -->
   <property name="extId" type="integer" not-null="true" />
   <property name="objectType" type="string" not-null="true" />
       
</class>


And in my Java code I take care of storing the correct type and extId... This not elegant, and I am not sure that transitive persistence apply well.

I thing it's a classical case/pattern and I am sure the wisemen you are already have the solution to reach a better mapping.

regards.
TFlop


Top
 Profile  
 
 Post subject: Re: Mapping two class with a many-to-one on a common third
PostPosted: Wed Jun 28, 2006 5:59 pm 
Newbie

Joined: Wed Jun 28, 2006 5:29 pm
Posts: 8
I now believe that using my 3 tables plus 2 tabls for the joint is the correct mapping (as described in the section 7.3 of ref manual)
Person
Company
Address
PersonAddress
CompanyAddress

Any better mapping ?

TFlop wrote:
Hi,
I am using hibernate 3.0 and
I wonder what is the correct mapping to take advantage of transitive persistence on a many-to-one relationship of two classes pointing on a third.

Basically,
a Person has 1..n address(es)
a company has 1..n address(es)


I thing it's a classical case/pattern and I am sure the wisemen you are already have the solution to reach a better mapping.

regards.
TFlop


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 5:59 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
add 'cascade="all-delete-orphan"' to the two <set> elements to have hibernate look have transitive persistence for you.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 10:50 am 
Newbie

Joined: Wed Jun 28, 2006 5:29 pm
Posts: 8
tenwit wrote:
add 'cascade="all-delete-orphan"' to the two <set> elements to have hibernate look have transitive persistence for you.


Yes I came to this solution by myself, but what I do not understand is that what I try to achieve is very common I think :
Let me restate the case :
Code:
1 Person has 0..1 Address
1 Company has 0..1 Address
I use  a one-to-one unidirectional association (like in 7.3.3)
(5 tables : Person, Company, Address, PersonAddress, CompanyAddress)


but in the manual point 7.3.3 is said extremly unusual, so I think it must exist a better mapping than unidirectional one-to-one association on a join table

Do Hibernate guys preffer inheritance in this case
a class PersonAddress and anotehr CompanyAddress both extending Address. If so I don't see the point because in my case an address for a person is exactly the same object than an Address for a company.
So here we talk about composition.

What is the recommanded mapping for this pattern ?

I'll give credit for the answer, for sure because I don't want to start going in the wrong way...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 30, 2006 11:14 am 
Beginner
Beginner

Joined: Tue Nov 29, 2005 4:42 pm
Posts: 49
Location: Atlanta, GA
I had the exact same situation, and while I got it to map in hibernate it was a huge pain. Unfortunately, I don't have much control over my schema and the way it was mapped out in the DB was to have a CompanyAddress and a PersonAddress tables, but in the code I just wanted to have an Address class.

So I resorted to mapping the class twice using entity-name! Yuck! One for the CompanyAddress table and the other for PersonAddress table. I ended up making it bi-directional, which I could do by mapping it twice, and the Company and Person classes implemented the Addressable interface. I got it to work, but I'm not happy with it. I would be very interested in alternatives.

This seems like such common problem I have to think there is another way.

Thanks
Charlie


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 02, 2006 6:21 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
For one-to-zero-or-one, I recommend using a many-to-one unique="true". A one-to-one will work, but it's not strictly correct. one-to-zero-or-one is many-to-one unique="true"; one-to-one (strictly speaking) should not allow one-to-zero.

However in your case, I would recommend using polymorphic mapping, union-subclass. Section 9.1.5 of the ref docs explains it. You'll have one Address interface and two impl classes, CompanyAddressImpl and PersonAddressImpl.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 04, 2006 11:33 am 
Newbie

Joined: Wed Jun 28, 2006 5:29 pm
Posts: 8
tenwit wrote:
However in your case, I would recommend using polymorphic mapping, union-subclass. Section 9.1.5 of the ref docs explains it. You'll have one Address interface and two impl classes, CompanyAddressImpl and PersonAddressImpl.


Thx for the reply, my mapping use many-to-one unique=true inside a jointable node with optional=true (
<join table="PersonAddress" optional="true">
<key column="personId" unique="true"/>
<many-to-one name="address" column="addressId" not-null="true"/>
</join>) like described in 7.3
which implement a 0..1

If I understand well the mapping you suggest I end up with 2 tables for the two impl classes (PersonAddressImpl and CompanyAddressImpl) instead of my two relation classes...
I must be idiot, but I don't clearly see the pro of your solution.

Mr Chubbard I think my mapping work for you :

the tables in this example are :
person (id, and other column)
company (id, and other columns)
address(id, and other column)
personAddress(idPerson,IdAddress)
companyAddress(idCopany,idAddress)

And I have only 3 classes : person, company and address

If your legacy-you-cannot-touch db schema is like this I do think it will work for you.

regards,

TFlop


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.