资源:未找到net/codejava/spring/model/User.hbm.xml-spring和Hibernate配置错误
我下载了一个教程项目,试图让Spring和Hibernate正常工作。但是,在服务器上运行后,我收到以下消息:资源:未找到net/codejava/spring/model/User.hbm.xml-spring和Hibernate配置错误,java,xml,spring,hibernate,spring-mvc,Java,Xml,Spring,Hibernate,Spring Mvc,我下载了一个教程项目,试图让Spring和Hibernate正常工作。但是,在服务器上运行后,我收到以下消息: 调用init方法失败;嵌套异常为org.hibernate.MappingNotFoundException:resource:net/codejava/spring/model/User.hbm.xml未找到 以下是我的项目结构: hibernate.cfg.xml org.hibernate.dialogue.mysqldialogue 真的 user.hbm.xml s
调用init方法失败;嵌套异常为org.hibernate.MappingNotFoundException:resource:net/codejava/spring/model/User.hbm.xml未找到
以下是我的项目结构:
hibernate.cfg.xml
org.hibernate.dialogue.mysqldialogue
真的
user.hbm.xml
servlet-context.xml
有什么想法吗?您有一个
maven
项目,因此User.hbm.xml
应该在src/main/resources/
中,而不是src/main/java
当你构建一个Maven项目时,它编译的是src/main/resources
,您可以在其中放置所有配置文件,如.xml,.properties
等。因此,当您构建maven项目时,它将编译src/main/resources
中的所有java文件,并将.class
文件放置在类路径中,maven还将复制src/main/resources
中的所有资源,并将它们放置在类路径中。因此,当您运行应用程序时,配置文件也将在类路径中可用
但是如果您只是将配置文件放在src/main/java
中,那么maven将忽略它们,因此它们在类路径中不可用
但是,如果您正在处理的项目是一个简单的java项目,而不是maven项目,那么即使您在src/main/java
中有配置文件,您的问题中提到的代码设置也可以正常工作。希望这个解释有帮助
要确保maven项目正确构建,您可以打开目录target/classes/
,然后打开配置文件的路径进行验证
因此,为了总结所有这些信息,您需要做以下几点:
在/src/main/resources
中创建一个包结构,它模仿/src/main/java
中的包结构-也就是说,在/src/main/resources
中创建net.codejava.spring.model
包,并将User.hbm.xml
放在其中。运行Mavenclean package
(或其他一些构建目标)命令后,User.hbm.xml
文件将位于正确的位置(下图中未显示)/target/SpringMvcHibernateXML-1.0.0-build-SNAPSHOT/WEB-INF/classes/net/codejava/spring/model/User.hbm.xml
最终,在Maven中构建之后,项目应该如下所示:
明白了。在我的
hibernate.config.xml
中,我有
-既然User.hbm.xml
在src/main/resources/
中,那么资源属性会变成什么呢?@tyleree12,不,您误解了。我会用进一步的解释更新我的问题,请检查。谢谢你进一步的解释,这很有意义。但是,即使将User.hbm.xml
移动到/src/main/resources
后,我仍然收到嵌套异常是org.hibernate.MappingNotFoundException:resource:net/codejava/spring/model/User.hbm.xml未找到
错误。我需要做什么更改来解决这个问题?你必须构建maven项目,看起来你只是放置了文件并运行了程序。构建项目会将文件正确复制到类路径。在我的答案上多加了一行,以表示理解。你是对的,我没有那样做。然而,即使在运行Mavenclean-package
并在Spring工具套件中使用嵌入式tomcat7服务器运行之后,我仍然收到相同的错误。有什么想法吗?
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping resource="net/codejava/spring/model/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="net.codejava.spring.model">
<class name="User" table="users">
<id name="id" column="user_id">
<generator class="native"/>
</id>
<property name="username" column="username" />
<property name="password" column="password" />
<property name="email" column="email" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="net.codejava.spring" />
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://domain:3306/databaseHere"/>
<property name="username" value="insertUserHere"/>
<property name="password" value="insertPassHere"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="userDao" class="net.codejava.spring.dao.UserDAOImpl">
<constructor-arg>
<ref bean="sessionFactory" />
</constructor-arg>
</bean>
</beans>