hibernate缓存问题

我根据此网站配置了二级缓存 http://www.cnblogs.com/iloveu/archive/2009/02/16/1391392.html
现在报出
[code="java"]1.%%%% Error Creating SessionFactory %%%%

2.org.hibernate.MappingException: invalid configuration

3. at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2241)

4. at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:230)

5. at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:71)

6. at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)

7. at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:212)

8. at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:71)

9. at com.huawei.ifree.autotest.log.analyse.platform.db.HibernateSessionFactory.updateHibernateCfg(HibernateSessionFactory.java:62)

10. at com.huawei.ifree.autotest.log.analyse.platform.db.HibernateSessionFactory.(HibernateSessionFactory.java:44)

11. at com.huawei.ifree.autotest.log.analyse.platform.servlet.ActivatorServlet.init(ActivatorServlet.java:53)

12. at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173)

13. at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)

14. at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4187)

15. at org.apache.catalina.core.StandardContext.start(StandardContext.java:4496)

16. at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)

17. at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)

18. at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)

19. at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)

20. at org.apache.catalina.core.StandardService.start(StandardService.java:519)

21. at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)

22. at org.apache.catalina.startup.Catalina.start(Catalina.java:581)

23. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

24. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

25. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

26. at java.lang.reflect.Method.invoke(Unknown Source)

27. at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)

28. at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)

29.Caused by: org.xml.sax.SAXParseException: The content of element type "session-factory" must match "(property*,mapping*,(class-cache|collection-cache)*,event*,listener*)".

30. at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:232)

31. at org.apache.xerces.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:173)

32. at org.apache.xerces.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:371)

33. at org.apache.xerces.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:305)

34. at org.apache.xerces.impl.dtd.XMLDTDValidator.handleEndElement(XMLDTDValidator.java:1918)

35. at org.apache.xerces.impl.dtd.XMLDTDValidator.endElement(XMLDTDValidator.java:851)

36. at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1008)

37. at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1469)

38. at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:329)

39. at org.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguration.java:525)

40. at org.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguration.java:581)

41. at org.apache.xerces.parsers.XMLParser.parse(XMLParser.java:152)

42. at org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1175)

43. at org.dom4j.io.SAXReader.read(SAXReader.java:465)

44. at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2238)

45. ... 25 more

[/code]

10个回答

[quote]查询缓存是直接缓存已经执行的那一条sql语句吗? 如果下次碰到语句一样的, 就到缓存里面去找吗?[/quote]
对啊,查询条件都要一样。

你的配置文件写错了呀
The content of element type "session-factory" must match "(property*,mapping*,(class-cache|collection-cache)*,event*,listener*)".

估计是property,mapping, class-cache|collection-cache, event, listener其中一个或者几个没有写对吧

在配置文件中配置开启二级缓冲
二级缓存都需要实现org.hibernate.cache.CacheProvider接口,Hibernate已经实现了一些缓存,开发人员可以直接配置使用,同时要启用二级缓存,配置hibernate.cache.use_second_level_cache为true。

可选值:

org.hibernate.cache.HashtableCacheProvide
org.hibernate.cache.EhCacheProvider
org.hibernate.cache.OSCacheProvider
org.hibernate.cache.SwarmCacheProvider
org.hibernate.cache.TreeCacheProvider...

org.hibernate.cache.EhCacheProvider

true

看这里
[url]http://log-cd.iteye.com/blog/355097[/url]

如果要从缓存里取得话应该根据对象的ID来查
楼主第一次查出来的东西已经放到缓存里了,第二次查的时候应该想办法根据ID查。
这样就不会访问数据库了

你说的是一级缓存吧, 二级缓存不是所有session共享吗? 如果也是只能根据id查询才行, 那配置这个就用处不大了

不是这样的。你想啊 如果是个查询语句,比如将年龄大于20的人查出来这样的查询,怎么可能去缓存里面查呢! 数据时刻都在变,你在缓存里面查出来的结果很可能是不正确的。要想从缓存里面取东西只有根据ID查出来的才有意义。

一般放到缓存里面的东西都是很少变化的东西。

至于缓存有啥好处等你到项目里面用了就知道了

[quote]
the second level cache will [b]only cache relations and entities you find on id[/b]. Queries can't be cached by the second level cache and will always go to the database (or query cache).
[/quote]

[quote]
2nd level cache contains only entities by their ids, so when retrieving an entity by id (i.e. get, load or resolving a proxy implicitly) a 2nd level cache may be accessed. Any other queries (hal, criteria) will bypass the cache and hit the DB - at least as long as no query cache is used as well.
[/quote]
二级缓存只对根据实体的ID来缓存实体,所以只有在通过ID获取实体时才有可能访问二级缓存,比如,get,load方法。所有其他查询都会跳过缓存直接命中数据库,至少在没有使用查询缓存时是这样的。

你可以试试 我觉得查询语句不太可能去缓存里面找呀。这样效率高但是结果不对呀

查询语句缓存的话 oracla也是缓存的。没有必要用它的。

oracle

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问