在配置applicationContext.xml时,其他先略去不说,sessionFactory如是:
<!-- 配置 SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:com/ssh/entities/*.hbm.xml"></property>
</bean>
这里出了问题:
Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
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: Could not parse mapping document from input stream
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:681)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:381)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:293)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:618)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1100)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1618)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3507)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3496)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3484)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1376)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1777)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1861)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:339)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:427)
at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:412)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
... 25 more
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping com.atguigu.ssh.entities.Department
at org.hibernate.cfg.Configuration$MappingsImpl.addClass(Configuration.java:2659)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:175)
at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3504)
... 35 more
检查hibernate.cfg.xml,代码如下:
<session-factory>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="com/ssh/entities/Department.hbm.xml" />
<mapping resource="com/ssh/entities/Employee.hbm.xml" />
</session-factory>
比较applicationContext.xml里的
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:com/ssh/entities/*.hbm.xml"></property>
</bean>
问题应该是该句和hibernate.cfg.xml中的
<mapping resource="com/ssh/entities/Department.hbm.xml" />
<mapping resource="com/ssh/entities/Employee.hbm.xml" />
重复了。
因为换个方式,把applicationContext.xml里的sessionFactory改一下
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</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.connection.autocommit">false</prop>
</props>
</property>
<property name="mappingLocations" value="classpath:com/ssh/entities/*.hbm.xml"></property>
</bean>
改动
<!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> -->
后相当于hibernate.cfg.xml已经没用了,其里面的
<mapping resource="com/ssh/entities/Department.hbm.xml" />
<mapping resource="com/ssh/entities/Employee.hbm.xml" />
与
<property name="mappingLocations" value="classpath:com/ssh/entities/*.hbm.xml"></property>
不存在重复,所以这样做也不报错
顺便,
<property name="mappingLocations" value="classpath:com/ssh/entities/*.hbm.xml"></property>
与
<property name="mappingLocations">
<list>
<value>com/ssh/entities/*.hbm.xml</value>
</list>
</property>
这样写貌似是一样的。
总结:1.将<property name="mappingLocations" value="classpath:com/ssh/entities/*.hbm.xml"></property>注释掉。
2.如下写。
<property name="dataSource" ref="dataSource"></property>
<!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</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.connection.autocommit">false</prop>
</props>
</property>
<property name="mappingLocations" value="classpath:com/ssh/entities/*.hbm.xml"></property>
</bean>
仅作记录。理解错误之处请大神真正