hibernate4下一个可用的hibernate.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/hibernate?characterEncoding=UTF-8 jdbc.username=username jdbc.password=password hibernate.dialect=org.hibernate.dialect.MySQLDialect <!-- Enable Hibernate‘s automatic session context management --> #hibernate.current_session_context_class=thread hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext <!-- start cache query --> hibernate.cache.use_query_cache=true hibernate.jdbc.batch_size=50 <!-- set second level --> 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 // hibernate3 hibernate.cache.provider_class=org.hibernate.cache.spi.RegionFactory hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory <!-- Drop and re-create the database schema on startup --> hibernate.hbm2ddl.auto=update <!-- Echo all executed SQL to stdout --> hibernate.show_sql=true
pom.xml中相关配置:
<properties> <hibernate-version>4.3.9.Final</hibernate-version> <spring-version>4.1.6.RELEASE</spring-version> <mysql-driver-version>5.1.35</mysql-driver-version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
hibernate.properpties中关于二级缓存的配置:
hibernate.cache.use_second_level_cache=true
hibernate.cache.provider_class=org.hibernate.cache.spi.RegionFactory
hibernate.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 more
Caused 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
添加:
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>${hibernate-version}</version> </dependency>
org.hibernate.HibernateException: save is not valid without active transaction
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
at com.sun.proxy.$Proxy24.save(Unknown Source)
at com.xx.dao.BaseDao.save(BaseDao.java:31)
对于提示*** 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