这篇文章是对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 otherSELECT
queries from reading data that is in the middle of a transaction (and thus possibly invalid) until it is committed.UPGRADE
: Uses theSELECT FOR UPDATE
SQL syntax to lock the data until the transaction is finished.UPGRADE_NOWAIT
: Uses theNOWAIT
keyword (for Oracle), which returns an error immediately if there is another thread using that row. Otherwise this is similar toUPGRADE
.
在大部分情况下, 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 theflush()
method. Hibernate never flushes the session itself.
defualt mode是 AUTO
6.Delete
public void delete(Object object) throws HibernateException
这个方法可以用来删除database里的数据,在没有association的场合,这个方法很简单就可以使用,但是如果有association的时候,就必须考虑配置好cascading.以保证正确的删除。