hibernate4 二级缓存demo实例.docx_第1页
hibernate4 二级缓存demo实例.docx_第2页
hibernate4 二级缓存demo实例.docx_第3页
hibernate4 二级缓存demo实例.docx_第4页
hibernate4 二级缓存demo实例.docx_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

hibernate4 二级缓存demo实例hibernate使用版本是:hibernate-release-4.2.5.Final需要的jar包:hibernate-release-4.2.5.Finallibrequired下所有jar包ehcache jar包:hibernate-release-4.2.5.Finalliboptionalehcache下所有包junit:junit-4.10.jar和mysql-connector-java-5.1.15-bin.jar注:hibernate 4.2.5版本ehcache缓存不依赖commons-logging-1.1.1.jar,需要的是slf4j-api-1.6.1.jar项目结构如下hibernate.cfg.xmlXml代码1. 2. 5. 6. 7. 8. com.mysql.jdbc.Driver9. jdbc:mysql:/:3306/hibernate410. root11. root12. 13. 114. 15. org.hibernate.dialect.MySQL5Dialect16. 17. thread18. 19. !-ernal.NoCacheProvider20. -21. 22. true23. org.hibernate.cache.ehcache.EhCacheRegionFactory24. 25. !-org.hibernate.cache.EhCacheProvider-26. 27. true28. 29. 30. true31. 32. update33. 34. 35. 注意:hibernate4和hibernate3配置不一样,hibernate4是Xml代码1. org.hibernate.cache.ehcache.EhCacheRegionFactory而hibernate3的配置是Xml代码1. org.hibernate.cache.EhCacheProvider此处有一个疑问是:hibernate4的官方文档中,已经把class改了,但是属性名称没有改,还是vider_class,不是上面的hibernate.cache.region.factory_class,但是写成vider_class会报下面错误Java代码1. org.hibernate.service.spi.ServiceException:Unabletocreaterequestedserviceorg.hibernate.engine.spi.CacheImplementor2. ernal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:186)3. ernal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:150)4. ernal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)5. ernal.SessionFactoryImpl.(SessionFactoryImpl.java:264)6. atorg.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1790)7. atcom.test.pojo.UserTest.beforeClass(UserTest.java:28)8. atsun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethod)9. atsun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)10. atsun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)11. atjava.lang.reflect.Method.invoke(Method.java:597)12. atorg.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)13. ernal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)14. atorg.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)15. ernal.runners.statements.RunBefores.evaluate(RunBefores.java:27)16. atorg.junit.runners.ParentRunner.run(ParentRunner.java:300)17. ernal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)18. ernal.junit.runner.TestExecution.run(TestExecution.java:38)19. ernal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)20. ernal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)21. ernal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)22. ernal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)23. Causedby:org.hibernate.cache.NoCacheRegionFactoryAvailableException:Second-levelcacheisusedintheapplication,butpropertyhibernate.cache.region.factory_classisnotgiven,pleaseeitherdisablesecondlevelcacheorsetcorrectregionfactoryclassnametopropertyhibernate.cache.region.factory_class(andmakesurethesecondlevelcacheprovider,hibernate-infinispan,forexample,isavailableintheclasspath).24. ernal.NoCachingRegionFactory.buildTimestampsRegion(NoCachingRegionFactory.java:87)25. atorg.hibernate.cache.spi.UpdateTimestampsCache.(UpdateTimestampsCache.java:62)26. ernal.CacheImpl.(CacheImpl.java:72)27. atorg.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:40)28. atorg.hibernate.engine.spi.CacheInitiator.initiateService(CacheInitiator.java:35)29. ernal.SessionFactoryServiceRegistryImpl.initiateService(SessionFactoryServiceRegistryImpl.java:91)30. ernal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:176)31. .20more说是hibernate.cache.region.factory_class属性没有配置,估计官方文档里没有把属性改过来。ehcache.xmlXml代码1. 2. 5. 18. 20. 23. User实体类Java代码1. packagecom.test.pojo;2. 3. importjavax.persistence.Entity;4. importjavax.persistence.GeneratedValue;5. importjavax.persistence.GenerationType;6. importjavax.persistence.Id;7. 8. importorg.hibernate.annotations.Cache;9. importorg.hibernate.annotations.CacheConcurrencyStrategy;10. 11. Entity12. Cache(usage=CacheConcurrencyStrategy.READ_WRITE)13. publicclassUser14. Id15. GeneratedValue(strategy=GenerationType.IDENTITY)16. privateintid;17. privateStringname;18. privateintage;19. publicintgetId()20. returnid;21. 22. publicvoidsetId(intid)23. this.id=id;24. 25. publicStringgetName()26. returnname;27. 28. publicvoidsetName(Stringname)29. =name;30. 31. publicintgetAge()32. returnage;33. 34. publicvoidsetAge(intage)35. this.age=age;36. 37. 38. UserTest测试类:Java代码1. packagecom.test.pojo;2. importorg.hibernate.HibernateException;3. importorg.hibernate.Session;4. importorg.hibernate.SessionFactory;5. importorg.hibernate.cfg.Configuration;6. importorg.hibernate.service.ServiceRegistry;7. importorg.hibernate.service.ServiceRegistryBuilder;8. importorg.junit.BeforeClass;9. importorg.junit.Test;10. 11. publicclassUserTest12. 13. privatestaticSessionFactorysessionFactory=null;14. BeforeClass15. publicstaticvoidbeforeClass()16. Configurationconfiguration=newConfiguration();17. 18. 19. try20. configuration.configure();21. catch(HibernateExceptione)22. /TODOAuto-generatedcatchblock23. e.printStackTrace();24. 25. 26. 27. ServiceRegistryserviceRegistry=newServiceRegistryBuilder().applySettings(configuration.getProperties().buildServiceRegistry();28. sessionFactory=configuration.buildSessionFactory(serviceRegistry);29. 30. Test31. publicvoidtestEhcache()32. Sessionsession=sessionFactory.openSession();33. session.beginTransaction();34. Useru1=(User)session.load(User.class,3);35. System.out.println(u1.getName();36. session.getTransaction().commit();37. session.close();38. Sessionsession2=sessionFactory.openSession();39. 40. session2.beginTransaction();41. Useru2=(User)session2.load(User.class,3);42. System.out.println(u2.getName();43. session2.getTransaction().commit();44. session2.close();45. 46. 结果:Java代码1. Hibernate:selectuser0_.idasid1_0_0_,user0_.ageasage2_0_0_,user0_.nameasname3_0_0_fromUseruser0_whereuser0_.id=?2. zhangsan3. zhangsanlist二级缓存测试Java代码1. packagecom.test.pojo;2. importjava.util.List;3. 4. importorg.hibernate.HibernateException;5. importorg.hibernate.Session;6. importorg.hibernate.SessionFactory;7. importorg.hibernate.cfg.Configuration;8. importorg.hibernate.service.ServiceRegistry;9. importorg.hibernate.service.ServiceRegistryBuilder;10. importorg.junit.BeforeClass;11. importorg.junit.Test;12. 13. publicclassUserTest14. 15. privatestaticSessionFactorysessionFactory=null;16. BeforeClass17. publicstaticvoidbeforeClass()18. Configurationconfiguration=newConfiguration();19. 20. 21. try22. configuration.configure();23. catch(HibernateExceptione)24. /TODOAuto-generatedcatchblock25. e.printStackTrace();26. 27. 28. 29. ServiceRegistryserviceRegistry=newServiceRegistryBuilder().applySettings(configuration.getProperties().buildServiceRegistry();30. sessionFactory=configuration.buildSessionFactory(serviceRegistry);31. 32. SuppressWarnings(unchecked)33. Test34.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论