public void
evict(Object object) throws HibernateException
Remove this instance from the session cache.
Changes to the instance will not be synchronized with the database.
This operation cascades to associated instances if the association is mapped with cascade="evict".
Test Casehbm.xml
<class name="Customer" table="CUSTOMER" dynamic-insert="true" dynamic-update="true">
<id name="id">
<generator class="hilo"/>
</id>
<property name="code" not-null="true" length="50" />
<property name="name" not-null="true" length="200" />
<property name="status" length="20" />
</class>
Java code
Session s = openSession();
Transaction t = s.beginTransaction();
Customer c = (Customer) s.get(Customer.class, new Long(1));
s.evict(c);
c.setName("IBM");
t.commit();
s.close();
show sql
Hibernate: select customer0_.id as id0_0_, customer0_.code as code0_0_, customer0_.name as name0_0_, customer0_.status as status0_0_ from CUSTOMER customer0_ where customer0_.id=?
public void
setReadOnly(Object entity, boolean readOnly)
Set an unmodified persistent object to read only mode, or a read only object to modifiable mode.
In read only mode, no snapshot is maintained and the instance is never dirty checked.
Test CaseJava code
Session s = openSession();
Transaction t = s.beginTransaction();
Customer c = (Customer) s.get(Customer.class, new Long(1));
s.setReadOnly(c, true);
c.setName("IBM");
t.commit();
s.close();
show sql
Hibernate: select customer0_.id as id0_0_, customer0_.code as code0_0_, customer0_.name as name0_0_, customer0_.status as status0_0_ from CUSTOMER customer0_ where customer0_.id=?
参考:
1. Hibernate Reference Documentation
2. Hibernate API Documentation
3. Hibernate Test Source