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