Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

(9) Integration of Spring and Hibernate

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Key points for the integration of Spring and Hibernate:

1) send the SessionFactory object of Hibernate to Spring to create

2) declarative transaction management of Hibernate transactions to Spring.

Steps to integrate Spring and Hibernate:

1) introduce jar package

2) configuration: hibernate.cfg.xml, * .hbm.xml, applicationContext.xml

3) build the environment and test separately

1. Introduce jar package

Hibernate3 related jar package

Hibernate3.jar

Antlr-2.7.6.jar (required)

Commons-collections-3.1.jar (required)

Dom4j-1.6.1.jar (required)

Javassist-3.12.0.GA.jar (required)

Jta-1.1.jar (required)

Slf4j-api-1.6.1.jar (required)

Hibernate-jpa-2.0-api-1.0.0.Final.jar (jpa)

Spring-core related jar package

Commons-logging-1.2.jar

Spring-beans-3.2.5.RELEASE.jar

Spring-context-3.2.5.RELEASE.jar

Spring-core-3.2.5.RELEASE.jar

Spring-expression-3.2.5.RELEASE.jar

Spring-aop related jar package

Aopalliance-.jar

Aspectjrt.jar

Aspectjweaver.jar

Spring-aop-3.2.5.RELEASE.jar

Spring-jdbc related jar package

Spring-jdbc-3.2.5.RELEASE.jar

Spring-tx-3.2.5.RELEASE.jar [transaction related]

Spring-orm related jar package

Spring-orm-3.2.5.RELEASE.jar [spring support for hibernate]

C3p0 related jar package

C3p0-0.9.1.2.jar

Mysql related jar package

Mysql-connector-java-5.1.38-bin.jar

2. Test Hibernate

Hibernate.cfg.xml

Com.mysql.jdbc.Driver jdbc:mysql:///test root root org.hibernate.dialect.MySQL5Dialect true false update thread

Dept.hbm.xml

Dept.java

Package com.rk.entity;public class Dept {private int deptId; private String deptName; private int deptVersion; public int getDeptId () {return deptId;} public void setDeptId (int deptId) {this.deptId = deptId;} public String getDeptName () {return deptName } public void setDeptName (String deptName) {this.deptName = deptName;} public int getDeptVersion () {return deptVersion;} public void setDeptVersion (int deptVersion) {this.deptVersion = deptVersion @ Override public String toString () {return "Dept [deptId=" + deptId + ", deptName=" + deptName + ", deptVersion=" + deptVersion + "]";}}

DeptDao.java

Package com.rk.dao;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import com.rk.entity.Dept;public class DeptDao {private static SessionFactory sf; static {sf = new Configuration (). Configure (). BuildSessionFactory ();} public Dept findById (int id) {Session session = sf.getCurrentSession () Session.beginTransaction (); Dept dept = (Dept) session.get (Dept.class, id); session.getTransaction () .commit (); return dept;} public void save (Dept dept) {Session session = sf.getCurrentSession () Session.beginTransaction (); session.save (dept); session.getTransaction (). Commit ();}

DeptService.java

Package com.rk.service;import com.rk.dao.DeptDao;import com.rk.entity.Dept;public class DeptService {private DeptDao deptDao = new DeptDao (); public Dept findById (int id) {return deptDao.findById (id);} public void save (Dept dept) {deptDao.save (dept);}}

App.java

Package com.rk.test;import org.junit.Test;import com.rk.entity.Dept;import com.rk.service.DeptService;public class App {@ Test public void test () {DeptService deptService = new DeptService (); / / Dept dept = deptService.findById (3); / / System.out.println (dept); Dept dept = new Dept () Dept.setDeptName ("HelloWorld"); deptService.save (dept);}}

3. Test the Spring environment

ApplicationContext.xml (add)

App.java (modified)

@ Test public void test () {ApplicationContext ac = new ClassPathXmlApplicationContext ("applicationContext.xml"); Dept dept = (Dept) ac.getBean ("dept"); System.out.println (dept);}

4. Spring and Hibernate integration

4.1.The SessionFactory of Hibernate is given to Spring to create

Among them, the three files of hibernate.cfg.xml, Dept.hbm.xml and Dept.java have not changed.

DeptDao.java (modified)

Package com.rk.dao;import org.hibernate.Session;import org.hibernate.SessionFactory;import com.rk.entity.Dept;public class DeptDao {private SessionFactory sf; public void setSf (SessionFactory sf) {this.sf = sf;} public Dept findById (int id) {Session session = sf.getCurrentSession (); session.beginTransaction () Dept dept = (Dept) session.get (Dept.class, id); session.getTransaction (). Commit (); return dept;} public void save (Dept dept) {Session session = sf.getCurrentSession (); session.beginTransaction () Session.save (dept); session.getTransaction (). Commit ();}}

DeptService.java (modified)

Package com.rk.service;import com.rk.dao.DeptDao;import com.rk.entity.Dept;public class DeptService {private DeptDao deptDao; public void setDeptDao (DeptDao deptDao) {this.deptDao = deptDao;} public Dept findById (int id) {return deptDao.findById (id) } public void save (Dept dept) {deptDao.save (dept);}}

ApplicationContext.xml (modified)

App.java (modified)

Package com.rk.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.rk.entity.Dept;import com.rk.service.DeptService;public class App {@ Test public void test () {ApplicationContext ac = new ClassPathXmlApplicationContext ("applicationContext.xml"); DeptService deptService = (DeptService) ac.getBean ("deptService") Dept dept = deptService.findById (3); System.out.println (dept); Dept newDept = (Dept) ac.getBean ("dept"); newDept.setDeptName ("HelloWorld"); deptService.save (newDept);}}

4.2.The transaction of Hibernate is transferred to the transaction control of Spring

Among them, Dept.hbm.xml and Dept.java did not change.

DeptDao.java (modified)

Package com.rk.dao;import org.hibernate.Session;import org.hibernate.SessionFactory;import com.rk.entity.Dept;public class DeptDao {private SessionFactory sf; public void setSf (SessionFactory sf) {this.sf = sf;} public Dept findById (int id) {Session session = sf.getCurrentSession () Dept dept = (Dept) session.get (Dept.class, id); return dept;} public void save (Dept dept) {Session session = sf.getCurrentSession (); session.save (dept);}}

DeptService.java (modified)

In the save method, int I = 1 0 is added; so an error occurs, while the save method is in a transaction, so the two pieces of data will not be saved successfully. If the error is removed, the two pieces of data can be saved successfully.

Package com.rk.service;import com.rk.dao.DeptDao;import com.rk.entity.Dept;public class DeptService {private DeptDao deptDao; public void setDeptDao (DeptDao deptDao) {this.deptDao = deptDao;} public Dept findById (int id) {return deptDao.findById (id) } public void save (Dept dept) {deptDao.save (dept); int I = 1 + 0; Dept newDept = newDept (); newDept.setDeptName ("HAHA_LLO"); deptDao.save (newDept);}}

Hibernate.cfg.xml (modified)

Only one line has been modified: delete or comment the following configuration

Thread

Otherwise, an error will be reported: org.hibernate.HibernateException: method is not valid without active transaction.

Com.mysql.jdbc.Driver jdbc:mysql:///test root root org.hibernate.dialect.MySQL5Dialect true false update

ApplicationContext.xml (modified)

App.java (no change, test)

Package com.rk.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.rk.entity.Dept;import com.rk.service.DeptService;public class App {@ Test public void test () {ApplicationContext ac = new ClassPathXmlApplicationContext ("applicationContext.xml"); DeptService deptService = (DeptService) ac.getBean ("deptService") Dept dept = deptService.findById (3); System.out.println (dept); Dept newDept = (Dept) ac.getBean ("dept"); newDept.setDeptName ("HelloWorld"); deptService.save (newDept);}}

4.3.The data source of Hibernate is handed over to Spring to create

This only involves modifying hibernate.cfg.xml and applicationContext.xml

Delete the following from hibernate.cfg.xml:

Com.mysql.jdbc.Driver jdbc:mysql:///test root root

Add dataSource and modify sessionFactory in applicationContext.xml

Complete hibernate.cfg.xml

Org.hibernate.dialect.MySQL5Dialect true false update

Complete applicationContext.xml

4.4.The configuration of Hibernate is written into the configuration of Spring.

In other words, delete the hibernate.cfg.xml and keep only the applicationContext.xml file

ApplicationContext.xml

Org.hibernate.dialect.MySQL5Dialect true false update Classpath:com/rk/entity/

4.5. Summary: final file status

ApplicationContext.xml

Org.hibernate.dialect.MySQL5Dialect true false update Classpath:com/rk/entity/

Dept.hbm.xml

Dept.java

Package com.rk.entity;public class Dept {private int deptId; private String deptName; private int deptVersion; public int getDeptId () {return deptId;} public void setDeptId (int deptId) {this.deptId = deptId;} public String getDeptName () {return deptName } public void setDeptName (String deptName) {this.deptName = deptName;} public int getDeptVersion () {return deptVersion;} public void setDeptVersion (int deptVersion) {this.deptVersion = deptVersion @ Override public String toString () {return "Dept [deptId=" + deptId + ", deptName=" + deptName + ", deptVersion=" + deptVersion + "]";}}

DeptDao.java

Package com.rk.dao;import org.hibernate.Session;import org.hibernate.SessionFactory;import com.rk.entity.Dept;public class DeptDao {private SessionFactory sf; public void setSf (SessionFactory sf) {this.sf = sf;} public Dept findById (int id) {Session session = sf.getCurrentSession () Dept dept = (Dept) session.get (Dept.class, id); return dept;} public void save (Dept dept) {Session session = sf.getCurrentSession (); session.save (dept);}}

DeptService.java

Package com.rk.service;import com.rk.dao.DeptDao;import com.rk.entity.Dept;public class DeptService {private DeptDao deptDao; public void setDeptDao (DeptDao deptDao) {this.deptDao = deptDao;} public Dept findById (int id) {return deptDao.findById (id) } public void save (Dept dept) {deptDao.save (dept); / / int I = 1apper0; Dept newDept = newDept (); newDept.setDeptName ("HAHA_LLO"); deptDao.save (newDept);}}

App.java

Package com.rk.test;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.rk.entity.Dept;import com.rk.service.DeptService;public class App {@ Test public void test () {ApplicationContext ac = new ClassPathXmlApplicationContext ("applicationContext.xml"); DeptService deptService = (DeptService) ac.getBean ("deptService") Dept dept = deptService.findById (3); System.out.println (dept); Dept newDept = (Dept) ac.getBean ("dept"); newDept.setDeptName ("HelloWorld"); deptService.save (newDept);}}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report