IntelliJ Java IDE
The moose likes Object Relational Mapping and the fly likes Hibernate returns duplicate results Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "Hibernate returns duplicate results" Watch "Hibernate returns duplicate results" New topic
Author

Hibernate returns duplicate results

Wayne Blignaut
Greenhorn

Joined: Aug 14, 2006
Posts: 1
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

Joined: May 16, 2005
Posts: 504

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);


If you are not laughing at yourself, then you just didn't get the joke.
 
 
subject: Hibernate returns duplicate results
 
Threads others viewed
how to save collection object into database
Getting same record in query.
SQL-Query not returning specified Object
Hibernate Issue
Hibernate with native SQL issue
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture