I was asked in an interview to tell all the queries that are executed when the method below is called:
public class UserService {
@PersistanceContext
private EntityManager entityManager;
@Transactional
public void updateUser(long userId, UserDTO userDTO) {
User user = entityManager.find(userId);
if (user != null) {
user.setName(userDTO.getName());
}
}
}
Because I only see a call to entityManager.find my answer was 1. The feedback was wrong. Is it true that the find method would hit the database more than once? I assume user.setName does not hit the database because there's no explicit call to merge/persist.