문제

I am developing some application and I am using two JPA repository.

public interface AuthorityRepository extends JpaRepository<Authority, Integer>{
    }

and

public interface UserRepository extends JpaRepository<User, Integer> {
}

my Question is: What is better in service layer: for one repository have one service layer or for multiple repository have one service layer. Only one difference will be in transaction (or not?).

So is better have one transaction for one save or multiple save for one transaction.

@Transactional
    public void createUser(User user) {
        // TODO Auto-generated method stub
        userRepository.save(user);
    }

or

    @Transactional
public void createUser(User user) {
    // TODO Auto-generated method stub
    userRepository.save(user);
    authRepository.save(user.getAuth())
}    

Thanks for help

도움이 되었습니까?

해결책

If you want all-or-nothing behaviour on the two saves, they need to be called in the same transaction. Your service layer is the place to define transactional units of work, so the second option is the normal way to do it.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top