-->
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.  [ 4 posts ] 
Author Message
 Post subject: Evaluating Hibernate
PostPosted: Tue Jan 03, 2006 10:11 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:52 am
Posts: 52
Location: Zurich
We are currently investigating, if hibernate is suitable for our environment.
It has really nice features and we are very keen to give it a try.
I read the HiA book, the online manuals and searched the forum, wiki, ... to find an answer to following question (without success):

In our existing schema, we very often have the relationship, that a table T_X is referencing multiple parent tables (T_A, T_B, ...).

Code:
table T_A
  oid, ...

table T_B
oid, ....

table T_X
  oid, parentID,...

(All primary keys are unique for the database )

Table T_A corresponds to a class A which has a set of x's.
Table T_B corresponds to a class B which has another set of x's.
Class A and B are not related.

As there is no example in the book nor in the online documentation, I guess that this is not properly supported by hibernate!?
I know about the "<any > attribute, but this would require new columns in our schema to store the parent table name (why? requires an additional select and why to store a non dynamic relationship in the database and not just in the xml file?)

Is there an article on how to implement such a mapping without a schema change?

Thanks for your advice


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 03, 2006 3:36 pm 
Beginner
Beginner

Joined: Fri Mar 05, 2004 6:33 pm
Posts: 29
Location: Vancouver, BC, Canada
I understand parentID is either equal to T_A.oid or T_B.oid, right? When you query the tables outside hibernate, how do you figure out to which one you should join, T_A or T_B? Do you have some kind of table that keeps track of all the Ids and what table they are in?

Calin


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 03, 2006 5:14 pm 
Regular
Regular

Joined: Wed Dec 21, 2005 6:57 pm
Posts: 70
Do you need bi-directional associations in the mapped objects? That is, does the X class need to point back to the parent class? Often I see these in object models that don't need them -- in that case they just create an update headache and tight coupling.

Note that you can, worst case, put the parentID right in the child element as a member-variable of the class. It's not that clean, since this is really DB linkage information and should be "converted" into object refrerences in the object model, but it can be done.

e.g.
Code:
Parent {
[...]
   /** @hibernate.property ... **/
   addChild(X child) {
      children.add(child)
      child.setParentID(this.getID());
   }
}


I suspect you could also use an interceptor or custom SQL to accomplish this so that the object model is less polluted with DB concerns.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 04, 2006 5:32 am 
Regular
Regular

Joined: Tue Jan 03, 2006 9:52 am
Posts: 52
Location: Zurich
Thanks for your replies!

In response to Calin’s question:

The query to retrieve all x’s for and object a is done as follows,
SELECT * from T_X where parentID = a.oid

Because the parentID is unique in the database, the oids in table T_A and T_B are unique.
This is actually the main use for id generators which generate unique keys within the database and not just within a table. And hibernate supports them nicely. ;-)
In these cases it is not required to store the relationship (table name, which is a static relationship). This is why I do not get the picture with <any ..>).

Damon,
The relations do not have to be bidirectional.
Your code fragment describes the mechanism as it is done today (without hibernate) which is a bit messy as you pointed out correctly.

Interceptors:
When I use an Interceptor, I would have to load/delete,… the related objects myself, is that what you mean?
eg. for load it could look something like this:
Code:
public boolean onLoad (….)
  if (object instanceof A) {
    // read x’
    Set x = xDAO.getByParentID(a.getID());
    // initialize x’s in a
    a.setX(x);
    return true;
  }
  if (object instanceof B) {
   ..
  }
}
...

I will also look into the custom SQL, thanks for the tip.

However, with interceptor or custom SQL, I actually have to implement the relational logic "myself".
Isn’t that what hibernate should do for me? Or is this just an exotic ORM problem?
I doubt it.
In the object oriented world, we like to reuse our classes; in the relational world, we try to avoid tables with the same structure (normalization). But hibernate seems to force the user into the direction:
Class A needs a table T_A_X for his collection members x
Class B needs a table T_B_X for his collection members x
even though the elements (x's) are the same, i.e. both tables are identical.

I am aware, that sometimes the database must be denormalized and two or more separate tables make sense (ex. horizontal split for performance). But this is not always the case!

Urs


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