Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate returns duplicate results

 
Wayne Blignaut
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am experiencing a problem with Hibernate whereby a call to the DAO method I have inserted below returns duplicate results. The query should return five unique results, however, the returned list contains five references to the same object. We are currently using Hibernate 3.0.5. The same error occurs when I do a HQL query. Does anyone perhaps know what I am doing wrong? Any help would be appreciated!

I have inserted a copy of the Hibernate mapping file we use below the method.

public List findAfterDate(Date d, String[] batchNumbers){
Session session = HibernateUtil.currentSession();

try {
Criteria criteria = session.createCriteria(EctBatchHistory.class);
criteria.add(Expression.ge("timedate", d));
criteria.add(Expression.ne("description", "Batch(es) successfully created."));
criteria.add(Restrictions.in("batch", batchNumbers));
criteria.addOrder(Order.asc("timedate"));
List results = criteria.list();
return results;
} catch(Exception e){
e.printStackTrace();
return new ArrayList();
}
finally {
HibernateUtil.closeSession();
}
}


<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.X" table="hist" schema="dbo" lazy="false">
<id name="timedate" column="timedate" type="date" >
<generator class="assigned"/>
</id>
<property name="resellerId" column="reseller_id" type="integer" />
<property name="description" column="description" type="string" />
<property name="batch" column="batch_id" type="string" />
</class>
</hibernate-mapping>

Thanks
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add this to your criteria,

Criteria criteria = session.createCriteria(EctBatchHistory.class);
criteria.add(Expression.ge("timedate", d));
criteria.add(Expression.ne("description", "Batch(es) successfully created."));
criteria.add(Restrictions.in("batch", batchNumbers));
criteria.addOrder(Order.asc("timedate"))
.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic