SPRING4 integrated HIBERNATE5 and emerging problem solving methods

Source: Internet
Author: User

Each time the study, is a small step by step, learning language, it is important to write Hello World

And the problems that arise during the learning process can be recorded and summarized in a timely manner.

Spring is currently the latest version of 4.3, while Hibernate is 5.2

The latest version of learning is going to take a lot of courage

First, there is hardly any information on the Internet for reference.

Secondly, these two frameworks are very different from the previous versions in the processing methods of some business logic.

The first is hibernate, and building sessionfactory is no longer the old version of 3.5.

Here's my approach to getting sessionfactory

 Public Staticsessionfactory getsessionfactory () {sessionfactory sessionfactory=NULL; FinalStandardserviceregistry Registry =Newstandardserviceregistrybuilder (). Configure (). build (); Try{sessionfactory=Newmetadatasources (registry). Buildmetadata (). Buildsessionfactory (); } Catch(Exception e) {Standardserviceregistrybuilder.destroy (registry); }        returnsessionfactory; }

Spring is a master who can manage hibernate well

Furthermore. Spring AOP is so powerful that it can be easily managed with slicing programming

Hibernate can be configured directly in spring, so the previous hibernate.cfg.xml can be discarded directly.

We can do sessionfactory configuration in spring configuration file, datasource configuration and transaction configuration

<id= "Sessionfactory"        class= " Org.springframework.orm.hibernate5.LocalSessionFactoryBean ">        <  name = "DataSource" ref = "DataSource" />

Note here that in Hibernate5, our sessionfactory is instantiated by the Org.springframework.orm.hibernate5.LocalSessionFactoryBean class provided by spring.

DataSource the definition of a data source

We can use the C3P0 for the data buffer pool.

Add transaction support

<!--Configure Hibernate transaction manager -    <BeanID= "TransactionManager"class= "Org.springframework.orm.hibernate5.HibernateTransactionManager">        < Propertyname= "Sessionfactory"ref= "Sessionfactory" />    </Bean>    <Aop:configProxy-target-class= "true">                <Aop:pointcutexpression= "Execution (* top.scorpion.service). *(..))"ID= "Servicemethod" />                <Aop:advisorAdvice-ref= "Txadvice"Pointcut-ref= "Servicemethod" />    </Aop:config>        <Tx:adviceID= "Txadvice"Transaction-manager= "TransactionManager">                <tx:attributes>            <Tx:methodname="*" />        </tx:attributes>    </Tx:advice>

So we can use spring to manage hibernate.

SPRING4 integrated HIBERNATE5 and emerging problem solving methods

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.