waterye

Session.evict(Object object), Session.setReadOnly(Object entity, boolean readOnly)

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 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
Session s = openSession();
Transaction t 
= s.beginTransaction();

Customer c 
= (Customer) s.get(Customer.classnew 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 Case
Java code
Session s = openSession();
Transaction t 
= s.beginTransaction();

Customer c 
= (Customer) s.get(Customer.classnew 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

posted on 2006-01-18 11:49 waterye 阅读(2058) 评论(0)  编辑  收藏 所属分类: hibernate


只有注册用户登录后才能发表评论。


网站导航: