Reputation: 12278
Basically I'm trying to get an entity that has a LAZY relation to another entity. Below are 2 things I tried. The first one works, the second one does not and I don't understand why. All I want is to get the entity from database. The reason why I put this in other method is I don't want the first one to be @Transactional because it can take some time to execute. Note that I'm not saving or even accessing the database again in the first method, I just need to read from db once.
Method 1 (Works as expected):
@Override
@Transactional
public void sendEmailToUser(Long exhibitorId) {
EmailExhibitorTO exhibitorTO = findExhibitorById(exhibitorId);
}
private EmailExhibitorTO findExhibitorById(Long id){
return converter.convert(exhibitorRepository.findById(id), EmailExhibitorTO.class);
}
Everything here is fine, I'm getting the entity and the lazy initialized entity as well.
Method 2 (Doesn't work):
@Override
public void sendEmailToUser(Long exhibitorId) {
EmailExhibitorTO exhibitorTO = findExhibitorById(exhibitorId);
}
@Transactional
private EmailExhibitorTO findExhibitorById(Long id){
return converter.convert(exhibitorRepository.findById(id), EmailExhibitorTO.class);
This however does not work. Error:
There's a mapping exception but that's because lazy entity could not be fetched.
I'm probably just being stupid but if theres something I don't understand, please explain.
Thanks in advance.
Upvotes: 3
Views: 1925
Reputation: 79
you cannot propagate transactions on private methods, you can see it here: Does Spring @Transactional attribute work on a private method?
Upvotes: 0
Reputation: 4056
The @Transactional
in your private method has no effect because you are calling it from another method of the class, bypassing the Proxy that handles the transaction.
Upvotes: 5