无需交易的JPA [英] JPA without transaction
问题描述
我是JPA的新手.我正在开发一个使用JPA(Hibernate实现)和Spring的应用程序.我已经在我的persistence.xml中声明了一个持久性单元,并在我的Spring配置文件中声明了关于EntityManagerFactory的配置.像这样:
I´m new to JPA. I´m developing an application which uses JPA (Hibernate implementation) and Spring. I´ve declared a persistence unit in my persistence.xml and configuration about EntityManagerFactory in my Spring config files. Something like this:
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="my.package" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
然后我有一些DAO,其中我在实体管理器中注入了@PersistenceContext批注:
Then I have some DAOs where I inject the entityManager with the @PersistenceContext annotation:
public MyDaoImpl implements MyDao{
private EntityManager entityManager;
@PersistenceContext
private void setEntityManager(EntityManager em){
this.entityManager = em;
}
}
最后,我有一些服务在其中注入了DAO(通过@Autowired Spring的注释):
And finally, I have some services where DAOs are injected (by @Autowired Spring's annotation):
public MyServiceImpl implements MyService{
@Autowired
private MyDao myDao;
public List<MyEntity> readOperation(){
//
return myDAo.searchAll();
}
}
由于它是只读操作,所以我认为它不需要@Transactional批注,但是如果没有它,则会有一个例外:
As its a read only operation I thought it wasn´t needed the @Transactional annotation, but without it, there is an exception:
java.lang.IllegalStateException: No transactional EntityManager available
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:223)
at $Proxy121.unwrap(Unknown Source)
我还阅读了其他类似的帖子: java .lang.IllegalStateException:没有可用的事务性EntityManager
I´ve read some other posts like this: java.lang.IllegalStateException: No transactional EntityManager available
所有所说的是事务注释是必需的.它确实可以使用它,但是我想知道(以及为什么)所有方法(甚至是只读操作)是否都必须是事务性的.
And all is said is that the transactional annotation is needed. It´s true that it works with it, but I´d like to know (and why) if all methods (even read only operations) must be transactional.
推荐答案
所有方法都需要进行JPA事务处理-本质上来说,事务处理是打开Hibernate会话的原因,而您需要一个开放的会话才能与其进行交互.
A JPA Transaction is needed for all your methods - essentially a transaction is what opens a Hibernate session, and you need an open session to interact with it.
您可以将事务注释为只读或可读写,也可以在类级别进行注释,以节省对每个方法的注释.例如:
You can annotate the transactions as readonly or readwrite, and you can also annotate at the class level to save you annotating each method. For example:
@Transactional(readOnly = true)
public MyDaoImpl implements MyDao{
private EntityManager entityManager;
@PersistenceContext
private void setEntityManager(EntityManager em){
this.entityManager = em;
}
@Transactional(readOnly = false)
public void saveItem(MyEntity entity) {
}
public List<MyEntity> searchAll() {
}
}
这篇关于无需交易的JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!