簡體   English   中英

如何掃描包中的 Hibernate 實體而不是使用 hbm.xml?

[英]How to scan packages for Hibernate entities instead of using hbm.xml?

我目前使用 Spring 3.1 & Hibernate 4 通過<context:component-scan>掃描 DAO 和服務的包有沒有辦法對標記為@Entity的類執行相同的操作,而不是使用configLocation屬性和hbm.xml文件?

<hibernate-configuration>
    <session-factory>
        <mapping class="com.example.model.User" />
            <!-- etc. -->
    </session-factory>
</hibernate-configuration>
<bean id="sessionFactory"
  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
  p:dataSource-ref="dataSource"
  p:configLocation="WEB-INF/classes/hibernate.cfg.xml"
  p:packagesToScan="com.example.model"
/>

將掃描模型包中的所有內容。 我使用cfg.xml包含show_sql和hb2ddl.auto等設置。

您可以在應用程序context.xml文件中執行此類操作來掃描所有注釋類 -

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="lobHandler" ref="lobHandler"/>
<property name="packagesToScan">
    <list>
        <value>com.idc.scd.domain</value>
        <value>com.idc.scd.domain.dropdown</value>
        <value>com.idc.scd.domain.external</value>
        <value>com.idc.scd.domain.pk</value>
    </list>
</property>
    <property name="hibernateProperties">
      <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
        <prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
        <prop key="hbm2ddl.auto">validate</prop>
        <prop key="hibernate.cache.use_query_cache">true</prop>
        <prop key="hibernate.connection.release_mode">after_statement</prop>
        <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
        <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
        <prop key="hibernate.cache.use_structured_entries">${hibernate.cache.use_structured_entries}</prop>
        <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
        </props>
    </property>
</bean>

您可以使用Spring的mappingLocations屬性指定spring查找hibernate映射文件的位置。

<property name="mappingLocations" value="classpath:com/example/model/hibernate/*.hbm.xml"/>

希望這可以幫助。

簡化,您可以在配置文件'spring-servlet.xml'中使用以下代碼:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
        <property name="packagesToScan" value="com.your.bean.package" />
    </bean>

注意:

  • “sessionFactory”中需要類AnnotationSessionFactoryBean
  • 屬性“packagesToScan”帶有帶注釋的類包(或包列表 - 請參閱Kumar的例子 - )

使用 Hibernate3 時,SessionFactory Bean 創建代碼可以是:

LocalSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean();
((AnnotationSessionFactoryBean)sessionFactory).setPackagesToScan("my.entity.packages1", "my.entity.packages2");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM