-->
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.  [ 3 posts ] 
Author Message
 Post subject: What is the correct mapping for the relationship
PostPosted: Mon Jan 07, 2008 4:30 pm 
Newbie

Joined: Mon Jan 07, 2008 3:58 pm
Posts: 7
I have been trying to figure out the correct mapping for the relationship that I have to work with, still with no results. I am not able to change the database layout, but I have complete control of the hibernate mapping and java code. The following is an example of the database setup.

Vendor Table - (vendorid, name) [PK: vendorid]
(1, 'Big City Productions')

Vendor Address - (vendorid, locationCode, address, city, state) [PK: vendorid, locationCode]
(1, 1, '123 Anywhere Lane', 'Sometown', 'MO')
(1, 2, '1234 Dead End Street', 'Anothertown', 'CA')


Because I have to send the vendor object(s) to other programs that I have no control over, I need to map the data into a single "Vendor" class, for example:

Vendor
Long id
Long location
String name
String address
String city
String state


If I open a terminal connection and run the following sql statement:

select ven.vendorid, ven.name, add.locationCode, add.address, add.city, add.state from vendor ven join vendoraddress add on ven.vendorid = add.vendorid where ven.vendorid like 'SomeName%'

I get back a row for each of the name/address combinations:

(vendor name, address1)
(vendor name, address2)

But when I have tried to make the mapping in hibernate, I get an incorrect result. I always get the correct number of rows, but they are all "copies" of the first combination.

(vendor name, address1)
(vendor name, address1)

Ideally, I would do something similar to:

Code:
<class name="Vendor" table="vendor">
  <id name="vendorid"/>
  <property name="name"/>

  <join table="vendorAddress">
    <key column="vendorid"/>
    <property name="location"/>
    <property name="address"/>
  </join>
</class>

<query name="Vendor.getVendorsByName">
  from Vendor ven where ven.name like :searchName
</query>


I have tried numerous different things that I've read on other forums, but have only found one way that has worked properly. If I specify the address as a separate class and create a set mapping to that class instead of trying to make one big class, I get the correct results... it's just incredibly slow because hibernate is generating a select statement for each of the vendors to get their address. On just a couple vendors this is not a problem, but several queries have the potential to return a couple thousand vendors bringing the application to a halt. Any help with how this relationship should be mapped would be greatly appreciated.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 07, 2008 4:56 pm 
Newbie

Joined: Mon Jan 07, 2008 3:58 pm
Posts: 7
I should probably add, that I've tried the <join> mapping that I stated that I feel would be ideal. It seems to work perfectly for a one-to-one relationship, but since there can be multiple addresses for a vendor it does not work for all of them. It just duplicates the first address for all of the possible addresses of a vendor.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 07, 2008 5:06 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
dvestal,

In your solution using a set of addresses, specify fetch="join" in your mapping. That will reduce the number of queries to 1.

_________________
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.  [ 3 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.