用Maven创建第一个Hibernate工程

一、环境:

操作系统:windows 7旗舰版 64bit

IDE:eclipse Luna Release (4.4.0)

创建第一个HIbernate+Maven工程时,遇到如下异常:

hibernate-core: 3.5.4-Final

(1)hibernate.cfg.xml路径问题:

Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found

原因主要是HIbernate的默认配置文件hibernate.cfg.xml路径不可变。将其放到src/main/java/这个目录下即可,maven install会自动target/classes目录下面;如下图所示:

用Maven创建第一个Hibernate工程
 

(2)添加二级缓存时,类找不到问题:

Exception in thread "main" org.hibernate.HibernateException: could not instantiate RegionFactory [org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge]
	at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:402)
	at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:270)
	at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2163)
	at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2159)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1383)
	at com.zjh.practice.dal.hibernate.simpleDemo.cache.SecondSessionCache.main(SecondSessionCache.java:31)
Caused by: java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
	at org.hibernate.cfg.SettingsFactory.createRegionFactory(SettingsFactory.java:397)
	... 5 more
Caused by: org.hibernate.cache.CacheException: could not instantiate CacheProvider [org.hibernate.cache.EhCacheProvider]
	at org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge.<init>(RegionFactoryCacheProviderBridge.java:66)
	... 10 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.cache.EhCacheProvider
	at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:259)
	at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:192)
	at org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge.<init>(RegionFactoryCacheProviderBridge.java:63)
	... 10 more

 最后发现是hibernate-core-3.5.4-Final.jar里面已经不包括org.hibernate.cache.EhCacheProvider,需要加入hibernate-ehcache-3.5.4-Final.jar这个包的依赖。完整的pom.xml如下:

<dependencies>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>3.5.4-Final</version>
		</dependency>
		<dependency>
			<groupId>jboss</groupId>
			<artifactId>javassist</artifactId>
			<version>3.4.ga</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.7</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.31</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-ehcache</artifactId>
			<version>3.5.4-Final</version>
		</dependency>
	</dependencies>

 (3)

Exception in thread "main" org.hibernate.cache.CacheException: net.sf.ehcache.CacheException: com.zjh.practice.dal.hibernate.simpleDemo.entity.BoxCache: Could not create disk store. Initial cause was null
	at org.hibernate.cache.EhCacheProvider.buildCache(EhCacheProvider.java:101)
	at org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge.buildEntityRegion(RegionFactoryCacheProviderBridge.java:104)
	at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:291)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
	at com.zjh.practice.dal.hibernate.simpleDemo.cache.SecondSessionCache.main(SecondSessionCache.java:26)
Caused by: net.sf.ehcache.CacheException: com.zjh.practice.dal.hibernate.simpleDemo.entity.BoxCache: Could not create disk store. Initial cause was null
	at net.sf.ehcache.store.DiskStore.<init>(DiskStore.java:164)
	at net.sf.ehcache.Cache.createDiskStore(Cache.java:577)
	at net.sf.ehcache.Cache.initialise(Cache.java:549)
	at net.sf.ehcache.CacheManager.addCacheNoCheck(CacheManager.java:707)
	at net.sf.ehcache.CacheManager.addCache(CacheManager.java:697)
	at net.sf.ehcache.CacheManager.addCache(CacheManager.java:634)
	at org.hibernate.cache.EhCacheProvider.buildCache(EhCacheProvider.java:94)
	... 4 more
Caused by: java.lang.NullPointerException

 在ehcache.xml中增加如下配置:

<cache name="com.zjh.practice.dal.hibernate.simpleDemo.entity.Box" maxElementsInMemory="5000"
		eternal="true" overflowToDisk="false" />

相关推荐