Hello,
There are two ways to update the datafield of an object A:
1. A = session.get(A.Class, aId);
A.setDataField(...)
session.saveOrUpdate(A)
2. session.createQuery("update A set field1=... where aId=..")
Method 1 generates 1 select and 1 update
Method 2 generates 1 update.
However, although Method 2 looks more efficient, it does not hadnle session cache or second level cache automatically. So you have to manually evic the cached instance.
For second level cache, you can do sessionfactory(A.class, aId);
However, I do not know how to evict an object from session (first level cache) without a select. There is a method: session.evict(Object obj), but you have to have the object first.
So in conclusion, I have two questions:
1. For method 2, is there a way to evict an object without first do a select? Something like, sessionFactory.evict(A.class, aId). Please note that the assumption is, you do not know wether this object is in session cache or not. If it does resides in session cache, you need to evict. If not, you don't have to do anything.
2. For method 1, if 2nd level cache is used, do we have to call sessionfactory(A.class, aId) or Hibernate will handle this for us automatically?
I read the cache chaper in both HIA and Reference but could not find answer.
Thanks,
Joe
|