New to JPA is relatively new, so I have a kind of architectural question. Let's say that I work for a relationship with many employees in the table and the department (i.e., many employees work for one department):
Employee EMPLOYEE_ID EMPLOYEE_NAME DEPARTMENT_ID Dept. DEPARTMENT_ID DEPARTMENT_NAME < / Code> So I can define the appropriate institutions for the employee and the department, there is no problem. Select Employee E.
D.DEPARTMENT_NAME, (Select COUNT (* *): However, in a scenario, I think that the number of employees working for the department With E.DEPARTMENT_ID = D.DEPARTMENT_ID) from the NUMBER_OF_EMPLOYEES department D I'm just not sure the correct strategy to complete using this JPA What is it for ... I do not want to always be the employees for the department unit To bring not the number, because it is only a scene when needed.
It seems that being a Hibernate @ Formula will be a possible perspective, but APAC does not conform to the JPA standard.
You can create an object in your QL using the "new" syntax - your class only A constructor is required that returns your query returned.
Like you, QL can use some: (D.DEPARTMENT_NAME, Count (E.id)) D.EPARTMENT_NAME by D.employees E Group from Department D.DEPARTMENT_NAME or you can easily put the query result in any number by selecting the Count (*) now.
Alternatively, to do this without the department staff class, you can skip the new, therefore:
SELECT D.DEPARTMENT_NAME, count (E. Id) th list & lt; Object [] & gt; , where each list item was an array of 2 elements, department name and count. To answer your later question in the comments, you must populate all areas of the department and one transient employee in the Potato Row, a suggestion 2 question. It will still be more efficient than your original question (one sub-category for each employee number).
Then a question to read the departments
from the department D to SELECT D code> Lt; Department & gt;
Then a second query is returning a temporary array:
SELECT D.DEPARTMENT_ID, the counting department D (E.id) left D. Employees E Group joining D.DEPARTMENT_ID You can get a from the list & lt; Object [] & gt; With DEPARTMENT_ID and count it. Then you use the second list to update the transient count property on your first list. (You can try to select this map to make this lookup easier, but I think this is the hibernate feature).
Comments
Post a Comment