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.