Hibernate version: 3.0.5
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-lazy="false">
<class name="com.test.Style" table="Style">
<id name="id" unsaved-value="null">
<generator class="native" />
</id>
<property name="description"/>
<many-to-one name="season" class="com.test.ListItem" cascade="none"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close(): Code:
List results = getSession().createQuery("select style from Style style where style.season.displayName like '%fall%' or style.description like '%10%' order by style.styleNumber").list();
System.out.println(results.size());
Full stack trace of any exception that occurs:No errors.
Name and version of the database you are using:MS SQL server 2000 sp4
The generated SQL (show_sql=true):Code:
select style0_.id as id, style0_.description as descript6_169_, from Style style0_, CollectiveListItems collective1_ where (collective1_.displayName like '%fall%' and style0_.season=collective1_.id)or(style0_.description like '%10%' )
I feel like quite the noob for this, but this is returning way too many results (for me, about 86000 rather than 2000). Adding a "distinct" to the hql query gets the right number, but it's horribly inefficient and takes nearly 10 seconds. From what I see, the expression " and style0_.season=collective1_.id" should be a condition of the join, so that the generated sql should look like this:
Code:
select style0_.id as id, style0_.description as descript6_169_, from Style style0_, CollectiveListItems collective1_ where style0_.season=collective1_.id and (collective1_.displayName like '%fall%' or style0_.description like '%10%' )
Even this would be equivalent and work just as well for me:
Code:
select style0_.id as id, style0_.description as descript6_169_, from Style style0_, CollectiveListItems collective1_ where (collective1_.displayName like '%fall%' and style0_.season=collective1_.id)or(style0_.description like '%10%' and style0_.season=collective1_.id)
That is a fast query and is what I want to do. I've been reading about joins, and have experimented with fetch="select|join" and haven't had any luck. What am I missing here?
The other option I can think of right now is to change the HQL query to something like this:
Code:
select style from Style style, ListItem listItem where style.season = listItem AND (listItem.displayName like '%fall%' or style.description like '%10%')
That works, and in an acceptable amount of time, but these queries are assembled at runtime through an advanced search type interface, so coming up with that HQL on the fly is a lot harder, but not impossible. Any ideas?
Thanks for looking.