Answer the question
In order to leave comments, you need to log in
Where should HQL queries be implemented?
I started to study HQL under Spring and questions immediately began to appear :) I read various articles about HQL and they give various code options, but for some reason they don’t write where exactly it should be implemented. I understand that a lot of things should be clear if you know the basics of Spring, but I decided to clarify some points here in order to know for sure that I correctly understand how it all works. And so, as I understand it, the most suitable for Spring are named queries, since they can be written once and then simply called?
According to the logic, this part of the code should be written in the model right above the class name?
@NamedQueries({
@NamedQuery(
name = "findCompaniesWithWorkerPassport",
query = "Select c from Company as c, IN(c.workers) as w where w.passport.series = :series")
})
Java
session.getNamedQuery("findCompaniesWithWorkerPassport")
.setParameter("series", "AS")
.list()
.forEach(System.out::println);
Answer the question
In order to leave comments, you need to log in
Link to useful source: https://www.baeldung.com/hibernate-named-query
According to the logic, this part of the code should be written in the model right above the class name?
Now, when I want to find companies with an employee's passport, logically, I can call this query and I should call it in the Service class, right?
The question also arises if we use HQL and we need to write one unpopular query, which, for example, will be used only once in the entire project.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question