-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to return proper discriminator subclass in where-clause?
PostPosted: Tue Jan 09, 2007 12:27 pm 
Newbie

Joined: Tue Jan 09, 2007 12:01 pm
Posts: 3
Hello,
I have the following object model in which various "type" classes (BooleanAttribute, DateAttribute, etc.) subclass from an AbstractAttribute class. The subclasses store their values in type-specific columns through the use of a discriminator. I'm trying to construct an HQL query where I'm returning BooleanAttributes whose value is "true". The problem is that even if I include a ".class=" statement in the where-clause, Hibernate still seems to want to return the "string_value" column (causing a SQL error) instead of the "boolean_value" column. How do I force it to return the "boolean_value" column for the comparison? Is there a "cast" that I can perform? Details below.
Thanks!

Hibernate version: 3.1.3

Mapping documents:

Relevant Portion:
<discriminator column="discriminator" type="string"/>
<subclass name="com.attribute.model.StringAttribute" discriminator-value="STR">
<property name="value" column="string_value" type="string"/>
</subclass>
<subclass name="com.attribute.model.FloatAttribute" discriminator-value="FLT">
<property name="value" column="float_value" type="float"/>
</subclass>
<subclass name="com.attribute.model.LongAttribute" discriminator-value="LNG">
<property name="value" column="long_value" type="long"/>
</subclass>
<subclass name="com.attribute.model.IntegerAttribute" discriminator-value="INT">
<property name="value" column="integer_value" type="integer"/>
</subclass>
<subclass name="com.attribute.model.BooleanAttribute" discriminator-value="BOO">
<property name="value" column="boolean_value" type="boolean"/>
</subclass>
<subclass name="com.attribute.model.DateAttribute" discriminator-value="DAT">
<property name="value" column="date_value" type="date"/>
</subclass>
<subclass name="com.attribute.model.CurrencyAttribute" discriminator-value="CUR">
<property name="value" column="float_value" type="float"/>
</subclass>

Code between sessionFactory.openSession() and session.close():

Relevant Portion:
queryStr += (" and a3.attributeDefinition.name='" + Constants.PRODUCT_ATTRIBUTE_NAME_NEWMEDIA + "' and a3.class=com.attribute.model.BooleanAttribute and a3.value=true");

Full stack trace of any exception that occurs:

org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could not execute query; bad SQL grammar [select distinct product0_.product_id as product1_20_, product0_.edition_id as edition2_20_, product0_.product_number as product3_20_, product0_.parent_source_product_id as parent4_20_, product0_.group_type_id as group5_20_, product0_.created_date as created6_20_, product0_.created_by as created7_20_, product0_.modified_date as modified8_20_, product0_.modified_by as modified9_20_ from product product0_ inner join relationship relationsh1_ on product0_.product_id=relationsh1_.product_id inner join abstract_attribute attributes2_ on relationsh1_.relationship_id=attributes2_.relationship_id inner join relationship relationsh3_ on product0_.product_id=relationsh3_.product_id inner join node node4_ on relationsh3_.node_id=node4_.node_id inner join relationship relationsh5_ on product0_.product_id=relationsh5_.product_id inner join node node6_ on relationsh5_.node_id=node6_.node_id inner join relationship relationsh7_ on product0_.product_id=relationsh7_.product_id inner join abstract_attribute attributes8_ on relationsh7_.relationship_id=attributes8_.relationship_id inner join relationship relationsh9_ on product0_.product_id=relationsh9_.product_id inner join abstract_attribute attributes10_ on relationsh9_.relationship_id=attributes10_.relationship_id, attribute_definition attributed11_ where attributes10_.attribute_definition_id=attributed11_.attribute_definition_id and attributed11_.name='newMedia' and attributes10_.discriminator='BOO' and attributes10_.string_value=true]; nested exception is org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying = boolean

Name and version of the database you are using: Postgres 8.1


Top
 Profile  
 
 Post subject: Figured it out.
PostPosted: Tue Jan 09, 2007 4:14 pm 
Newbie

Joined: Tue Jan 09, 2007 12:01 pm
Posts: 3
The answer is to use older "non-ANSI"-style syntax so that you can explicitly declare types in the table list and join through IDs:

select distinct p from Product p, Relationship r, BooleanAttribute ba where p.relationships.id=r.id
and r.attributes.id=ba.id and ba.value=TRUE

instead of...

select distinct p from Product p join p.relationships as r join r.attributes as a where a.class=BooleanAttribute and a.value=TRUE

...which doesn't work! I don't know if I'd chalk this up as a bug, but it would be nice if this were possible with the "ANSI-style" syntax.


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