Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mapping a single object to multiple tables

 
Harathi Rao
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have the following requirement in my project. I would appreciate your kind help.

Lets assume my object has A,B,C,D,E data members. I want to map
A,B,C to table 'X'
B,C,D to table 'Y'
A,C,E to table 'Z'.

How to achieve this in hibernate. How many mapping files I need to write ?

Thanks
Harathi
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, your requirement sounds a little confused. You appear to have common properties that belongs to more than one distinct object. So you update one object and you want to persist it to more than one table. Can I ask why? Could you perhaps give use a less abstract example?
 
Franck McGeough
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
personally I'd map each of these as a class to their corresponding table and then write a containing class if you want to pass them around as a logical unit.
 
Harathi Rao
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My requirement was clear. I want to map the following class to three tables
Class Test
{
int A,
int B,
int C,
int D,
int E
// getter & setter methods
}
I want to map
A,B,C to table X
B,C,D to table Y
A,C,E to table Z.

I dont want to write 3 different classes to persist to three tables. I want to map one single class 'Test' to three tables X,Y,Z.
Please give me the solution for this.

Thanks
Harathi
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My requirement was clear

With all due respect, they were not clear to me, so I hypothesised they might also not be clear to others. That is why I asked if you could explain them in another way. Please don't take this as a criticism, my intention was to make it easier for me (or any others who are reading this) to offer you help.

The only way you can map one object to more than one table is if it contains properties that span both tables. Hibernate identifies objects based on their type and their key property. Suppose you have a record in table X with the primary key value 1, and a record in table Y with the primary key value 1. The way you want to map these you would have two Test objects which, given they have the same type and id, are the same object. How could Hibernate (or any other ORM for that matter) know that one instance of Test maps to X and another to Y?

Another problem. If the properties of X, Y and Z are unrelated (i.e. X is a Person, Y is Location and Z is a Product) and there are no relationships between these tables you need separate objects in order to write a clear, understandable object model. That X, Y an Z all have a "name" property doesn't mean they are of the same type. If there are relationships between these tables and, for example, your common property C represents the same property then your ER model is not properly normalized.

This is why I asked if you could supply a less abstract example. Perhaps you are trying to do something which is perfectly reasonable, but the way you currently explain it doesn't look that way.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic