在Spring+Hibernate企业级应用中,会用到Hibernate annotation。 在开发过程中常常会遇到unknown entity 的error。这个问题有以下几个原因:
1. @Entity 引入错误。
在ORM annotation中有两个Entity: org.hibernate.annotations.Entity 和 javax.persistence.Entity。
在bean中应使用后一个。
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Entity;// this is the RIGHT one
@Entity
@Table(name="MyTable")
public class MyBean implements Serializable
2. 缺少相应的配置。
a) 在hibernate配置文件中(eg. hibernate.cfg.xml) 需要相应的配置:
<mapping class="com.mycompany.model.MyBean" />
这样在sessionFactory中才能找得到
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> <property name="configurationClass"> <value>org.hibernate.cfg.AnnotationConfiguration</value> </property> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <value> hibernate.dialect=#{jdbcProperties.dialect} hibernate.show_sql=true hibernate.hbm2ddl.auto=update//自动生成数据库表,如果表存在就更新数据库表字段但是不会删除表中的数据 hibernate.format_sql=true hibernate.cache.use_second_level_cache=false hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider </value> </property> </bean>
或者
<jee:jndi-lookup id="dataSource" jndi-name="java:/ds" /> <bean id="entityInterceptor" class="org.hibernate.EmptyInterceptor" /> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> <property name="configurationClass"> <value>org.hibernate.cfg.AnnotationConfiguration</value> </property> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <ref bean="hibernateProperties" /> </property> <property name="entityInterceptor"> <ref local="entityInterceptor" /> </property> </bean> <bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="properties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect </prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.c3p0.minPoolSize">5</prop> <prop key="hibernate.c3p0.maxPoolSize">20</prop> <prop key="hibernate.c3p0.timeout">600</prop> <prop key="hibernate.c3p0.max_statement">50</prop> <prop key="hibernate.c3p0.testConnectionOnCheckout"> false </prop> </props> </property> </bean>
在企业级开发中,分别部署,或者以plugin的形式部署不同的Eclipse project是常见的现象,为了遵从开闭原则(OCP:Open-Closed Principle -- Software entities should be open for extension,but closed for modification), 可以用spring bean的形式来添加ORM。只需新建一个sessionFactory即可。
<bean id="amSessionFactory" parent="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="annotatedClasses"> <list> <value>com.mycompany.model.MyBean</value> <value>com.mycompany.model.MyBean2</value> </list> </property> <!-- or below configuration --> <property name="packagesToScan"> <list> <value>com.mycompany.model</value> </list> </property> <!-- <property name="packagesToScan" value="com.mycompany.*.model" > --> </bean>
这样就可以实现部分功能的可插拔性。