I have an object called User that has the following field called roles :
@ManyToMany @JoinTable( name = "user_role", joinColumns = {@JoinColumn(name = "user_id", nullable = false)}, inverseJoinColumns = {@JoinColumn(name = "role_id", nullable = false)} ) private List<Role> roles;
I load User using a service method, and the service method is wrapped in a transaction (JTA). After calling the service method and getting the User I access this role field outside of the transaction that was used to load the User object. I expected to get an error because the documentation for eclipselink states that the default fechtype for the ManyToMany lazy association. This tells me that when the User object was loaded in the service method, roles should not be automatically loaded.
Why was I able to access roles outside of a transaction? Why does it seem that roles were impatiently taken instead of the lazy?
Here is the service class that loads the user (I deleted some code that is not related to the question):
@Service @Transactional(rollbackFor = ServiceException.class) public class UserServiceImpl implements UserService { @Autowired(required = true) private UserRepository userRepository; @Override public User authenticate(String username, String password) throws ServiceException {
source share