-->
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.  [ 5 posts ] 
Author Message
 Post subject: HQL queries and polymorphism
PostPosted: Wed May 25, 2005 11:07 am 
Newbie

Joined: Wed May 25, 2005 10:37 am
Posts: 5
Hi, I am running into a problem when using HQL (or Criteria API) with Hibernate 3, I've been reading some of the posts but I am not sure yet if it's possible to do what I want.

The question is, given the next mapping:

Code:
<class
    name="A"
    table="values"
    lazy="false">

    ...

    <subclass
        name="B"
        discriminator-value="1">

        <property name="value" type="string">
            <column name="string_value" sql-type="text"/>
        </property>
       
    </subclass>
   
    <subclass
        name="C"
        discriminator-value="2">

        <property name="value" type="double">
            <column name="number_value" />
        </property>
    </subclass>
   
</class>


and the next classes:

Code:
public abstract class A {
    public abstract Object getValue();
    public abstract void setValue(Object obj);
}

public class B extends A {
    private String value;
    public Object getValue() {
        return value;
    }
    public voud setValue(Object obj) {
        this.value = (String) obj;
    }
}

public class C extends A {
    private Integer value;
    public Object getValue() {
        return value;
    }
    public voud setValue(Object obj) {
        this.value = (Integer) obj;
    }
}


Is it possible to execute polymorphic HQL queries such as the next ones?:

Code:
from A as a where a.class = B and a.value = 'test'      // It should check the string_value field

from A as a where a.class = C and a.value = 1           // It should check the number_value field



In my tests Hibernate doesn't seem "smart" enough to choose the right field of the table, but maybe I am doing something wrong or it's simply not possible.

Could anyone give me any clue about this?


Thanks![/code]


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 9:19 am 
Newbie

Joined: Wed May 25, 2005 10:37 am
Posts: 5
I have implemented a workaround using SQL and criteria API, but is not very nice though.

Is the HQL stuff definitely not supported by the current version?


Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 10:13 am 
Regular
Regular

Joined: Fri Sep 17, 2004 10:51 am
Posts: 61
Location: Rimini, Italy
Why you dont' simply use:
Code:
from B as b where b.value = 'test'

and

from C as c where c.value = 1


Instead of specify the class instance as a where clause, simply use it in the from. You can always up-cast the result to A.

_________________
--
Marco


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 18, 2005 6:31 pm 
Beginner
Beginner

Joined: Sun Jun 13, 2004 9:49 pm
Posts: 38
Any more comments on this one.

I'm running into the same problem but cannot use the from B or from C as marco suggested.

A user types a product name into a text box and i'd like to look up the record, but i don't know what type it is.

So, i want to do from A where a.blah = "what the user typed".

Is this possible yet?


Top
 Profile  
 
 Post subject: similar problem
PostPosted: Wed Feb 08, 2006 11:47 pm 
Newbie

Joined: Wed Feb 08, 2006 11:40 pm
Posts: 8
I've got a similar problem in that 2 subclasses (B anc C) of A have a field with the same name and same type (string). When I do a criteria search, the hql shows that its searching for the common field only in B. In other words, I get something like this:

... where (B.common like ?) or (B.common like ?)

This is sort of a bug because I need it to be:

... where (B.common like ?) or (C.common like ?)

It works properly for fields that don't have the name across B and C. It seems that the problem can easily be fixed by allowing the classname to be given along with the field name when creating the expression.

I'm generating a generic search using refelction and the line that does the actual work is:

disj.add(Expression.like(fld, sToken, MatchMode.ANYWHERE));

From this point, it would be very easy for me to include the class to search in (so that I can tell it to search in B.common and C.common as needed. Is there a way to do this? There is a chance I've overlooked this.

ps. Don't ask why the common field isn't moved to the superclass, its not my decision to make.


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