Let's say we have the following piece of code:
@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?*/
//
}
}
The question relies in the comment from the last method; I am curios what happens in both Spring case and EJB case. Suppose Spring is configured with JTA transactions and any method called from this class would start a new transaction, just as in EJB.