1

I'm trying to work with Hibernate criteria to do a join (returning data from both tables) where there is NO relationship defined in the HBM.XML configuration. There is no relationship defined because I'm trying to create a 'generic' relationship table that points to many different tables. Say I have 2 type of 'parent' objects, ParentType1 and ParentType2. Each of these parents can have lists of multiple TYPES of children, ChildType1, ChildType2, etc.

class ParentType1 {
    int P1ID;
    attributes ...
}

 class ParentType2 {
     int P2ID;
     attributes ...
 }

 class ChildType1 {
     int C1ID;
     attributes ...
 }
 etc . . .

The actual child OBJECTS can be related to multiple parents, so I created a relationship tables/objects at first, one for each type of child :

class ChildType1_Rel {
    String parentType;
    int    parentId;
    String relationType;
    int    childId;
}

class ChildType2_Rel {
    String parentType;
    int    parentId;
    String relationType;
    int    childId;
}
etc . . .

But then I figured why should I need new relationship objects and new tables for every type of child, so I created a generic relationship table

class Children_Rel {
    String parentType;
    int    parentId;
    String relationType;
    String childType;
    int    childId;
}

In the first multiple-relation tables situation, I could create many-to-many relationships from the ChildType1_Rel table to the ChildType1 table, and ChildType2_Rel table to ChildType2 because I knew what object to join to. However, in Children_Rel, I don't because the type of child is in the column value for that relationship. I thought there might be some type of conditional HBM.XML mapping syntax that would map an object based on a value in column, but didn't find anything.

Given my application, I do not and can not retrieve ALL data related to a parent at once, hence why I dont want many-to-many relations defined in the HBM.XML). The reason is that 'child' data is displayed in Infragistics GRIDs that needs to use remote paging, so once I have the parent ID, the grids call servlets to get each type of child data. So my servlet query would look like this :

SELECT * FROM Children_Rel CR, ChildType1 C 
  WHERE CR.parentType = 'Parent1'
    AND CR.parentId = X 
    AND CR.childType = 'ChildType1' 
    AND CR.childId = C.childId

My questions are : 1) Does this kind of model make sense or is it making things more difficult? 2) How can I model that SQL in criteria? Its not really that complicated but I cannot figure out how to join tables that do not have a hibernate-defined relationship. Nor do I see how I can get back 2 separate objects from Hibernate.

Thanks for any help.

user3708842
  • 463
  • 5
  • 23
  • 1) it's a big misuse/waste of hibernate, but it seems comprehensible and legal ..the sense is up to you 2) Is a du(tri)plicate!! (http://stackoverflow.com/questions/720502/hibernate-criteria-joining-table-without-a-mapped-association), ..in short: You can't use criteria, but detachedCriteria! ;) ..or HQL (http://stackoverflow.com/questions/974085/join-without-association-in-hql) – xerx593 Apr 17 '15 at 03:33
  • 1
    Thanks for your reply. I rethought about this situation and after seeing this and other difficulties I went back to having one relationship table per object. So with this I can use Hibernates many-to-one for queries. Thanks again. – user3708842 Apr 27 '15 at 17:35

0 Answers0