hibernate4下一个可用的hibernate.properties:
jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/hibernate?characterEncoding=UTF-8jdbc.username=usernamejdbc.password=passwordhibernate.dialect=org.hibernate.dialect.MySQLDialect #hibernate.current_session_context_class=threadhibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext hibernate.cache.use_query_cache=truehibernate.jdbc.batch_size=50 hibernate.cache.use_second_level_cache=true#hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory#hibernate.cache.provider_class=org.hibernate.cache.ehcache.EhCacheRegionFactory // hibernate3hibernate.cache.provider_class=org.hibernate.cache.spi.RegionFactoryhibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory hibernate.hbm2ddl.auto=update hibernate.show_sql=true
pom.xml中相关配置:
4.3.9.Final 4.1.6.RELEASE 5.1.35 UTF-8
hibernate.properpties中关于二级缓存的配置:
hibernate.cache.use_second_level_cache=truehibernate.cache.provider_class=org.hibernate.cache.spi.RegionFactoryhibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory报错信息:Caused by: org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.ehcache.EhCacheRegionFactory] at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:101) at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:46) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:105) at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:251) ... 80 moreCaused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.cache.ehcache.EhCacheRegionFactory] as strategy [org.hibernate.cache.spi.RegionFactory] at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:128) at org.hibernate.cache.internal.RegionFactoryInitiator.initiateService(RegionFactoryInitiator.java:87) ... 83 more原因及解决办法:
缺少jar:hibernate-ehcache添加:org.hibernate hibernate-ehcache ${hibernate-version}
对于提示*** is not valid without active transaction 的错误
可以在Hibernate的配置文件中做如下修改<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>(Hibernate4)对于Hibernate3.x,可以直接把上述设置删除,就不会报错了,具体原因尚不清楚。须知:几乎所有正常的操作都必须在transcation.isActive()条件下才能执行。get,load,save, saveOrUpdate,list都属于这类。http://blog.csdn.net/wzk527/article/details/8543480
日志打印:
增加slf4j的jar,譬如log4j可选择添加slf4j-api.jar和slf4j-log4j12.jar