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