有没有可能让Spring将JPA entityManager对象注入我的DAO类而不扩展JpaDaoSupport?如果是,那么Spring会在这种情况下管理交易吗?
我试图让Spring配置尽可能简单:
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="em"/>
</bean>
<bean id="em" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="myPU"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
ska*_*man 32
是的,虽然它充满了陷阱,因为JPA有点特殊.非常值得阅读有关注入JPA的文档,EntityManager并且EntityManagerFactory在代码中没有明确的Spring依赖项:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/orm.html#orm-jpa
这允许您直接注入EntityManagerFactory,或者EntityManager直接注入线程安全的事务代理.后者使代码更简单,但意味着需要更多的Spring管道.
Pas*_*ent 12
是否有可能让spring将JPA entityManager对象注入我的DAO类whitout,扩展JpaDaoSupport?如果是的话,在这种情况下Spring会管理交易吗?
这在12.6.3中以白色记录为黑色.基于普通JPA实现DAO:
可以使用注入的
EntityManagerFactory或者不使用任何Spring依赖项来编写针对普通JPA的代码EntityManager.请注意,如果 启用了Spring,Spring可以在字段和方法级别理解@PersistenceUnit和@PersistenceContext注释PersistenceAnnotationBeanPostProcessor.相应的DAO实现可能看起来像这样(...)
关于交易管理,请看12.7.交易管理:
Spring JPA允许配置
JpaTransactionManager为将JPA事务公开给访问相同JDBC DataSource的JDBC访问代码,前提是已注册JpaDialect支持检索基础JDBC连接.开箱即用,Spring为Toplink,Hibernate和OpenJPA JPA实现提供方言.有关该JpaDialect机制的详细信息,请参阅下一节.
| 归档时间: |
|
| 查看次数: |
141856 次 |
| 最近记录: |