1.不建议使用Spring的Support类。
虽然Spring提供JpaTemplate类和JpaDaoSupport类,还是建议使用DI的方式注入EntityManager。例子如下:
@PersistenceContext
private
EntityManager em;
另外需要在XML里面注册2个bean。
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBea nPostProcessor">
</bean>
<bean
class="org.springframework.dao.annotation.PersistenceExceptionTran slationPostProcessor">
</bean>
摘录JavaDoc里的相关描述:
NOTE: JpaTemplate mainly exists as a sibling of JdoTemplate and HibernateTemplate, offering the same style for people used to it. For newly started projects, consider adopting the standard JPA style of coding data access objects instead, based on a "shared EntityManager" reference injected via a Spring bean definition or the JPA PersistenceContext annotation. (Using Spring's SharedEntityManagerBean / PersistenceAnnotationBea nPostProcessor,
or using a direct JNDI lookup for an EntityManager on a Java EE 5 server.)
2.EntityManagerFactory相关的XML定义
<bean
id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property
name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property
name="url"
value="jdbc:mysql://localhost:3306/SpringStudy">
</property>
<property
name="username" value="root"></property>
<property
name="password" value="pwd"></property>
</bean>
<bean
id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityMana gerFactoryBean">
<property
name="dataSource" ref="dataSource"></property>
<property
name="jpaVendorAdapter" ref="jpaVendorAdapter"></property>
</bean>
<bean
id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapte r">
<property
name="database" value="MYSQL"></property>
<property
name="showSql" value="true"></property>
<property
name="generateDdl" value="false"></property>
<property
name="databasePlatform"
value="org.hibernate.dialect.MySQL5Dialect">
</property>
</bean>
3.JpaTransactionManager相关的XML定义,其中会引用到上面定义的EntityManagerFactory。
<bean
id="jpaDialect"
class="org.springframework.orm.jpa.vendor.HibernateJpaDialect">
</bean>
<bean
id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<property
name="entityManagerFactory" ref="emf"></property>
<property
name="jpaDialect" ref="jpaDialect"></property>
</bean>
<tx:annotation-driven
transaction-manager="transactionManager" />
通过上面的定义在BeanClass里面通过@Transactional注解就可以引入事物管理功能。
4.基于注解的autowire
<context:annotation-config></context:annotation-config>
通过上面的定义就可以实现@Autowired注解标记对象的自动注入。@Autowired是ByType注入。如果需要ByName注入可以使用@Resource注解。使用上面的定义方式被注入的bean还是需要通过XML的方式注册的。如果连bean的注册都希望通过注解的方式完成的话,使用下面的定义方式来替代。
<context:component-scan
base-package="xxx.package.name"></context:component-scan>
相关package下
摘录JavaDoc里的相关描述:
NOTE: JpaTemplate mainly exists as a sibling of JdoTemplate and HibernateTemplate, offering the same style for people used to it. For newly started projects, consider adopting the standard JPA style of coding data access objects instead, based on a "shared EntityManager" reference injected via a Spring bean definition or the JPA PersistenceContext annotation. (Using Spring's SharedEntityManagerBean / PersistenceAnnotationBea
2.EntityManagerFactory相关的XML定义
3.JpaTransactionManager相关的XML定义,其中会引用到上面定义的EntityManagerFactory。
4.基于注解的autowire
被@Component,@Service, @Controller,@Repository之中的任何一个注解标记的Class都会被自动注册成Bean。

本文介绍了Spring框架中使用JPA的最佳实践,包括推荐使用DI方式注入EntityManager而非使用Spring的Support类,配置EntityManagerFactory和JpaTransactionManager的具体XML定义,以及如何通过注解实现自动装配。
3056

被折叠的 条评论
为什么被折叠?



