Difference between configuring Hibernate in Spring and configuring Hibernate separately, springhibernate
First, let's talk about the features of Spring and Hibernate:
Spring: Automatic dependency injection. classes can be used as beans.
Hibernate: Session factory sessionfactory, transaction Manager transaction.
If you only use Hibernate, You need to perform the following operations when operating the database:
Configuration conf = new Configuration (). configure ();
SessionFactory sf = conf. buildSessionFactory ();
Session sess = sf. openSession ();
Transaction tx = sess. beginTransaction ();
Then, operate the database method in the session and submit it in tx.
However, when you use Spring and configure Hibernate in Spring,
First, the session factory can be written into the Spring configuration file as a bean, 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 attributes in the bean sessionfactory. Of course, this class is in the Spring package and is a function provided by Spring.
Then, Spring can also provide a Transaction Manager to Hibernate in bean mode, for example:
<Bean id = "transactionManager"
Class = "org. springframework. orm. hibernate4.HibernateTransactionManager">
<Property name = "sessionFactory" ref = "sessionFactory"/>
</Bean>
<Tx: annotation-driven transaction-manager = "transactionManager"/>
Then the transaction interceptor injects the bean of the Transaction Manager, for example:
<Bean id = "transactionInterceptor"
Class = "org. springframework. transaction. interceptor. TransactionInterceptor">
<! -The transaction interceptor bean needs to inject a Transaction Manager dependency->
<Property name = "transactionManager" ref = "transactionManager"/>
</Bean>
Then, the bean of the Business proxy can be automatically generated. The bean will use the transaction interceptor, for example:
<! -Define BeanNameAutoProxyCreator->
<Bean
Class = "org. springframework. aop. framework. autoproxy. BeanNameAutoProxyCreator">
<! -Specify which beans automatically generate the business proxy->
<Property name = "beanNames">
<! -The following are all the beans that require automatic transaction proxy creation->
<List>
<Value> mgr </value>
</List>
<! -You can add other beans that require automatic transaction proxy creation.->
</Property>
<! -The following defines the transaction interceptor required by BeanNameAutoProxyCreator->
<Property name = "interceptorNames">
<List>
<! -You can add another Interceptor here.->
<Value> transactionInterceptor </value>
</List>
</Property>
</Bean>
Then, the bean in which we write the automatic creation transaction proxy id mgr is like this:
<Bean id = "mgr" class = "com. service. impl. ActionManagerImpl">
<Property name = "usersDAO" ref = "usersDAO"/>
</Bean>
UsersDAO is automatically injected.
In usersDAO, we don't need to be as complicated as creating configurations, session factories, and transactions as before. We just need to use various methods under getCurrentSession. Automatically retrieves the current session.
The configuration process is slightly more, but the overall write code is definitely less than the code written in the DAO class using hibernate alone.