-->
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.  [ 3 posts ] 
Author Message
 Post subject: Creating queries to a ISet of different objects
PostPosted: Fri May 12, 2006 4:53 am 
Newbie

Joined: Thu May 11, 2006 11:33 am
Posts: 2
Location: Moscow, Russia
Hello all! Can anybody help me to resolve this problem?

Example:
Code:
class A {
   ISet lines; // contents objects of B1 & B2 classes
...}

class B {
   int b_field;
...}

class B1 : B {
   int b1_field;
...}

class B2 : B {
   int b2_field;
...}

So, B1 & B2 inherite from B and have one specific field each.

I map class A (TABLE_A) this way:
Code:
<set name="lines" lazy="true"...>
   <key column="..." />
   <one-to-many class="B" />
</set>


Classes B1 & B2 are mapped as subclasses in mapping for B (TABLE_B).
This is a typical situation and it works well. But what will you do, if you want to write a query for class A?!

Example:
Code:
ICriteria criteria = session.CreateCriteria(typeof(A))
                                      .CreateCriteria("Lines")
                                         .Add(Expression.Like("b_field", "...", MatchMode.Anywhere))
                                         .Add(Expression.Like("b1_field", "...", MatchMode.Anywhere))
                                         .Add(Expression.Like("b2_field", "...", MatchMode.Anywhere));

You will get the error that the 'b1_field' cannot be resolved in class B2 and b2_field in B1.

What to do???
And some extra question from me as a lamer: for illustrated createria (if delete two last rows) hibernate creates SQL query and use INNER join for TABLE_A and TABLE_B. How to force it use LEFT join?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 12, 2006 7:25 pm 
Newbie

Joined: Sun Aug 28, 2005 10:54 pm
Posts: 14
If I recall correcly, King and Bauer's book "Hibernate in Action" states that you need a type descriminator column on the table in order for the persistence tier to distinguish between concrete classes.

Take a look at http://www.hibernate.org/hib_docs/nhibernate/html/inheritance.html for an example of how to implement the mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 15, 2006 7:32 am 
Newbie

Joined: Thu May 11, 2006 11:33 am
Posts: 2
Location: Moscow, Russia
I have them. Possibly I have to use HQL because in the way as it is now it will work never.

Anyway thanks.


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