I'm wondering about a case, which I encountered recently. Suppose we have a service method as follows:
@Override
public User addUser(UUID organizationId, User user) {
Organization organization = organizationRepository.findById(organizationId).orElseThrow(IllegalArgumentException::new);
user.setOrganization(organization);
organization.getUsers().add(user);
invitationService.deleteByUserEmail(user.getEmail());
return user;
}
Organization is provided by a repository method:
@Repository
public interface OrganizationRepository extends JpaRepository<Organization, UUID> {
Optional<Organization> findById(UUID id);
}
Adding of users works only because invitationService.deleteByUserEmail(user.getEmail());
is present. The entire method belongs to another service and looks as follows:
@Override
@Transactional
public void deleteByUserEmail(String userEmail) {
invitationRepository.deleteByUserEmail(userEmail);
}
I am not sure if I correctly understand why adding a user works. According to Hibernate Docs, automatic updates occur only, when an entity is in managed state and the update takes place in a single transaction. If my reasoning is correct there are the following steps which occur:
Is it possible despite of addUser method is non-transactional ?
On one of you configuration classes you may have special filters registered which basically keep the session / entitymanager opened for the life of an entire request:
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
or
org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.