-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problem accesing fields in one child - many parents scenario
PostPosted: Sun Jun 01, 2008 1:55 am 
Regular
Regular

Joined: Tue Feb 19, 2008 6:05 pm
Posts: 82
I have stumbled across a problem in mapping tables in hibernate to match my requirement and need help!

I have a base table with 5 different parent FK's stored in my table/object. I have defined these relationships as <many-to-one ... /> in my base table mapping file.

In the actual database, only ONE of the 5 different table FK's is valid with the other FK's stored as 0, meaning, whenever I need the parent 3 field my query would be

Code:
select parent3.someField From baseTable where parent3.FK != 0


I will have to add the "where parent3.FK != 0" in all of my 5 queries in order to get the 5 different parent fields via the baseTable.

However, our requirements need us to dynamically fetch all the fields from base table and their parents, i.e., users can pickup from all available fields of the baseTable and all the fields from the 5 different parent tables. A HQL query constructed as

Code:
select baseTableField1, parent1.someField2, parent2.someField3 From baseTable

will fail!!

This will have to be written as 5 different queries

Code:
select baseTableField1, parent1.someField2, From baseTable where parent1.FK != 0

select parent2.someField3 From baseTable where parent3.FK != 0

...

and then joined based on the baseTable PK. For sure, I do not mind having the other parent fields empty when showing the records of a particular parent.

Is there some property in Hibernate mapping file that can allow me to put a discriminator to fetch a particular parent records only when its FK is not 0??

Any other suggestions to modify our architecture?

-Thanks
Rama


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 01, 2008 6:38 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
I don't understand why you can't just get the "baseTable" entity by using hibernate, and then use "getParent1()" and "getParent2()" (and so on...) to retrieve your data.
This getters would return null for the non-existent parents, you don't need to specify additional "were" clauses.

Also a thing you could do is mark your entity as abstract and inherit it 5 times, each time implementing a getParent returning an appropriate type; in this case Hibernate will offer many different configurable strategies to differentiate the types; the 5 classes could be mapped to the same table but hide inexistent fields to you application logic, that could be a good think: you get the choice to use polymorphism to have them all use the same method name.

_________________
Sanne
http://in.relation.to/


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