-->
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: DetachedCriteria query returns correct count, but empty list
PostPosted: Tue Jul 15, 2008 9:16 am 
Newbie

Joined: Tue Jul 15, 2008 8:42 am
Posts: 2
Hibernate version:
3.2.6ga

Mapping documents:
...
<class name="com.ext.portal.inventory.model.impl.ProductImpl" table="Product">
<cache usage="read-write" />
<id name="productId" type="long">
<generator class="assigned" />
</id>
<property name="companyId" type="com.liferay.util.dao.hibernate.LongType" />
<property name="groupId" type="com.liferay.util.dao.hibernate.LongType" />
<property name="userId" type="com.liferay.util.dao.hibernate.LongType" />
<property name="createDate" />
<property name="modifiedDate" />
<property name="name" type="com.liferay.util.dao.hibernate.StringType" />
<property name="description" type="com.liferay.util.dao.hibernate.StringType" />
<property name="imageId" type="com.liferay.util.dao.hibernate.LongType" />
<property name="url" type="com.liferay.util.dao.hibernate.StringType" />
<property name="properties" type="com.liferay.util.dao.hibernate.StringType" />
<many-to-one name="type" class="com.ext.portal.inventory.model.impl.ProductTypeImpl" column="typeId" not-null="true" not-found="ignore" />
<many-to-one name="model" class="com.ext.portal.inventory.model.impl.ModelImpl" column="modelId" not-null="true" not-found="ignore" />
<property name="sku" type="com.liferay.util.dao.hibernate.StringType" />
<property name="discontinued" type="com.liferay.util.dao.hibernate.BooleanType" />
<set name="costs" inverse="true">
<key column="productId" not-null="true" />
<one-to-many class="com.ext.portal.inventory.model.impl.CostImpl" />
</set>
<set name="prices" inverse="true">
<key column="productId" not-null="true" />
<one-to-many class="com.ext.portal.inventory.model.impl.PriceImpl" />
</set>
<set name="features" table="Features_Products" inverse="true">
<key column="productId" />
<many-to-many column="featureId" class="com.ext.portal.inventory.model.impl.FeatureImpl" />
</set>
<set name="offers" table="Offers_Products" inverse="true">
<key column="productId" />
<many-to-many column="offerId" class="com.ext.portal.inventory.model.impl.OfferImpl" />
</set>
</class>
...

Code between sessionFactory.openSession() and session.close():
For list
DetachedCriteria dc = DetachedCriteria.forClass(com.ext.portal.inventory.model.impl.ProductImpl.class, "Product");
Criteria c = dc.getExecutableCriteria(session));
c.list();

For count
DetachedCriteria dc = DetachedCriteria.forClass(com.ext.portal.inventory.model.impl.ProductImpl.class, "Product");
dc.setProjection(Projections.rowCount());
Criteria c = dc.getExecutableCriteria(session));
Iterator itr = c.list().iterator();
if (resultsItr.hasNext())
return ((Number) resultsItr.next()).intValue();

Name and version of the database you are using:
MySQL (latest)

The generated SQL (show_sql=true):
For list
Hibernate: select this_.productId as productId111_4_, this_.companyId as companyId111_4_, this_.groupId as groupId111_4_, this_.userId as userId111_4_, this_.createDate as createDate111_4_, this_.modifiedDate as modified6_111_4_, this_.name as name111_4_, this_.description as descript8_111_4_, this_.imageId as imageId111_4_, this_.url as url111_4_, this_.properties as properties111_4_, this_.typeId as typeId111_4_, this_.modelId as modelId111_4_, this_.sku as sku111_4_, this_.discontinued as discont15_111_4_, producttyp2_.typeId as typeId114_0_, producttyp2_.companyId as companyId114_0_, producttyp2_.groupId as groupId114_0_, producttyp2_.userId as userId114_0_, producttyp2_.createDate as createDate114_0_, producttyp2_.modifiedDate as modified6_114_0_, producttyp2_.name as name114_0_, producttyp2_.description as descript8_114_0_, producttyp2_.imageId as imageId114_0_, producttyp2_.url as url114_0_, producttyp2_.properties as properties114_0_, producttyp2_.parentTypeId as parentT12_114_0_, producttyp3_.typeId as typeId114_1_, producttyp3_.companyId as companyId114_1_, producttyp3_.groupId as groupId114_1_, producttyp3_.userId as userId114_1_, producttyp3_.createDate as createDate114_1_, producttyp3_.modifiedDate as modified6_114_1_, producttyp3_.name as name114_1_, producttyp3_.description as descript8_114_1_, producttyp3_.imageId as imageId114_1_, producttyp3_.url as url114_1_, producttyp3_.properties as properties114_1_, producttyp3_.parentTypeId as parentT12_114_1_, modelimpl4_.modelId as modelId110_2_, modelimpl4_.companyId as companyId110_2_, modelimpl4_.groupId as groupId110_2_, modelimpl4_.userId as userId110_2_, modelimpl4_.createDate as createDate110_2_, modelimpl4_.modifiedDate as modified6_110_2_, modelimpl4_.name as name110_2_, modelimpl4_.description as descript8_110_2_, modelimpl4_.imageId as imageId110_2_, modelimpl4_.url as url110_2_, modelimpl4_.properties as properties110_2_, modelimpl4_.makeId as makeId110_2_, modelimpl4_.discontinued as discont13_110_2_, makeimpl5_.makeId as makeId109_3_, makeimpl5_.companyId as companyId109_3_, makeimpl5_.groupId as groupId109_3_, makeimpl5_.userId as userId109_3_, makeimpl5_.createDate as createDate109_3_, makeimpl5_.modifiedDate as modified6_109_3_, makeimpl5_.name as name109_3_, makeimpl5_.description as descript8_109_3_, makeimpl5_.imageId as imageId109_3_, makeimpl5_.url as url109_3_, makeimpl5_.properties as properties109_3_, makeimpl5_.manufacturerId as manufac12_109_3_ from Product this_ inner join ProductType producttyp2_ on this_.typeId=producttyp2_.typeId left outer join ProductType producttyp3_ on producttyp2_.parentTypeId=producttyp3_.typeId inner join Model modelimpl4_ on this_.modelId=modelimpl4_.modelId left outer join Make makeimpl5_ on modelimpl4_.makeId=makeimpl5_.makeId where ((((((this_.companyId=? and 1) and 1) and 1) and 1) and 1) and 1) order by this_.name asc limit ?

For count
Hibernate: select count(*) as y0_ from Product this_ where ((((((this_.companyId=? and 1) and 1) and 1) and 1) and 1) and 1) order by this_.name asc

Debug level Hibernate log excerpt:
For list
15:09:19,823 DEBUG [SessionImpl:220] opened session at timestamp: 4981257665835008
15:09:19,855 DEBUG [AbstractBatcher:366] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
15:09:19,858 DEBUG [ConnectionManager:421] opening JDBC connection
15:09:19,860 DEBUG [SQL:401] select this_.productId as productId111_4_, this_.companyId as companyId111_4_, this_.groupId as groupId111_4_, this_.userId as userId111_4_, this_.createDate as createDate111_4_, this_.modifiedDate as modified6_111_4_, this_.name as name111_4_, this_.description as descript8_111_4_, this_.imageId as imageId111_4_, this_.url as url111_4_, this_.properties as properties111_4_, this_.typeId as typeId111_4_, this_.modelId as modelId111_4_, this_.sku as sku111_4_, this_.discontinued as discont15_111_4_, producttyp2_.typeId as typeId114_0_, producttyp2_.companyId as companyId114_0_, producttyp2_.groupId as groupId114_0_, producttyp2_.userId as userId114_0_, producttyp2_.createDate as createDate114_0_, producttyp2_.modifiedDate as modified6_114_0_, producttyp2_.name as name114_0_, producttyp2_.description as descript8_114_0_, producttyp2_.imageId as imageId114_0_, producttyp2_.url as url114_0_, producttyp2_.properties as properties114_0_, producttyp2_.parentTypeId as parentT12_114_0_, producttyp3_.typeId as typeId114_1_, producttyp3_.companyId as companyId114_1_, producttyp3_.groupId as groupId114_1_, producttyp3_.userId as userId114_1_, producttyp3_.createDate as createDate114_1_, producttyp3_.modifiedDate as modified6_114_1_, producttyp3_.name as name114_1_, producttyp3_.description as descript8_114_1_, producttyp3_.imageId as imageId114_1_, producttyp3_.url as url114_1_, producttyp3_.properties as properties114_1_, producttyp3_.parentTypeId as parentT12_114_1_, modelimpl4_.modelId as modelId110_2_, modelimpl4_.companyId as companyId110_2_, modelimpl4_.groupId as groupId110_2_, modelimpl4_.userId as userId110_2_, modelimpl4_.createDate as createDate110_2_, modelimpl4_.modifiedDate as modified6_110_2_, modelimpl4_.name as name110_2_, modelimpl4_.description as descript8_110_2_, modelimpl4_.imageId as imageId110_2_, modelimpl4_.url as url110_2_, modelimpl4_.properties as properties110_2_, modelimpl4_.makeId as makeId110_2_, modelimpl4_.discontinued as discont13_110_2_, makeimpl5_.makeId as makeId109_3_, makeimpl5_.companyId as companyId109_3_, makeimpl5_.groupId as groupId109_3_, makeimpl5_.userId as userId109_3_, makeimpl5_.createDate as createDate109_3_, makeimpl5_.modifiedDate as modified6_109_3_, makeimpl5_.name as name109_3_, makeimpl5_.description as descript8_109_3_, makeimpl5_.imageId as imageId109_3_, makeimpl5_.url as url109_3_, makeimpl5_.properties as properties109_3_, makeimpl5_.manufacturerId as manufac12_109_3_ from Product this_ inner join ProductType producttyp2_ on this_.typeId=producttyp2_.typeId left outer join ProductType producttyp3_ on producttyp2_.parentTypeId=producttyp3_.typeId inner join Model modelimpl4_ on this_.modelId=modelimpl4_.modelId left outer join Make makeimpl5_ on modelimpl4_.makeId=makeimpl5_.makeId where ((((((this_.companyId=? and 1) and 1) and 1) and 1) and 1) and 1) order by this_.name asc limit ?
15:09:19,870 DEBUG [AbstractBatcher:382] about to open ResultSet (open ResultSets: 0, globally: 0)
15:09:19,871 DEBUG [AbstractBatcher:389] about to close ResultSet (open ResultSets: 1, globally: 1)
15:09:19,872 DEBUG [AbstractBatcher:374] about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
15:09:19,874 DEBUG [StatefulPersistenceContext:837] initializing non-lazy collections
15:09:19,875 DEBUG [ConnectionManager:302] transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
15:09:19,878 DEBUG [ConnectionManager:441] releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
15:09:19,883 DEBUG [ConnectionManager:302] transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!

For count
15:09:19,898 DEBUG [SessionImpl:220] opened session at timestamp: 4981257666142208
15:09:19,901 DEBUG [AbstractBatcher:366] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
15:09:19,903 DEBUG [ConnectionManager:421] opening JDBC connection
15:09:19,906 DEBUG [SQL:401] select count(*) as y0_ from Product this_ where ((((((this_.companyId=? and 1) and 1) and 1) and 1) and 1) and 1) order by this_.name asc
15:09:19,908 DEBUG [AbstractBatcher:382] about to open ResultSet (open ResultSets: 0, globally: 0)
15:09:19,910 DEBUG [Loader:1173] result row:
15:09:19,911 DEBUG [AbstractBatcher:389] about to close ResultSet (open ResultSets: 1, globally: 1)
15:09:19,912 DEBUG [AbstractBatcher:374] about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
15:09:19,917 DEBUG [StatefulPersistenceContext:837] initializing non-lazy collections
15:09:19,920 DEBUG [ConnectionManager:302] transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
15:09:19,922 DEBUG [ConnectionManager:441] releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
15:09:19,923 DEBUG [ConnectionManager:302] transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!

Results:
When I run the count query (same query but with a count projection), then I get the result "1" back, indicating that the query found 1 row in the db (which is correct in this case).
However, when I run the query as a normal list() get, then it returns an empty List.
Could it have something to do with the "not-null" attribute in the "many-to-one" tags of the Product mapping? If, for instance "typeId" equals 0 (no type), will this cause the query to fail for this row?

Thanks in advance.


Top
 Profile  
 
 Post subject: [Solved] I figured it out
PostPosted: Tue Jul 15, 2008 9:38 am 
Newbie

Joined: Tue Jul 15, 2008 8:42 am
Posts: 2
Right after I typed the post, I tried removing the not-null="true" attributes of the many-to-one tags. This solved my problem.
:)


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.