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