is it possible to mix a session.load(class, id) with a flexible criteria (criteria api)?
I need something like :
select * from entity where id = 1 AND name = 'miller'
I use the generic approach for the find by id:
public T findById(ID id, boolean lock) {
T entity;
if (lock)
entity = (T) getSession().load(getPersistentClass(), id, LockMode.UPGRADE);
else
entity = (T) getSession().load(getPersistentClass(), id);
return entity;
}
and would like to extend this by
Criteria criteria = session.createCriteria(getPersistentClass());
criteria.add( Restrictions.eq("name", "miller"));
where ("name", "miller") will be replaced by something generic.
Load and criteria can’t be mixed. Load creates a proxy if the instance isn’t in cache yet. The purpose of load (and get) is that its arguments are the keys for the cache (class and id) and it doesn’t need to flush the session before. Criteria is a kind of query. When you already know the id, you don’t need a query anymore. I mean, does
id = 1 AND name = 'miller'make any sense?Just write you own “Get”:
(Sorry for possible syntactic errors, I’m not a java programmer)