Spring integrates Hibernate ing files in four ways,

Source: Internet
Author: User

Spring integrates Hibernate ing files in four ways,

In Spring applicationContext. the Hibernate ing file is integrated in xml, usually in the <sessionFactory> Bean instance. If there are few ing files configured, you can use the "mappingResources" attribute of LocalSessionFactoryBean, which belongs to sessionFactory. However, using the "mappingResources" attribute is not the only choice. Other equivalent options include mappingLocations, mappingDirectoryLocations, and mappingJarLocations)

An example of configuring a sessionFactory is as follows:


<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">            <property name="dataSource">                <ref bean="hotelSysDS" />            </property>            <property name="hibernateProperties">                <props>                    <prop key="hibernate.dialect">                        org.hibernate.dialect.MySQLDialect                    </prop>                    <prop key="hibernate.show_sql">true</prop>                    <prop key="hibernate.format_sql">true</prop>                                </props>            </property>            <property name="mappingResources">               <list>                  <value>/edu/fjnu/hotelsys/domain/User.hbm.xml</value>                  <value>/edu/fjnu/hotelsys/domain/Hotel.hbm.xml</value>                  <value>/edu/fjnu/hotelsys/domain/Room.hbm.xml</value>               </list>            </property>     </bean>     
Directory: mappingResources

MappingResources: Specifies the specific clasing file name under classpath.

Method 1:

<property name="mappingResources" value=“userTest.hbm.xml”/>

Method 2:

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

Method 3: Use list to specify multiple ing files at the same time

<property name="mappingResources">    <list>        <value>/edu/fjnu/hotelsys/domain/User.hbm.xml</value>        <value>/edu/fjnu/hotelsys/domain/Hotel.hbm.xml</value>        <value>/edu/fjnu/hotelsys/domain/Room.hbm.xml</value>      </list></property>

As the number of configuration files increases, reading and modification becomes more and more troublesome, and XML-based configuration may also cause input errors, as a result, you may waste half a day searching for errors due to a character error.

Note: The configuration methods will not be listed one by one below


MappingLocations: You can specify any file path.

<property name="mappingLocations">     <value>/WEB-INF/petclinic.hbm.xml </value> </property> 


Or specify the prefix: classpath, file, etc.

<property name="mappingLocations">     <value>classpath:/com/company/domain/petclinic.hbm.xml </value> </property> 


You can also specify a file (PATH) name with a wildcard character. '*' specifies multiple file (PATH) names. For example:

<property name="mappingLocations">     <value>classpath:/com/company/domainmaps/*.hbm.xml </value> </property> 

The above configuration is that all hbm. xml files under the com/company/domain package are loaded as ing files.


MappingDirectoryLocations: Specifies the mapped file path.

<property name="mappingDirectoryLocations">  <list>  <value>WEB-INF/HibernateMappings</value>  </list></property>


You can also use classpath to specify

<property name="mappingDirectoryLocations">  <list>  <value>classpath:/XXX/package/</value>  </list></property>



MappingJarLocations: Specifies the loaded ing file in the jar file.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.