Answer the question
In order to leave comments, you need to log in
Can a Service have repositories of other classes?
Let's say I have Entity A and Entity B. Can I directly access the Entity A repository from Service B? Or should I do it like this Service B -> Service A -> Repository A?
The problem is that I have all DTO services, and Service B needs normal objects to work, what should I do in this case? Is it really possible to create separate exactly the same methods simply without an envelope in the DTO?
Answer the question
In order to leave comments, you need to log in
Can I directly access the Entity A repository from Service B?
Or should I do it like this Service B -> Service A -> Repository A?
Optional<T>
from the repository and if the object is not found, then at the service level you throw an exception orElseThrow()
. Well, then ExceptionHandler catches the exception and gives the appropriate error code and message to the front. ServiceA {
@Autowired
RepoB repoB;
@Autowired
ServiceB serviceB;
// 1 вариант
List<Job> doSomeJob1(String email){
User user = repoB.findUserByEmail(email).orElseThrow(UserNotFoundException::new);
return user.getJobs();
}
// 2 вариант
List<Job> doSomeJob2(String email) {
User user = serviceB.getUserByEmail(email);
return user.getJobs();
}
RepoB {
Optional<User> findUserByEmail(String email);
}
ServiceB {
@Autowired
RepoB repoB;
User getUserByEmail(String email){
return repoB.findUserByEmail(email).orElseThrow(UserNotFoundException::new);
}
}
The problem is that I have all DTO services, and Service B needs normal objects to work, what should I do in this case? Is it really possible to create separate exactly the same methods simply without an envelope in the DTO?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question