Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Hibernate: one class many tables

 
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 ]
 
Ranch Hand
Posts: 1902
Hibernate Spring Java
  • 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 Spring Java
  • 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.
 
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 ]
 
Onion rings are vegetable donuts. Taste this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic