hibernate 4 sessionfactory packagestoscan属性用法

时间:2013-12-18 05:44:07

标签: spring hibernate sessionfactory

我们有一个基于Spring 3.2.4和Hibernate 4.2.3的Web应用程序。它是使用appfuse 2.2.1原型生成的。根据可用的文档,我们可以使用SessionFactory的packagesToScan属性来动态获取实体类并设置sessionFactory。但是,我们面临一个问题,我们必须在hibernate.cfg.xml中明确提到实体类名,以便hibernate识别它。 我在论坛上找不到合适的答案,所以在这里发帖。可能我对其工作原理的理解可能不正确,因此欢迎使用任何指针。

由于

的applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans -- namespace declarations here
       default-lazy-init="true">
     <!-- Activates scanning of @Autowired -->
     <context:annotation-config/>
     <!-- Activates scanning of @Repository and @Service -->
     <context:component-scan base-package="com.xyz"/>
</beans>

hibernate.cfg.xml中

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 <hibernate-configuration>
    <session-factory>
       <mapping class="com.xyz.model.Address" />
       <mapping class="com.xyz.model.Company" />
       <mapping class="com.xyz.model.CompanyBilling" />
       <!-- BIG LIST OF MY ENTITY CLASSES -->
    </session-factory>
 </hibernate-configuration>

的applicationContext-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans -- namespace declarations here
      default-lazy-init="true">
    <bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator"/>
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" destroy-method="destroy">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
        <property name="hibernateProperties">
            <props>
                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                 <prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop>
                 <prop key="hibernate.cache.use_second_level_cache">true</prop>
                 <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
            </props>
        </property>
        <property name="packagesToScan"><list><value>com.xyz.model</value></list></property>
    </bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
    <bean id="transactionManager"  class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <!-- Activates scanning of @Autowired -->
    <context:annotation-config/>
    <!-- Activates scanning of @Repository -->
    <context:component-scan base-package="com.xyz.dao"/>
</beans>

Company.java

 package com.xyz.model;
 // imports
 @Entity
 @Table(name = "company")
 @SQLDelete(sql = "UPDATE company SET activeFlag = 0, lastupdated=now() WHERE id = ?")
 @Where(clause = "1=activeflag")
 @Component
 public class Company extends BaseEntity implements java.io.Serializable {
    // properties and getter setters
 }

2 个答案:

答案 0 :(得分:1)

第二个属性packagesToScan指定Java包以自动扫描带注释的实体类。这样就不再需要准备Hibernate映射文件了。

答案 1 :(得分:-4)

变化:

<property name="packagesToScan"><list><value>com.xyz.model</value></list></property>

要:

<property name="packagesToScan" value="com.xyz.model" />

这适合我。