欢迎来到天天文库
浏览记录
ID:32020104
大小:44.47 KB
页数:42页
时间:2019-01-30
《Hibernate二级缓存.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、HibernateTest.java代码:/** * */packagecom.b510.examplex;importorg.hibernate.Session;importorg.hibernate.SessionFactory;importorg.hibernate.Transaction;/** * *@authorXHW * *@date2011-7-15 * */publicclassHibernateTest{ /** *@paramargs */ publicstaticvoidmain(S
2、tring[]args){ newHibernateTest().getGuestbooks(); } publicvoidgetGuestbooks(){ SessionFactorysessionFactory=HibernateSessionFactoryUtil.getSessionFactory(); Sessionsession1=sessionFactory.getCurrentSession(); System.out.println("Session1:"+session1);
3、Transactiontx=session1.beginTransaction();//Guestbookgb=(Guestbook)session1.load(Guestbook.class,1); Guestbookgb=(Guestbook)session1.get(Guestbook.class,1); System.out.println("ID:"+gb.getId()+"name: "+gb.getName()); tx.commit(); System.out.println("
4、----------------------------------"); Sessionsession2=sessionFactory.getCurrentSession(); System.out.println("Session2:"+session2); System.out.println("session1是否等于session2 ?"); System.out.println(session1==session2); session2.beginTransaction();//
5、 gb=(Guestbook)session2.load(Guestbook.class,1); load和get方法在这里可以随便使用,不会影响运行的结果 gb=(Guestbook)session2.get(Guestbook.class,1); System.out.println("ID:"+gb.getId()+"name: "+gb.getName()); session2.getTransaction().commit(); } }运行效果:log4j:WARNNoappend
6、erscouldbefoundforlogger(org.hibernate.cfg.Environment).log4j:WARNPleaseinitializethelog4jsystemproperly.Session1:SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=[]updates=[]deletions=[]collectionCreations=[]collectio
7、nRemovals=[]collectionUpdates=[]])Hibernate: select guestbook0_.idasid0_0_, guestbook0_.versionasversion0_0_, guestbook0_.nameasname0_0_, guestbook0_.emailasemail0_0_, guestbook0_.phoneasphone0_0_, guestbook0_.titleast
8、itle0_0_, guestbook0_.contentascontent0_0_, guestbook0_.created_timeascreated8_0_0_ from users.guestbookguestbook0_ where guestbook0_.id=?ID:1name: liuwei--------------------------
此文档下载收益归作者所有