简体   繁体   中英

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

I currently scan packages for DAOs and services using Spring 3.1 & Hibernate 4 via <context:component-scan> Is there a way to do the same for classes marked @Entity instead of using the configLocation property and a hbm.xml file?

<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"
/>

Will scan everything in model package. I use my cfg.xml to contains settings like show_sql, and hb2ddl.auto.

You can do some like this in application context.xml file to scan all annotation classes -

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

You can use the mappingLocations property of Spring to specify a location where spring will look for hibernate mapping files.

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

Hope this helps.

Simplifying, you could use code like the following in the configuration file '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>

Note:

  • class AnnotationSessionFactoryBean is required in "sessionFactory"
  • property "packagesToScan" with the annotated classes package (or a list of packages -see Kumar's example to do this-)

While using Hibernate3, the SessionFactory Bean creation code could be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM