ssh项目运行一直报org.hibernate.MappingException: Unknown entity 。网络上有很多其他的关于这个问题的解决方法,我这里记录的是我在撸码过程中遇到的情形。
spring-xxx.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<import resource="classpath:**/config/datasource_amicos_dev.xml" />
<import resource="classpath:**/config/datasource_foc.xml" />
<bean id="medetHRSessionFactory2"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="prodszaDataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">
true
</prop>
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.connection.release_mode">
auto
</prop>
<prop key="hibernate.autoReconnect">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
<property name="packagesToScan">
<list> <value>com/sXxx/smart/productlogXXX/model/</value>
</list>
</property>
</bean>
<!-- *************Transaction manager for a single Hibernate SessionFactory (alternative to JTA)************ -->
<bean id="medetHRTransactionManager2"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="medetHRSessionFactory2" />
</property>
</bean>
<tx:advice id="txAdvice"
transaction-manager="medetHRTransactionManager2">
<tx:attributes>
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:advisor
pointcut="execution(* com.szair.smartme.productlogsys.*.*.*(..))"
advice-ref="txAdvice" />
</aop:config>
<!-- 用@Transactional注释的方式控制事务 -->
<tx:annotation-driven
transaction-manager="medetHRTransactionManager2"
proxy-target-class="true" />
<!-- 使Spring关注Annotation -->
<context:annotation-config />
<!-- 让Spring通过自动扫描来查询和管理Bean -->
<context:component-scan base-package="com.szair.smartme.productlogsys" />
<!-- 配置Quartz -->
<bean id="quartzJob" class="com.szair.smartme.productlogsys.service.QuartzJob"></bean>
<bean id="quartzJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 指定任务类 -->
<property name="targetObject" ref="quartzJob"></property>
<!-- 指定执行的方法 -->
<property name="targetMethod" value="execute"></property>
</bean>
<bean id="quartzJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="quartzJobDetail" />
<!-- 每10秒运行一次 -->
<property name="cronExpression" value="0/30 * * * * ?" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="quartzJobTrigger" />
</list>
</property>
</bean>
</beans>
自认为是没有什么错的,但是就是提示unknow entity 的错误。后来才明白错误出在哪了。因为这个项目里有两个spring的配置文件,我是直接在前人写好的dao里接着写,然而那个dao里引用的是datasource和我在建表的数据库不一致,所以才出现这个问题。后来就自己新建了一个dao类,里面引用正确的sessionFactory就可以了。