-->
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.  [ 4 posts ] 
Author Message
 Post subject: Reverse engineer tables with foreign (and no primary) key
PostPosted: Thu Mar 06, 2008 6:08 am 
Newbie

Joined: Thu Mar 06, 2008 5:27 am
Posts: 5
Hi!

I have two tables:

Code:
-------------   -------------
| Person    |   | Addresses |
-------------   -------------
| ID        |   | Person_ID |
| FirstName |   | Street    |
| LastName  |   | City      |
| BirthDate |   | Zip       |
-------------   -------------


where ID in table person is the primary key and referenced in table Addresses as a foreign key in the field Person_ID. When I now reverse engineer this schema with the Hibernate tools in Eclipse, a composite ID will be created for all the columns in the table Addresses. I then have three classes:

Code:
Person
AddressesID
Addresses


where Addresses is mainly a wrapper for the AddressesID class and all the fields are contained in the AddressesID class. When I want to create a new Address, I have to instantiate a new AddressesID class and set all fields (street, city, zip) in this class. Then I have to use this class and pass it in the constructor of the Addresses class. Again when retrieving values, I have to do something like this:

Code:
person.getAddresses().iterator().next().getAddressesID().getStreet();


I have this ugly indirection over AddressesID. I want something like this:

Code:
person.getAddresses().iterator().next().getStreet();


Now my question: is there a way to directly map the table addresses to a class Addresses without introducing this composite ID? I have seen that any mapping file must contain an id|composite-id element. How can I reference the foreign key Person_ID in this mapping, without having to introduce a new key?

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 06, 2008 2:02 pm 
Beginner
Beginner

Joined: Thu Nov 15, 2007 11:27 am
Posts: 34
The cleanest way to do it is to add in your address table an id (an artificial identifier).
So alter the address table to add a column "id" unique, not null... set it as primary key, set a uniqu constraint on the old primary key if you need, and re-reverse.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 06, 2008 2:30 pm 
Newbie

Joined: Thu Mar 06, 2008 5:27 am
Posts: 5
Indeed, that would be a solution, but not a very pretty one. I would introduce an artificial key, that is only there for Hibernate to get its unique identifier for every object, but has no value for the data in my database. In some way it's obfuscating my database design.

I think, I am missing something important here. I am probably not the only one who has such a table structure. And I think there must be a way (probably an obvious one) in Hibernate to express this kind of relationship without having to introduce artificial keys or creating additional composite-id classes.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 07, 2008 4:20 am 
Beginner
Beginner

Joined: Thu Nov 15, 2007 11:27 am
Posts: 34
Check http://www.hibernate.org/hib_docs/refer ... ompositeid

If you don't want to add an artificial id (it seems they recommend that...), you can use composite id like:
<composite-id>
<key-property name="medicareNumber"/>
<key-property name="dependent"/>
</composite-id>

(may be you will found this option in your reverse tool)
So the id is not mapped on a class, but on property of your bean. Basically reverse engineering do what they recommend (i.e. do a class for the composite id.)

Other way may be to add a "getStreet()" in your address object that will do the getAddressesID().getStreet().


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