Integration of Spring and Hibernate
1. Integration of Spring and Hibernate 1.1. Create SessionFactory in Spring container
To avoid the Close coupling between hard-coded resource searches and application objects, Spring allows you to define data such as JDBC DataSource or Hibernate SessionFactory as beans in the Spring container to access resources. Any application object that requires resource access only needs to hold references to these predefined instances. The following Code demonstrates how to create a JDBC DataSource and Hibernate SessionFactory.
Configure ApplicationContext. xml as follows:
"DataSource"Class = "Org. apache. commons. dbcp. BasicDataSource"> "DriverClassName"Value = "Oracle. jdbc. driver. OracleDriver"> "Url"Value = "Jdbc: oracle: thin: @ 127.0.0.1: 1521: orcl"> "Username"Value = "Scott"> "Password"Value = "Tiger"> "SessionFactory" Class ="Org. springframework. orm. hibernate3.LocalSessionFactoryBean"> "DataSource"> "DataSource"/> "HibernateProperties"> "Hibernate. dialect"> Org. hibernate. dialect. Oracle9Dialect "MappingResources"> Com/Morris/School/Dao/Entity/Department. hbm. xml |
You can use the following in an application:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext ( "ApplicationContext. xml "); SessionFactory sessionFactory = (SessionFactory) applicationContext . GetBean ("sessionFactory "); |
1.2. Implement DAO Based on the native API of Hibernate3
DepartmentDapImpl. java
Package com. morris. school. dao. jdbc; Import org. hibernate. SessionFactory; Import org. hibernate. Transaction; Import org. hibernate. classic. Session; Import org. springframework. context. ApplicationContext; Import org. springframework. context. support. ClassPathXmlApplicationContext; Import com. morris. school. dao. IDepartmentDao; Import com. morris. school. dao. entity. Department; Public class DepartmentDapImpl implements IDepartmentDao { Private SessionFactory sessionFactory; Public SessionFactory getSessionFactory (){ Return sessionFactory; } Public void setSessionFactory (SessionFactory sessionFactory ){ This. sessionFactory = sessionFactory; } Public void addDepartment (Department dept ){ Session session = sessionFactory. openSession (); Transaction transaction = null; Try { Transaction = session. beginTransaction (); Session. save (dept ); Transaction. commit (); } Catch (Exception e ){ If (transaction! = Null ){ Transaction. rollback (); } E. printStackTrace (); } Finally { Session. close (); } } } |
In the spring configuration file, configure the following:
1.3. Spring-based DAO implementation
HibernateDaoSupport
The base class provides access bound to the current transactionSession
Object function, thus ensuring the correct conversion of exceptions in this case. Similar functions can also be used inSessionFactoryUtils
Class, but they appear in the form of static methods. It is worth noting that'false
'Is passedgetSession(..)
Method. At this time, the entire call will be completed in the same transaction (the entire lifecycle of the transaction is controlled by the transaction to avoid closing the returnedSession
).
Package com. morris. school. dao. jdbc; Import org. hibernate. SessionFactory; Import org. hibernate. Transaction; Import org. hibernate. classic. Session; Import org. springframework. context. ApplicationContext; Import org. springframework. context. support. ClassPathXmlApplicationContext; Import org. springframework. orm. hibernate3.support. HibernateDaoSupport; Import com. morris. school. dao. IDepartmentDao; Import com. morris. school. dao. entity. Department; Public class DepartmentDapImpl extends HibernateDaoSupport implements IDepartmentDao { Public static void main (String [] args ){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext ( "ApplicationContext. xml "); IDepartmentDao deptDao = (IDepartmentDao) applicationContext . GetBean ("deptDao "); DeptDao. addDepartment (new Department ("4", "Logistics ")); } Public void addDepartment (Department dept ){ This. getHibernateTemplate (). save (dept ); } } |
1.4. Single Transaction Management Configuration
"DeptDao"Class = "Com. morris. school. dao. jdbc. DepartmentDapImpl"> "SessionFactory"Ref = "SessionFactory"> "TransactionManager" Class ="Org. springframework. orm. hibernate3.HibernateTransactionManager"> "SessionFactory"Ref = "SessionFactory"> "TransactionProxy" Class ="Org. springframework. transaction. interceptor. TransactionProxyFactoryBean"> "Target"Ref = "DeptDao"> "TransactionManager"Ref = "TransactionManager"> "ProxyTargetClass"Value = "True"> "TransactionAttributes"> "Add *"> PROPAGATION_REQUIRED "Update *"> PROPAGATION_REQUIRED "Delete *"> PROPAGATION_REQUIRED "Insert *"> PROPAGATION_REQUIRED "Save *"> PROPAGATION_REQUIRED "Do *"> PROPAGATION_REQUIRED "Query *"> ReadOnly "Find *"> ReadOnly "*"> PROPAGATION_REQUIRED, readOnly |
1.5. configuration of public transactions
"DeptDao"Class = "Com. morris. school. dao. jdbc. DepartmentDapImpl"> "SessionFactory"Ref = "SessionFactory"> "TransactionManager" Class ="Org. springframework. orm. hibernate3.HibernateTransactionManager"> "SessionFactory"Ref = "SessionFactory"> "BaseTransactionProxy" Class ="Org. springframework. transaction. interceptor. TransactionProxyFactoryBean" Abstract ="True"Lazy-init ="True"> "TransactionManager"Ref = "TransactionManager"> "TransactionAttributes"> "Add *"> PROPAGATION_REQUIRED "Update *"> PROPAGATION_REQUIRED "Delete *"> PROPAGATION_REQUIRED "Insert *"> PROPAGATION_REQUIRED "Save *"> PROPAGATION_REQUIRED "Do *"> PROPAGATION_REQUIRED "Query *"> ReadOnly "Find *"> ReadOnly "*"> PROPAGATION_REQUIRED, readOnly "UserDaoProxy"Parent = "BaseTransactionProxy"> "Target"Ref = "DeptDao"> |