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