Given a repository and a service
// UserRepository.java
public interface UserRepository {
void createUser(User user);
void persistUser(User user);
}
// UserRepositoryImpl.java
@Repository
public class UserRepositoryImpl implements UserRepository {
@Override
public void createUser(User user) {
validateUser(user);
persistUser(user);
}
@Override
@Transactional
public void persistUser(User user) {
// actually persist the user
}
private void validateUser(User user) {
// do some validation
}
}
// UserService.java
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
// ...
// somewhere in the UserService methods
userRepository.createUser(user);
// ...
}
will the call userRepository.createUser(user)
in the UserService
create a transaction?
Follow-up
- Why?
- How to fix?
- Will your fix work if we make
persistUser
private? Why?