1
我使用的是Glassfish v3.1上託管的Spring + JSF + JPA配置。 我正在經歷@Transactional註解的一些奇怪現象(至少對我而言)。這是我簡單的例子:Spring @Transactional - 事務問題中的JPA上下文
@Transactional
public void associateGroupToRole(String role, String group) throws MyServiceException {
GroupEntity groupEntity = userDao.getGroupByName(group);
RoleEntity roleEntity = userDao.getRoleByName(role);
//some stuff
if(!roleEntity.getGroups().contains(groupEntity)) {
roleEntity.getGroups().add(groupEntity);
}
}
@Transactional
public void associateGroupToRole(RoleEntity roleEntity, GroupEntity groupEntity) throws MyServiceException {
//some stuff
if(!roleEntity.getGroups().contains(groupEntity)) {
roleEntity.getGroups().add(groupEntity);
}
}
事實證明,「associateGroupToRole」以實體作爲參數正常工作和一個用字符串 - 不。小的修改和應對代碼從一個方法到另一個後:
@Transactional
public void associateGroupToRole(String role, String group) throws MyServiceException {
GroupEntity groupEntity = userDao.getGroupByName(group);
RoleEntity roleEntity = userDao.getRoleByName(role);
if(!roleEntity.getGroups().contains(groupEntity)) {
roleEntity.getGroups().add(groupEntity);
}
}
代碼運行沒有任何問題,一切都致力於數據庫。我的問題是:在上面的例子中,什麼可能是錯誤的,事務上下文發生了什麼(從一個註釋方法訪問另一個方法時),以及爲什麼我的實體不再處於受管狀態?
這裏是我的Spring配置:
<context:annotation-config />
<context:component-scan base-package="com.mypackage"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
</property>
</bean>
<tx:jta-transaction-manager/>
<tx:annotation-driven/>
正如你可以看到我使用persistence.xml文件和我的EntityManager使用JNDI連接到數據庫。
我沒有看到第一個和第二個片段之間的區別。 – Bozho 2011-03-24 21:29:33