org.hibernate.MappingException:找不到实体类:dto.CityDTO

时间:2014-04-07 22:54:14

标签: java spring hibernate tomcat struts2

我正在开发一个Web应用程序,在Spring,Struts 2,Hibernate with Maven。使用maven,我可以成功制作war文件但是当我在tomcat 7上部署它时,我收到了以下错误。

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myTransactionManager' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Cannot resolve reference to bean 'mySessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: entity class not found: dto.CityDTO
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4600)
    at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5097)
    at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5092)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    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:722)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in ServletContext resource [/WEB-INF/classes/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: entity class not found: dto.CityDTO

我有以下配置文件

的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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


    <!-- Transaction manager for a single Hibernate SessionFactory (alternative 
        to JTA) -->

    <bean id="myTransactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="mySessionFactory" />
        </property>
    </bean>

    <!-- City -->
    <bean id="cityDTO" class="dto.CityDTO" scope="prototype">
        <property name="cityId" value="" />
        <property name="cityName" value="" />
    </bean>

    <bean id="cityDAOBean" class="dao.CityDAOHiberImp">
        <property name="sessionFactory">
            <ref local="mySessionFactory" />
        </property>
    </bean>
    <bean id="cityServiceBean" class="service.CityServiceImp" scope="prototype">
        <property name="cityDAO">
            <ref local="cityDAOBean" />
        </property>
    </bean>

    <!-- Employee -->
    <bean id="employeeDTO" class="dto.EmployeeDTO" scope="prototype">
        <property name="empId" value="" />
        <property name="empName" value="" />
        <property name="dateOfBirth" value="" />
        <property name="dateOfJoining" value="" />
        <property name="salaryExpected" value="" />
        <property name="salaryOffered" value="" />
        <property name="fulltime" value="" />
        <property name="permanent" value="" />
        <property name="currentCity" value="" />
    </bean>

    <bean id="employeeDAOBean" class="dao.EmployeeDAOHiberImp">
        <property name="sessionFactory">
            <ref local="mySessionFactory" />
        </property>
    </bean>

    <bean id="employeeServiceBean" class="service.EmployeeServiceImp"
        scope="prototype">
        <property name="employeeDAO">
            <ref local="employeeDAOBean" />
        </property>
    </bean>
    <!-- action mapping -->

    <bean id="dispEmployeeAction" class="action.DispEmployeeAction">
        <property name="employeeService">
            <ref local="employeeServiceBean" />
        </property>
        <property name="cityService">
            <ref local="cityServiceBean" />
        </property>
    </bean>

    <bean id="employeeAction" class="action.EmployeeAction">
        <property name="employeeService">
            <ref local="employeeServiceBean" />
        </property>
        <property name="cityService">
            <ref local="cityServiceBean" />
        </property>
    </bean>

    <bean id="dispEmployeeListAction" class="action.DispEmployeeListAction">
        <property name="employeeService">
            <ref local="employeeServiceBean" />
        </property>
    </bean>

    <bean id="employeeListAction" class="action.EmployeeListAction">
        <property name="employeeService">
            <ref local="employeeServiceBean" />
        </property>
    </bean>
        <!-- Hibernate Db Connection -->
    <bean id="mySessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="mappingResources">
            <list>
                <!-- mapping of session factory to dto hibernate -->
                <value>dto/CityDTO.hbm.xml</value>
                <value>dto/EmployeeDTO.hbm.xml</value>
            </list>
        </property>
        <!-- Hibernate configuration to database -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
                <prop key="hibernate.connection.password">root</prop>
                <prop key="hibernate.connection.username">root</prop>
                <prop key="hibernate.connection.driver_class">
                    com.mysql.jdbc.Driver
                </prop>
                <prop key="hibernate.connection.url">
                    jdbc:mysql://localhost:3306/employee_database
                </prop>
                <prop key="hibernate.connection.autocommit">true</prop>
            </props>
        </property>
    </bean>


</beans>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!--  <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">-->
    <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <listener>
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
    </listener>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
    </context-param>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>EmployeeManagementSystem</groupId>
    <artifactId>EmployeeManagementSystem</artifactId>
    <version>0.0.1</version>
    <packaging>war</packaging>
    <name>EmployeeManagementSystem</name>
    <build>
        <plugins>
            <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.2</version>
            <configuration>
            <webXml>WebContent\WEB-INF\web.xml</webXml>
            </configuration>
        </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.1.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.1.8</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
            <version>2.5.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>2.5.6</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate</artifactId>
            <version>3.2.7.ga</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.9</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.1</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-tiles-plugin</artifactId>
            <version>2.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-dojo-plugin</artifactId>
            <version>2.1.6</version>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>antlr</groupId>
            <artifactId>antlr</artifactId>
            <version>2.7.7</version>
        </dependency>
    </dependencies>
</project>

请有人建议解决方案。

1 个答案:

答案 0 :(得分:0)

更改为

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfigurationWithWildcard"/>
    <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>

添加hibernate.cfg文件:

    

    <!-- connection details -->
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql://localhost/myDatabase</property>
    <property name="hibernate.connection.username">myUser</property>
    <property name="hibernate.connection.password">myPass</property>
    <!-- @see: http://community.jboss.org/wiki/HibernateCoreMigrationGuide36 -->
    <property name="hibernate.jdbc.use_streams_for_binary">false</property>

    <!-- mappings for annotated classes -->
    <mapping class="dto.CityDTO"/>
    <mapping class="dto.EmployeeDTO"/>

</session-factory>

配置部分

 <property name="mappingResources">
            <list>
                <!-- mapping of session factory to dto hibernate -->
                <value>dto/CityDTO.hbm.xml</value>
                <value>dto/EmployeeDTO.hbm.xml</value>
            </list>

正在寻找我认为你没有的hbm文件