Spring integrates hibernate configuration files

Spring integrates the hibernate configuration file hibernate.cfg.xml to replace the configuration of hibernate.cfg.xml

Spring's integration of hibernate configuration file hibernate.cfg.xml is quite good, and Hibernate's SessionFactory can be configured in Spring to replace Hibernate.cfg.xml and HibernateSessionFactory.java

Spring is divided into two forms when integrating Hibernate: (Hibernate.cfg.xml is not needed after integration)
1. Continue to use Hibernate's mapping file *.hbm.xml
2. Use the pojo object in the form of jpa annotation, and remove it *.hbm.xml Hibernate mapping file


1. The method of scanning the mapping file when continuing to use the Hibernate mapping file *.hbm.xml

When Spring integrates Hibernate, Hibernate.cfg.xml is removed. If you continue to use Hibernate's mapping file *.hbm.xml, you need to configure how to find Hibernate mapping file *.hbm when configuring Hibernate's SessionFactory. xml

At this time, when the SessionFactory bean is configured in spring, its corresponding class should be org.springframework.orm.hibernate.LocalSessionFactoryBean
, for example:

<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" /><!-- Reference data source -->
        <property name="mappingDirectoryLocations">
            <list>
                <value>classpath:com/cn/nos/services/pojo/</value><!-- Load the hibernate mapping file *.hbm.xml -->
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <!--<prop key="hibernate.current_session_context_class">thread</prop>-->
            </props>
        </property>
    </bean>

LocalSessionFactoryBean has several properties used to find hibernate mapping files: mappingResources, mappingLocations, mappingDirectoryLocations and mappingJarLocations 

Their differences:    

① mappingResources: Specify the specific mapping file name under the classpath

<property name="mappingResources">    
    <value>petclinic.hbm.xml </value>    </property>


 ② mappingLocations: You can specify any file path, and you can specify a prefix: classpath, file, etc.  

<property name="mappingLocations">    
    <value>classpath:/com/company/domainmaps/*.hbm.xml </value>    </property>
 The above configuration is that the hbm.xml file under any maps path under the com/company/domain package is loaded as a mapping file 


③ mappingDirectoryLocations: specify the mapped file path 

<property name="mappingDirectoryLocations">   <list>    
    <value>WEB-INF/HibernateMappings</value>    
  </list>    </property>
It can also be pointed out through the classpath    
<property name="mappingDirectoryLocations">    
  <list>    
  <value>classpath:/XXX/package/</value>    
  </list>    </property>

④ mappingJarLocations: Specify the loaded mapping file in the jar file


2. The method configured when using the pojo object in the form of jpa annotations and removing the Hibernate mapping file of *.hbm.xml

When Spring integrates Hibernate, Hibernate.cfg.xml is removed. At this time, if the pojo object in the form of jpa annotation is used, and the Hibernate mapping file *.hbm.xml is removed, when configuring Hibernate's SessionFactory, it is necessary to configure how to find it. pojo mapping object in the form of jpa annotations

At this time, when the SessionFactory bean is configured in spring, its corresponding class should be org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean

E.g:

 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" /><!-- 引用数据源 -->
        <property name="packagesToScan">
            <list>
                <value>com.cn.nos.services.pojo*</value><!-- 加载hibernate的jpa注解形式的实体类 -->
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <!--<prop key="hibernate.current_session_context_class">thread</prop>-->
            </props>
        </property>
    </bean>

AnnotationSessionFactoryBean中查找jpa注解形式的pojo映射对象的属性有:annotatedClasses、packagesToScan

① annotatedClasses:指定classpath下指定的注解映射实体类的类名

<property name="annotatedClasses">
     <list>
       <value>com.test.ObjectBean</value><!-- 可以在这个list中配置多个 -->
     </list></property>
② packagesToScan指定映射文件的包名
<property name="packagesToScan">
    <list>
       <value>com.cn.nos.services.pojo*</value><!-- 加载hibernate的jpa注解形式的实体类 -->
    </list></property>


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325601831&siteId=291194637