从Hibernate unknown entity说起

在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>

 这样就可以实现部分功能的可插拔性。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值