-->
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.  [ 1 post ] 
Author Message
 Post subject: Hql query for table per subclass with discriminator
PostPosted: Mon Mar 17, 2008 5:06 am 
Newbie

Joined: Mon Mar 17, 2008 4:43 am
Posts: 3
Hello,

I am using hibernate 3.2.6.ga and and Mysql 5.0.15.

I have an Answer pojo mapped like this:

Code:
<hibernate-mapping default-access="field">
   
    <class name="com.foo.model.Answer" table="answers">
        <id name="id" column="id" type="long">
            <generator class="native" />
        </id>
        <discriminator column="answerType" type="string" length="10" not-null="true"/>
        <version name="version" access="field" type="long" />
       
        <subclass name="com.foo.model.SimpleAnswer" discriminator-value="SIMPLE">
            <property name="answer" column="stringAnswer" length="512" />
        </subclass>
        <subclass name="com.foo.model.BooleanAnswer" discriminator-value="BOOLEAN">
            <property name="answer" column="booleanAnswer" />
        </subclass>
        <subclass name="com.foo.model.PredefinedAnswer" discriminator-value="PREDEFINED">
            <!-- this is a custom user type of hibernate TypeSafeEnumType -->     
            <property name="answer" column="predefinedAnswer" type="com.foo.persistence.PredefinedAnswerOptionType" length="6"/>
        </subclass>
    </class>
   
</hibernate-mapping>


I also have a wrapper pojo QuestionAnswer whose mapping is:

Code:
<hibernate-mapping default-access="field">
   
    <class name="com.foo.model.QuestionAnswer" table="answers_question">
        <id name="id" column="id" type="long">
               <generator class="native" />
        </id>
        <many-to-one name="parent" class="com.foo.model.Question" column="parent_id" not-null="true" insert="false" update="false"  />
        <many-to-one name="answer" class="com.foo.model.Answer"  unique="true" cascade="all" fetch="join"/>
    </class>
       
</hibernate-mapping>


I want to query the database and get something like :
"get all question answers whose answer value is TRUE" or "get all question answers whose answer value is 'foo'".
I am using hql and my query is :

Code:
Query q = session.createQuery(
                        "from QuestionAnswer where answer.answer=(:answerValue)").
                        setParameter("answerValue", "foo");


Whatever my query parameter is (string, boolean, or enum type), hibernate resolves it as of the last type defined in my Answer.xml and only looks in this column. Here is the resulting query:

Code:
select questionan0_.parent_id as parent2_59_, questionan0_.answer as answer59_ from answers_question questionan0_, answers answer1_ where questionan0_.answer=answer1_.id and answer1_.predefinedAnswer=?


If I change the ordering, it will look for answer1_.booleanAnswer and so on..

Is there any way I can query for all answer types and let hibernate resolve my input dynamically??

Thank you in advance,
Argyro Kazaki


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.