The difference between configuring Hibernate in spring and configuring hibernate separately

Source: Internet
Author: User

Let's start with some features of spring and hibernate:

Spring: Automatic Dependency Injection, classes can be used as beans.

Hibernate: Session factory Sessionfactory, transaction manager transaction.

If you use Hibernate only, then when you manipulate the database, you need to knock on things like:

Configuration conf = new configuration (). Configure ();

Sessionfactory SF = Conf.buildsessionfactory ();

Session sess = Sf.opensession ();

Transaction tx = Sess.begintransaction ();

The database method is then manipulated in the session and submitted in TX.

But when you use spring to configure Hibernate in spring,

The first is that the session factory can be used as a bean to write to the spring configuration file, for example:

<bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" >
<property name= "url" value= "Jdbc:jtds:sqlserver://localhost:1433/test" >
</property>
<property name= "Driverclassname" value= "Net.sourceforge.jtds.jdbc.Driver" >
</property>
<property name= "username" value= "sa" ></property>
<property name= "Password" value= "123456″></property>
</bean>
<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name= "DataSource" >
<ref bean= "DataSource"/>
</property>
<property name= "Hibernateproperties" >
<props>
<prop key= "Hibernate.dialect" >
Org.hibernate.dialect.SQLServerDialect
</prop>
</props>
</property>
<property name= "Mappingresources" >
<list>
<value>com/domain/Users.hbm.xml</value>
</list>
</property>
</bean>

Configure the data source and various properties in the Sessionfactory bean. This class, of course, is a spring package and is a feature that spring provides.

Spring can then also be provided to hibernate as a transaction manager in bean form, for example:

<bean id= "TransactionManager"
class= "Org.springframework.orm.hibernate4.HibernateTransactionManager" >
<property name= "Sessionfactory" ref= "Sessionfactory"/>
</bean>
<tx:annotation-driven transaction-manager= "TransactionManager"/>

The transaction interceptor is then injected into the transaction manager's Bean, for example:

<bean id= "Transactioninterceptor"
class= "Org.springframework.transaction.interceptor.TransactionInterceptor" >
The <!– transaction Interceptor Bean requires a dependency injection into a transaction manager –>
<property name= "TransactionManager" ref= "TransactionManager"/>
</bean>

Again, the bean that can generate the business agent automatically, the bean will use the transaction interceptor, for example:

<!– definition Beannameautoproxycreator–>
<bean
class= "Org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
<!– specifies which beans are automatically generated by the business agent –>
<property name= "Beannames" >
<!– below are all the bean–> that need to automatically create a transaction agent
<list>
<value>mgr</value>
</list>
<!– Here you can add additional bean–> that need to automatically create a transaction agent
</property>
<!– the transaction interceptors required for beannameautoproxycreator are defined below –>
<property name= "Interceptornames" >
<list>
<!– here to add other new interceptor–>
<value>transactionInterceptor</value>
</list>
</property>
</bean>

So, the bean that we wrote in which the auto-Create transaction Agent ID is Mgr is this:

<bean id= "Mgr" class= "Com.service.impl.ActionManagerImpl" >
<property name= "Usersdao" ref= "Usersdao"/>
</bean>

Among them, Usersdao will be automatically injected.

We don't need to create configurations, session factories, transactions so complex in Usersdao, just use the various methods under Getcurrentsession (). will help you get the current session automatically.

The configuration process is slightly more, but the overall code is definitely less than the code written in the DAO class alone with Hibernate.

The difference between configuring Hibernate in spring and configuring hibernate separately

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.