I am using the method
Code:
Session.createFilter(Object collection, String queryString)
as shown below where my queryString has "or" criterias.
The collection being filtered is obtained by engagement.getLanes() and I can see the "lane0_.engagement_id = ?" where criteria in the generated SQL. The problem is that the user provided query string in not grouped within paranthesis and as a result when "lane0_.engagement_id = ? " is appened by Hibernate to the user query string, the semantics are totally incorrect when the user query has "or" criterias. For intance in my case I would get "lanes" from engagements other than the the one expected.
Let me know if I'm missing something.
Thanks,
Sanjiv
Hibernate version:3.0.1 Code between sessionFactory.openSession() and session.close():Code:
Collection nullEntryLanes = session.createFilter(engagement.getLanes(),
"where this.laneId is null " +
"or this.serviceGroup is null or this.equipmentGroup is null " +
"or this.demand is null or this.demand <= 0 or this.distance is null").list();
Name and version of the database you are using: Oracle 9iCode:
The generated SQL (show_sql=true):
Hibernate: select lane0_.lane_id as lane1_, lane0_.name as name9_, lane0_.demand as demand9_, lane0_.distance as distance9_, lane0_.shipper_price as shipper5_9_, lane0_.shipper_price_unit as shipper6_9_, lane0_.stops as stops9_, lane0_.commodity as commodity9_, lane0_.comments as comments9_, lane0_.businessunit_id as busines10_9_, lane0_.servicegroup_id as service11_9_, lane0_.equipmentgroup_id as equipme12_9_, lane0_.engagement_id as engagement13_9_, lane0_.originlocation_id as originl14_9_, lane0_.destinationlocation_id as destina15_9_, lane0_.round_id as round16_9_, lane0_.closed as closed9_ from lane lane0_ where lane0_.engagement_id = ? and lane0_.name is null or lane0_.servicegroup_id is null or lane0_.equipmentgroup_id is null or lane0_.demand is null or lane0_.demand<=0 or lane0_.distance is null
The query generated should be :
Code:
select lane0_.lane_id as lane1_, lane0_.name as name9_, lane0_.demand as demand9_, lane0_.distance as distance9_, lane0_.shipper_price as shipper5_9_, lane0_.shipper_price_unit as shipper6_9_, lane0_.stops as stops9_, lane0_.commodity as commodity9_, lane0_.comments as comments9_, lane0_.businessunit_id as busines10_9_, lane0_.servicegroup_id as service11_9_, lane0_.equipmentgroup_id as equipme12_9_, lane0_.engagement_id as engagement13_9_, lane0_.originlocation_id as originl14_9_, lane0_.destinationlocation_id as destina15_9_, lane0_.round_id as round16_9_, lane0_.closed as closed9_ from lane lane0_ where lane0_.engagement_id = ? and (lane0_.name is null or lane0_.servicegroup_id is null or lane0_.equipmentgroup_id is null or lane0_.demand is null or lane0_.demand<=0 or lane0_.distance is null)
Mapping documents:Code:
<class name="foo.Engagement" table="engagement" lazy="false">
<id name="id" column="engagement_id" type="long" unsaved-value="null">
<generator class="sequence">
<param name="sequence">engagement_sequence</param>
</generator>
</id>
<property name="name" column="name" unique="true" />
<property name="message" column="message"/>
<set name="lanes" inverse="true" table="lane" lazy="true">
<key column="engagement_id"/>
<one-to-many class="foo.Lane"/>
</set>
...
...
</class>