-->
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: Many-to-One relationship to a one-to-one solution
PostPosted: Mon Nov 24, 2008 12:31 pm 
Newbie

Joined: Sun Nov 23, 2008 6:16 pm
Posts: 1
Hi,

I'm wondering if there is a more elegant solution to my current problem:

I have two classes: User and Address.
A user has more than one address associated with it but only one current address.

Ideally I would like the user bean to contain only the latest address such that user.getAddress() returns the current address object for this user.
One possible solution would be to load all historic addresses for this user as a list ordered by date created and return only the latest address in the getAddress method.
Another solution would be to have a one-to-one mapping between user and address and split the address tables into to two: current_address and historic_addresses

Is there a more hibernate way of doing this? Such that I have a one-to-one mapping between user and latest address enforced by some kind of custom load SQL or configuration?

I imagine my hbm.xml files would look something like this:

Code:
<class name="User" table="user">
  <id name="id" column="id">
    <generator class="native"/>
  </id>
  <property name="firstName"/>
  <property name="LastName"/>
  <!-- Solution would go here -->
</class>

<class="name"Address" table="address">
  <id name="id" column="id">
    <generator class="native"/>
  </id> 
  <property name="street"/>
  <property name="town"/>
  <property name="createdDate"/>
  <many-to-one name="user" column="user_id"/> 
</class>

Thanks in Advance,
puow


Top
 Profile  
 
 Post subject: Re: Many-to-One relationship to a one-to-one solution
PostPosted: Wed Nov 26, 2008 12:29 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
You could use a <many-to-one ... formul="xyz" />. The formula part should return the correct foreign key for the address record. There you can use a native sql which finds out what the latest address is. Something like:


Code:
select top 1 address_id from person_address pa where pa.person_id = orig_person_id order by desc pa.address_date



and of course in sql server's dialect. Note orig_person_id is the mapped primary key of Person entity.




Farzad-


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.