I'm trying to understand, how does injecting of EntityManger in Spring bean work. I have bean
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class TestUpdateService {
@PersistenceContext
private EntityManager entityManager;
private final OrderRepository orderRepository;
@Transactional
public void doWork(Long id) {
Order order = orderRepository.findById(id).get();
entityManager.detach(order);
orderRepository.save(order);
}
}
And have a couple of questions:
- Am I right that for every call of my transactional method doWork new instance of EntityManager will be created? I read (or mb misunderstood) on stackoverflow that entity manager annotated with @PersistanceContext creates own EntityManager for each transaction.
- If it is so, does @Autowired work the same way?
- If it isn't so, then how do these both annotations work?