Session.evict(Object object), Session.setReadOnly(Object entity, boolean readOnly)
public void evict(Object object) throws HibernateExceptionRemove 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 Case
hbm.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
<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>
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
Transaction t = s.beginTransaction();
Customer c = (Customer) s.get(Customer.class, new Long(1));
s.evict(c);
c.setName("IBM");
t.commit();
s.close();
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 Case
Java 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
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();
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
posted on 2006-01-18 11:49 waterye 閱讀(2068) 評論(0) 編輯 收藏 所屬分類: hibernate