Hibernate主键生成策略与save()方法是否发sql语句的研究
生活随笔
收集整理的這篇文章主要介紹了
Hibernate主键生成策略与save()方法是否发sql语句的研究
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
public class SessionFlushTest extends TestCase { ?
???? ?
??? /**
??? * 測試uuid主鍵生成策略
??? */ ?
??? public void testSave1() { ?
?????? Session session = null; ?
?????? Transaction tx = null; ?
?????? try { ?
??????? session = HibernateUtils.getSession(); ?
??????? tx = session.beginTransaction(); ?
???? ?
??????? User1 user = new User1(); ?
??????? user.setName("李四"); ?
??????? user.setPassword("123"); ?
??????? user.setCreateTime(new Date()); ?
??????? user.setExpireTime(new Date()); ?
??????? ?
??????? //因為user的主鍵生成側路采用的是uuid,所以調用完成save后,只是將user納入到了session的管理 ?
??????? //不會發出insert語句,但是id已經生成,session中existsInDatebase狀態為false ?
??????? session.save(user); ?
??????? ?
??????? //調用flush,hibernate會清理緩存,執行sql ?
??????? //如果數據庫的隔離級別設置為為提交讀,那么我們可以看到flush過的數據 ?
??????? //并且session中existsInDatebase狀態為true ?
??????? session.flush(); ?
??????? ?
??????? //提交事務 ?
??????? //默認情況下commit操作會先執行flush清理緩存,所以不用顯示的調用flush ?
??????? //commit后數據是無法回滾的 ?
??????? tx.commit(); ?
?????? }catch(Exception e) { ?
??????? e.printStackTrace(); ?
??????? tx.rollback(); ?
?????? }finally { ?
??????? HibernateUtils.closeSession(session); ?
?????? } ?
??? } ?
???? ?
??? /**
??? * 測試native主鍵生成策略
??? */ ?
??? public void testSave2() { ?
?????? Session session = null; ?
?????? Transaction tx = null; ?
?????? try { ?
??????? session = HibernateUtils.getSession(); ?
??????? tx = session.beginTransaction(); ?
???? ?
??????? User2 user = new User2(); ?
??????? user.setName("張三1"); ?
??????? user.setPassword("123"); ?
??????? user.setCreateTime(new Date()); ?
??????? user.setExpireTime(new Date()); ?
??????? ?
??????? //因為user的主鍵生成策略為native,所以調用session.save后,將執行insert語句,返回有數據庫生成的id ?
??????? //納入了session的管理,修改了session中existsInDatebase狀態為true ?
??????? //如果數據庫的隔離級別設置為為提交讀,那么我們可以看到save過的數據 ?
??????? session.save(user); ?
??????? tx.commit(); ?
?????? }catch(Exception e) { ?
??????? e.printStackTrace(); ?
??????? tx.rollback(); ?
?????? }finally { ?
??????? HibernateUtils.closeSession(session); ?
?????? } ?
??? }?
???? ?
??? /**
??? * 測試uuid主鍵生成策略
??? */ ?
??? public void testSave1() { ?
?????? Session session = null; ?
?????? Transaction tx = null; ?
?????? try { ?
??????? session = HibernateUtils.getSession(); ?
??????? tx = session.beginTransaction(); ?
???? ?
??????? User1 user = new User1(); ?
??????? user.setName("李四"); ?
??????? user.setPassword("123"); ?
??????? user.setCreateTime(new Date()); ?
??????? user.setExpireTime(new Date()); ?
??????? ?
??????? //因為user的主鍵生成側路采用的是uuid,所以調用完成save后,只是將user納入到了session的管理 ?
??????? //不會發出insert語句,但是id已經生成,session中existsInDatebase狀態為false ?
??????? session.save(user); ?
??????? ?
??????? //調用flush,hibernate會清理緩存,執行sql ?
??????? //如果數據庫的隔離級別設置為為提交讀,那么我們可以看到flush過的數據 ?
??????? //并且session中existsInDatebase狀態為true ?
??????? session.flush(); ?
??????? ?
??????? //提交事務 ?
??????? //默認情況下commit操作會先執行flush清理緩存,所以不用顯示的調用flush ?
??????? //commit后數據是無法回滾的 ?
??????? tx.commit(); ?
?????? }catch(Exception e) { ?
??????? e.printStackTrace(); ?
??????? tx.rollback(); ?
?????? }finally { ?
??????? HibernateUtils.closeSession(session); ?
?????? } ?
??? } ?
???? ?
??? /**
??? * 測試native主鍵生成策略
??? */ ?
??? public void testSave2() { ?
?????? Session session = null; ?
?????? Transaction tx = null; ?
?????? try { ?
??????? session = HibernateUtils.getSession(); ?
??????? tx = session.beginTransaction(); ?
???? ?
??????? User2 user = new User2(); ?
??????? user.setName("張三1"); ?
??????? user.setPassword("123"); ?
??????? user.setCreateTime(new Date()); ?
??????? user.setExpireTime(new Date()); ?
??????? ?
??????? //因為user的主鍵生成策略為native,所以調用session.save后,將執行insert語句,返回有數據庫生成的id ?
??????? //納入了session的管理,修改了session中existsInDatebase狀態為true ?
??????? //如果數據庫的隔離級別設置為為提交讀,那么我們可以看到save過的數據 ?
??????? session.save(user); ?
??????? tx.commit(); ?
?????? }catch(Exception e) { ?
??????? e.printStackTrace(); ?
??????? tx.rollback(); ?
?????? }finally { ?
??????? HibernateUtils.closeSession(session); ?
?????? } ?
??? }?
轉載于:https://www.cnblogs.com/hainange/archive/2012/03/16/6153653.html
總結
以上是生活随笔為你收集整理的Hibernate主键生成策略与save()方法是否发sql语句的研究的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 虚拟化部署的四大安全问题
- 下一篇: Apache Nutch 1.3 学习笔