Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top