Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate: one class many tables

 
Igor Semenko
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to have one class to be mapped into 2 tables in hibernate?
For example, I have 2 tables (which are 1-1 relation):

Customer (id*, name)
CustomerDetails (id*, comments)

and a class
Customer {
int id;
String name;
String comments;
}
I know, db schema is not 'right', but we can not change that.
Appreciate for any comments.
[ August 17, 2004: Message edited by: Igor Semenko ]
 
Theodore Casser
Ranch Hand
Posts: 1902
Hibernate Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's possible. There's actually an example in "Hibernate: A Developer's Notebook" (well, using music tracks instead of customers, but it's about the same, really, when you think about it ) which allows for multiple comments to be associated with a single track. Simply declare a set with the second column being a string, and use that for storage of the comments.

Similar functions should be easy enough to implement, allowing you to do what you're asking, if I'm understanding correctly.
 
Igor Semenko
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Theodore, thank you for the response.

I may probably have not specified requirements clearly, I understand that Hibernate can load related entries from another table into a list of objects of another type, in my case it would be Customer containing a set of CustomerDetails.

But I want only one object Customer to be loaded from/to different tables Customer/CustomerDetail which have the same primary key id. Basically Customer and CustomerDetails is the one table splitted into two, relation between tables is 1-1. So for one particular Customer object it would load properties "id" and "name" from table Customer and "comments" from CustomerDetails.

As for now, we come to solution when CustomerDetails is loaded as a separate object, but its propertly "comments" is exposed by delegate method in Customer class.
 
Theodore Casser
Ranch Hand
Posts: 1902
Hibernate Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I didn't read clearly, you're right. I'm a little fuzzy-headed this morning, I'm afraid.

I'm sure there's a way to do it (without using a set-based kludge). I'm just not thinking clearly enough at the moment about how best to go about it.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
ok, if i got you right, then you want to map different properties (name, comments) to be mapped on different tables ? here are some notes about this:

- you can not map properties of one class to different tables in version 2 (dont know if this is possible in version 3). perhaps you can work around this but i dont think so.

- you can use a one-to-one relationship with a foreign key generator.
then your Customer holds a reference to CustomerDetail and they share the primary key. check the hibernate reference doc, there is an example of one-to-one with foreign key

- you can not map one class on two tables with current version of hibernate (will be available with version 3, just dont ask the hibernate team if they think this is a good design idea :-) )

cheers

pascal

(blatant advertsing: check out my hibernate/xdoclet tutorial on http://www.downside.ch/pascal)

[ August 18, 2004: Message edited by: pascal betz ]
[ August 18, 2004: Message edited by: pascal betz ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic