hibernate学习笔记之 Lifecycle Entity operations

本文详细探讨了Hibernate中的实体操作,重点介绍了Save方法的使用,强调了在不确定数据库是否存在对象时应使用的方法。同时,文章还讨论了同一个Session中对象实例的特性,以及Load方法的各种模式,包括它们的锁机制和数据一致性保证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


这篇文章是对hibernate 中operations 的总结:

1. Save method 总共有三个 


//把object存到db, 返回存好的object的id

public Serializable save(Object object) throws HibernateException

//把object存到db里指定的id的那一行

public void save(Object object, Serializable id) throws HibernateException

//存储object到指定的entityName, entityName attribute 指的是mapping file中entity的logic name

public Serializable save(String entityName,Object object) throws HibernateException

第一个method用于database 自动生成row id.

而第二个method可以用来override database自动生成的row id.


save 只应该在确定database里没有存在的想要存储的object的情况下用, 如果在不确定db里是不是存在将要存的data row, save不是一个很好的选择, 

在这种情况下, 应该用下边的method

public void saveOrUpdate(Object object) throws HibernateException


2. 在hibernate中, 同一个session中的object 的instance是不变的。所以比较的时候可以用"=", 但是如果要比较不同session里的object, 那么那个object 必须implement equal method, 同时用equals进行比较


3. Load methods

//load specified class type mapped in database with the specified row id 
public Object load(Class theClass, Serializable id) throws HibernateException

//load the object which having the "entityName" in mapping file in database row with the speicifed row id
public Object load(String entityName, Serializable id) throws HibernateException

//load the object which have the same type of input object in database row with the speicifed row id 
public void load(Object object, Serializable id) throws HibernateException

特别注意最后一个method, 里边的object argument必须是空的, 其他的load methods 就是以这三个为基础, 在后边多加了一个argument来定义lockMode, 一般情况下不需要定义LockMode, 如果使用不定义LockMode, Hibernate则会为你自动选择。但如果实在需要的话,下边是可以选择的lock mode:
  • NONE: Uses no row-level locking, and uses a cached object if available; this is the Hibernate default.
  • READ: Prevents other SELECT queries from reading data that is in the middle of a transaction (and thus possibly invalid) until it is committed.
  • UPGRADE: Uses the SELECT FOR UPDATE SQL syntax to lock the data until the transaction is finished.
  • UPGRADE_NOWAIT: Uses the NOWAIT keyword (for Oracle), which returns an error immediately if there is another thread using that row. Otherwise this is similar to UPGRADE.
所有的LockMode 都源自org.hibernate.LockMode Class的static field

在大部分情况下, Load Method 不是一个很好的选择, 因为如果用了load却没有在DB里找到相应的object, Hibernate就会throw HibernateException, 所以大部分情况下应该用get() 来代替load(), 因为get()在没有在database里找到object的时候只会返还一个null值 。

4. refresh methods
Java 提供两个refresh methods 给用户更新objects里的数据, call refresh method的时候, object里的数据会从
新从数据库中load一次。
public void refresh(Object object)
             throws HibernateException

public void refresh(Object object, LockMode lockMode)
             throws HibernateException

5. Update
Hibernate中有两个概念 Dirty 和 Flush,
Flush是指session的object里有内容和db不match的时候, 用session.flush() method 会强制hibernate 执行SQL query把数据写到db里, 值得注意的是这个时候database仅仅是执行了sql,但还没有commit, 直到call session.commit()才会真正把数据commit到database里

Dirty是指现在session的object里还有没有flush到Database的数据。

除了手动call flush() 方法以外, hibernate 里有个FlushMode, 这个mode定义hibernate在什么时候会执行flush.
程序员可以通过public void setFlushMode(FlushMode flushMode) 方法来设置FlushMode, 而通过public FlushMode getFlushMode()方法来查询当前的flushMode.
下边是可以选择的mode
  • ALWAYS: Every query flushes the session before the query is executed.
  • AUTO: Hibernate manages the query flushing to guarantee that the data returned by a query is up-to-date.
  • COMMIT: Hibernate flushes the session on transaction commits.
  • NEVER: Your application needs to manage the session flushing with the flush() method. Hibernate never flushes the session itself.

defualt mode是 AUTO


6.Delete
public void delete(Object object) throws HibernateException

这个方法可以用来删除database里的数据,在没有association的场合,这个方法很简单就可以使用,但是如果有association的时候,就必须考虑配置好cascading.以保证正确的删除。







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值