Hibernate configuration files under spring supervision

Source: Internet
Author: User

I looked at someone else's program today.,with theSSHbuilt by,I recalled the feeling hypothesis.Hibernateconfiguration file Hibernate.cfg.xml is still needed.,and*.hbm.xmlcan be replaced by annotations.,It turns out I didn't find the hibernate.cfg.xml I wanted..in fact, think of yourself .SSHThe building of the environment is not well understood..but look at how they build themselves and how they build..so toSpringand theHibernatethe consolidated configuration file has been slightly sorted down.

Spring's integration of Hibernate profile Hibernate.cfg.xml is pretty good. Ability to configure Hibernate's sessionfactory in spring instead of Hibernate.cfg.xml and Hibernatesessionfactory.java,so inSpringand theHibernatewhen it comes to integration, we focus on*.hbm.xmlIf you still need.Of course we can choose to continue to use*.hbm.xmlor do not use such a configuration file,How to use annotations instead.

How to scan a map file while continuing to use Hibernate's mapping file *.hbm.xml

Spring integrates hibernate with Hibernate.cfg.xml removed. At this point, assume that you continue to use Hibernate's mapping file *.hbm.xml. Configuring the Hibernate mapping file in the Sessionfactory configuration is the key to this configuration file *.hbm.xml will be

When you configure Sessionfactorybean in spring, its corresponding class should be Org.springframework.orm.hibernate.LocalSessionFactoryBean

  <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 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 to look up hibernate mapping files: mappingresources, Mappinglocations, Mappingdirectorylocations and mappingjarlocations

the difference between them:    

mappingresources : Specify Classpath under detailed mapping file name  

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

mappinglocations : The ability to specify any file path, and the ability to specify a prefix: Classpath , file wait   

<property name= "Mappinglocations" >        <value>/web-inf/petclinic.hbm.xml </value>    </ property>    <property name= "mappinglocations" >        <value>classpath:/com/company/domain/ Petclinic.hbm.xml </value>    </property>

can also be specified with a wildcard character. ' * ' specifies a file (path) name, ' * * ' to specify multiple file (path) names, such as:

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

The above configuration is under the com/company/domain Package No matter what the maps path under the hbm.xml files are loaded into a mapping file  

mappingdirectorylocations : Specify the file path of the map    

<property name= "Mappingdirectorylocations" >      <list>          <value>web-inf/hibernatemappings </value>        </list>    


can also be pointed out through Classpath.

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

mappingjarlocations : Specifies that the loaded mapping file is Jar in the file  


Use the annotation form. The method that is configured when you remove the *.hbm.xml hibernate mapping file

Spring integrates hibernate with Hibernate.cfg.xml removed. This assumes the Pojo object in the form of JPA annotations. While removing Hibernate's mapping file *.hbm.xml, configure Hibernate sessionfactory to find out how to look for Pojo mapping objects in the form of JPA annotations

when Sessionfactorybean is configured in spring, its corresponding class should be Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean

Like what:

<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >        <property name= "DataSource" ref= "DataSource"/><!--Reference Data Source--        <property name= " Packagestoscan ">            <list>                <value>com.cn.nos.services.pojo*</value><!-- Entity classes loaded into Hibernate's JPA annotations-            </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 >

The properties of the Pojo mapping object in the form of Annotationsessionfactorybean lookup JPA annotations are: annotatedclasses, Packagestoscan

annotatedclasses : Specify Classpath The class name of the annotation map entity class specified under

<property name= "annotatedclasses" >     <list>          <value>com.test.ObjectBean</value> <!--can configure multiple--</list></property> in this list     

Packagestoscan Specify the package name for the mapping file

<property name= "Packagestoscan" >    <list>       <value>com.cn.nos.services.pojo*</value ><!--entity classes in the form of JPA annotations in hibernate-    </list></property>

The relationships between these profiles help us better understand how the framework works , Let's use the framework more handy .

Hibernate configuration files under spring supervision

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.