Welcome 微信登录

首页 / 操作系统 / Linux / Spring注入JPA+JPA事务管理

本例实现的是Spring注入JPA 和 使用JPA事务管理。JPA是sun公司开发的一项新的规范标准。在本质上来说,JPA可以看作是Hibernate的一个子集;然而从功能上来说,Hibernate是JPA的一种实现。在web开发的过程中,使用hibernate进行数据库连接、事务等的管理。当然也可以使用JPA替换Hibernate是实现这些功能。一、使用Spring来注入JPA之前,在
  • ssh(sturts2_spring_hibernate) 框架搭建之hibernate1  http://www.linuxidc.com/Linux/2016-09/134985.htm
  • ssh(sturts2_spring_hibernate) 框架搭建之spring  http://www.linuxidc.com/Linux/2016-09/134984.htm
  • ssh(sturts2_spring_hibernate) 框架搭建之struts2  http://www.linuxidc.com/Linux/2016-09/134983.htm
  • SSH(Sturts2+Spring+Hibernate) 框架搭建之JPA代替Hibernate http://www.linuxidc.com/Linux/2016-11/137185.htm
中已经进行了hibernate的搭建和后期的是实现。了解到了,获取hibernate中的sessionFactory有两种方式。 一种是自己手动获取src目录下hibernate.cfg.xml配置文件:(在dao层面的代码) SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory(); 另外一种是使用spring注入,并且可以删除src目录下的配置文件: 在spring的配置文件applicationContext.xml文件中进行sessionFactory和dataSource的bean的配置:之后在dao层面的实现类中定义一个属性为SessionFactory,使用spring注入       同理,使用JPA也有这两中方式来是实现。先前,本人博客中已进行了JPA第一种的实现(手动书写代码读取JPA配置文件):http://www.linuxidc.com/Linux/2016-11/137185.htm,所以在此,进行第二种使用spring注入的方式是实现获取EntityManagerFactory。1、首先:在spring配置文件创建一个bean标签,目的创建一个EntityManagerFactory,这里不同于sessionFactory的bean,因为这里读取的是src目录下的META-INF目录下的persistence.xml 即JPA的配置文件,所以在下面设置的是持久化单元名"myJpa",对应JPA配置文件中的单元名字。 <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> <property name="persistenceUnitName" value="myJpa"/></bean> JPA的配置文件 :persistence.xml,这里的具体不做过多的解释,如果有疑问,请上网查找资料。<?xml version="1.0" encoding="UTF-8"?><persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"><persistence-unit name="myJpa" transaction-type="RESOURCE_LOCAL"><provider>org.hibernate.jpa.HibernatePersistenceProvider</provider><class>entity.Product</class>
<properties><property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/><property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/><property name="hibernate.connection.username" value="root"/><property name="hibernate.connection.password" value="123456"/><property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/ProductDB"/><property name="hibernate.show_sql" value="true"></property><property name="hibernate.format_sql" value="true"></property><property name="hibernate.connection.autocommit" value="false"/><property name="hibernate.hbm2ddl.auto" value="update"/></properties></persistence-unit></persistence>2、最后:在dao层面设置sessionFactory成员属性。                          在这里,可以使用spring的注解,或者是使用bean注入的方式,这里使用的spring的注入进行注入。使用spring注解需要在spring配置文件中添加一个注解的解析器:,这个@Qualifier("entityManagerFactory")注解中的entityManagerFactory对应的是spring配置文件中JPA配置的entityManagerFactory的id属性值。二、添加JPA的事务管理1、首先建立在之前使用spring注入JPA的基础之上,再加入JPA的事务管理。好处 不需要手动开启、提交事务和关闭资源,因为这些都是重复的代码,做的事情都是同一样的。所以可以交给事务来管理。首先:在spring配置文件applicationContext.xml中添加事务管理,如下 <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"/></bean><tx:annotation-driven transaction-manager="txManager"/> 其次:在dao层面加上注解@Transactional。这个注解需要加载类上。并且在这个dao类上加上一个属性:之后就可以直接使用这个entityManager进行CURD操作。这个属性: 的定义是建立在JPA的事务管理之上的,当web容器读取spring配置文件中的entityManagerFactory后会自动创建一个entityManager,然后根据在dao类中定义的这个属性进行注入,和上面的定义EntityManagerFactory不同。具体的可以自己进行尝试。 Hibernate3.1.2_中文文档PDF  http://www.linuxidc.com/Linux/2016-02/128462.htmHibernate学习入门教程  http://www.linuxidc.com/Linux/2015-08/121498.htm在Hibernate中开启日志 http://www.linuxidc.com/Linux/2015-07/120499.htmHibernate+JUnit测试实体类生成数据库表  http://www.linuxidc.com/Linux/2015-07/120161.htmHibernate整体理解 http://www.linuxidc.com/Linux/2014-07/104405.htmHibernate的映射机制  http://www.linuxidc.com/Linux/2014-12/110265.htmHibernate 的详细介绍:请点这里
Hibernate 的下载地址:请点这里本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-11/137186.htm