Does a new transaction remove all previous objects?

Say we have the following code snippet:

@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 is based on comments from the latter method; I'm curious what happens in both the Spring case and the EJB case. Suppose Spring is configured with JTA transactions, and any method called from this class will start a new transaction, as in EJB.

+5
source share
2 answers

This is more of a JPA problem. The Manager entity does not apply to a new transaction unless you have expanded it:

@PersistenceContext(type = PersistenceContextType.EXTENDED)
//@Autowired
private EntityManager entityManager;

Quote from the JPA 2.0 specification:

, , , PersistenceContextType, , . , .

+5

. , "" , , . , "", , , , @Transactional!

otherLogicOnUser() .

: EJB -

+5

All Articles