Under some circumstance(not all the time) following methond throws NonUniqueObjectException
Code:
public FooObject getFooObject (Class clazz, long id, Session session) throws HibernateException{
Criteria criteria = session.createCriteria(clazz);
criteria.add(Expression.eq(“id”, new Long(id)));
return (Executor) criteria.uniqueResult();
}
while the following method never throws NonUniqueObjectException
Code:
public FooObject getFooObject(Class clazz, long id,Session session) throws HibernateException{
return (FooObject)session.get(clazz,new Long(id));
}
First question: what is the difference between querying using Criteria and loading using session.get()?
Second question: what should I do if I have to load object with composite identifier?
In following method I can not replace query by get.
Code:
public FooObject getFooObject (Class clazz, long id, Session session) throws HibernateException{
Criteria criteria = session.createCriteria(clazz);
criteria.add(Expression.eq(“id”, new Long(id)));
criteria.add(Expression.eq(“type”, new Long(id)));
return (Executor) criteria.uniqueResult();
}
NOTE: I'm using Hibernate 2.1.8
Is this problem appears in latest Hibernate version?