Spring3整合Hibernate4的问题,求指导

J2EE 码拜 9年前 (2016-03-17) 946次浏览
org.springframework.beans.factory.BeanCreationException: Error creating bean with name "sessionFactory" defined in class path resource [applicationContext.xml]:
Invocation of init method failed; nested exception is org.hibernate.InvalidMappingException: Unable to read XML
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
	at com.ssh2.dao.testSpring3AndHibernate4.testSpring(testSpring3AndHibernate4.java:18)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.hibernate.InvalidMappingException: Unable to read XML
	at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109)
	at org.hibernate.cfg.Configuration.add(Configuration.java:490)
	at org.hibernate.cfg.Configuration.add(Configuration.java:486)
	at org.hibernate.cfg.Configuration.add(Configuration.java:659)
	at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:697)
	at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:356)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
	... 35 more
Caused by: org.dom4j.DocumentException: http://hibernate.org/dtd/hibernate-mapping-3.0.dtd%20 Nested exception: http://hibernate.org/dtd/hibernate-mapping-3.0.dtd%20
	at org.dom4j.io.SAXReader.read(SAXReader.java:484)
	at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
	... 42 more

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver">
		</property>
		<property name="url"
			value="jdbc:mysql://localhost:3306/bookshop">
		</property>
		<property name="username" value="root"></property>
		<property name="password" value="woaiwojia..123"></property>
	</bean>
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>  
	            <prop key="hibernate.format_sql">true</prop>  
	            <prop key="hibernate.hbm2ddl.auto">update</prop> 
	            <prop key="hibernate.current_session_context_class">
	            	thread
	            </prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>com/ssh2/entity/Users.hbm.xml</value></list>
		</property></bean> 

		<!-- 定义UserDAOImpl类实例,并将已经创建好的LocalSessionFactoryBean的实例sessionFactory依赖注入给
		UserDAOImpl类中的sessionFactory
		 --> 
		 <bean id="userDAO" class="com.ssh2.dao.UserDAOImpl">
		 	<property name="sessionFactory" ref="sessionFactory"></property>
		 </bean>
		 <!-- 定义userBizImpl类实例,并给UserBizImpl类中的userDAO注入值 -->
		 <bean id="userBiz" class="com.ssh2.biz.UserBizImpl">
		 	<property name="userDAO" ref="userDAO"></property>
		</bean>
</beans>

测试方法:

package com.ssh2.dao;
import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ssh2.biz.UserBiz;
import com.ssh2.entity.Users;
public class testSpring3AndHibernate4 {
	@Test
	public void testSpring()
	{
		//加载spring的配置文件
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

		//获取配置中的实例
		UserBiz userBiz = (UserBiz)applicationContext.getBean("userBiz");
		Users condition = new Users();
		condition.setLoginName("wuyanlong");
		condition.setLoginPwd("woaiwojia..123");
		//模拟登陆实现
		List list = userBiz.login(condition);

		//登陆check
		if(list.size() > 0)
		{
			System.out.println("登陆成功!");
		}else{
			System.out.println("登陆失败!");
		}
	}
}
解决方案

10

hibernate配置文件

40

com/ssh2/entity/Users.hbm.xml中http://hibernate.org/dtd/hibernate-mapping-3.0.dtd多了个空格吧?贴出文件看看

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Spring3整合Hibernate4的问题,求指导
喜欢 (0)
[1034331897@qq.com]
分享 (0)