假设我们有以下代码:
@Entity
public class User {
@Id
private String name;
@OneToOne(cascade = CascadeType.ALL)
private Address address;
//getters and setters
}
@Entity
public class Address {
@Id
private int id;
private String street;
//getters and setters
}
@Stateless
//@Service
public class UserLogicClass {
@PersistenceContext
//@Autowired
private EntityManager entityManager;
public void logicOnUser(User user) {
if(logicOnAddress(user.getAddress()) {
otherLogicOnUser(user);
}
}
public boolean logicOnAddress(Address address) {
//
entityManager.find(address);//address becomes managed
//
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
//@Transactional(propagation = Propagation.REQUIRES_NEW)
public void otherLogicOnUser
//
entityManager.find(user);/*without annotation, user is not managed and address is managed, but with the transaction annotation is the address still managed?*/
//
}
}
问题依赖于最后一种方法的评论;我很好奇 Spring 案例和 EJB 案例中会发生什么。假设 Spring 配置了 JTA 事务,并且从此类调用的任何方法都将启动一个新事务,就像在 EJB 中一样。