File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Hibernate: one class many tables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate: one class many tables" Watch "Hibernate: one class many tables" New topic

Hibernate: one class many tables

Igor Semenko

Joined: Sep 15, 2003
Posts: 2
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

Joined: Mar 14, 2001
Posts: 1902

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.

Theodore Jonathan Casser
SCJP/SCSNI/SCBCD/SCWCD/SCDJWS/SCMAD/SCEA/MCTS/MCPD... and so many more letters than you can shake a stick at!
Igor Semenko

Joined: Sep 15, 2003
Posts: 2
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

Joined: Mar 14, 2001
Posts: 1902

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

Joined: Jun 19, 2001
Posts: 547
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 :-) )



(blatant advertsing: check out my hibernate/xdoclet tutorial on

[ August 18, 2004: Message edited by: pascal betz ]
[ August 18, 2004: Message edited by: pascal betz ]
I agree. Here's the link:
subject: Hibernate: one class many tables
It's not a secret anymore!