首先,在做关于JpaTransactionManager之前,先对Jpa做一个简单的了解,他毕竟不如hibernate那么热门,其实二者很相识,只不过后期hibernate和JDO 版本都已经兼容了其Jpa,目前大家用的少了。
JPA全称Java Persistence API.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
JPA的宗旨是为POJO提供持久化标准规范,由此可见,经过这几年的实践探索,能够脱离容器独立运行,方便开发和测试的理念已经深入人心了。Hibernate3.2、TopLink 10.1.3以及OpenJPA都提供了JPA的实现。
JPA的总体思想和现有Hibernate、TopLink、JDO等ORM框架大体一致。总的来说,JPA包括以下3方面的技术:
ORM映射元数据
JPA支持XML和JDK5.0注解两种元数据的形式,元数据描述对象和表之间的映射关系,框架据此将实体对象持久化到数据库表中;
API
用来操作实体对象,执行CRUD操作,框架在后台替我们完成所有的事情,开发者从繁琐的JDBC和SQL代码中解脱出来。
查询语言
这是持久化操作中很重要的一个方面,通过面向对象而非面向数据库的查询语言查询数据,避免程序的SQL语句紧密耦合。
下面,做一些JPA简单的例子,让大家熟悉一下JPA的配置:
1.productDao.java
package com.spring.jpa;
import com.spring.model.Product;
public interface ProductDao {
public void save(Product p);
}
2.productDaoImpl.java
package com.spring.jpa;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.spring.model.Product;
public class ProductDaoImpl implements ProductDao {
public void save(Product p) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("SimplePU");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(p);
em.getTransaction().commit();
emf.close();
}
}
3.ProductManager.java
package com.spring.jpa;
import com.spring.model.Product;
public interface ProductManager {
public void save(Product p);
}
4.ProductManagerImpl.java
package com.spring.jpa;
import com.spring.model.Product;
public class ProductManagerImpl implements ProductManager{
private ProductDao productDao = new ProductDaoImpl();
@Override
public void save(Product p) {
productDao.save(p);
}
}
5.persistence.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- 切记,该文件一定要放在/WEB-INF/classes/META-INF这里才能生效 --> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> <persistence-unit name="SimplePU" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>com.spring.model.Product</class> <properties> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /> <property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1:3306/jinhonglun" /> <property name="hibernate.connection.username" value="root" /> <property name="hibernate.connection.password" value="root" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <property name="hibernate.use_sql_comments" value="false" /> <property name="hibernate.hbm2ddl.auto" value="update" /> </properties> </persistence-unit> </persistence>6.SimpleSpringJpaDemo.java
package com.spring.jpa;
import com.spring.model.Product;
public class SimpleSpringJpaDemo {
public static void main(String[] args) {
Product p = new Product();
p.setProductTitle("JPA测试");
new ProductManagerImpl().save(p);
}
}
接下来进行主题介绍JpaTransactionManager:* 我们引入 Spring,以展示 Spring 框架对 JPA 的支持。业务层接口 ProductManager 保持不变,ProductManagerImpl 中增加了三个注解,
* 以让 Spring 完成依赖注入,因此不再需要使用 new 操作符创建 ProductDaoImpl 对象了。同时我们还使用了 Spring 的声明式事务:
1.ProductDaoImpl.java
package com.spring.jpaTransactionManager;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.spring.model.Product;
@Repository("productDao")
public class ProductDaoImpl implements ProductDao {
@PersistenceContext
private EntityManager em;
@Transactional
public void save(Product p) {
em.persist(p);
}
}
2.ProductManagerImpl.java
package com.spring.jpaTransactionManager;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.spring.model.Product;
@Service("productManager")
public class ProductManagerImpl implements ProductManager{
@Resource
private ProductDao productDao;
@Transactional
@Override
public void save(Product p) {
productDao.save(p);
}
}
3.SimpleSpringJpaDemo.java
package com.spring.jpaTransactionManager;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.model.Product;
public class SimpleSpringJpaDemo {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/com/spring/jpaTransactionManager/spring-jpa.xml");
ProductDao productDao = ctx.getBean("productDao", ProductDao.class);
Product p = new Product();
p.setProductTitle("JPA Spring与JpaTransactionManager结合测试");
productDao.save(p);
}
}
4.spring-jpa.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 自动扫描包,自动将@Repository、@Service、@Controller 和 @Component自动实例化 --> <context:component-scan base-package="com.spring.jpaTransactionManager" /> <!-- Spring 事务配置,声明式事务 --> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <!-- LocalContainerEntityManagerFactoryBean 会自动检测 persistence units , 实际上,就是META-INF/persistence.xml(/WEB-INF/classes/META-INF) 文件和web.xml中的persistence-unit-ref, 以及定义的environment naming --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> </bean> </beans>