Hibernate Filter (with outer join) using Annotations on OneToMany relationship
posted 2 years ago
I am attempting to write a Rest Service to access some data using Hibernate. This is a little complicated to explain, so I am going to try and use an example. If you have the following tables (not a real scenario, so forgive the illogical business logic):
- Employee (Many Employees per Company)
- EmployeeProject (Really a Many To Many between Employees and Project, but treated as a 1:M from both tables)
- User (This has an optional relationship to the EmployeeProject table, i.e. the user_id (PK) is nullable in EmployeeProject - This is an important relationship to the question)
- I can read the company by name and retrieve all of the employees and in turn can read all of the projects for each employee using something like the following (Please excuse any java typos or errors since all of this is working and this is just to setup my question):
My question is, I want to add a Filter so that the results returned from the employee.getProjectEmployees() is restricted so that the only rows returned are ones in which the user is null or the user is the person executing the query. Just for clarity, I am using annotations on everything, so please restrict the answers to use annotations (unless of course this is impossible with annotations). I understand how to use the Filter, so we can skip that part, but I have been unable to figure out how to define the filter correctly. I can get simple Filters to work, but I don't know how to define a Filter that includes the User table (and in addition, an outer join to that table) in the getProjectEmployees call (since it is not a part of the access to begin with). In addition, I can't figure out which Entity the Filter should be defined on (I assume the Employee, but any reference to projectEmployee.user seems to fail). Any help would be greatly appreciated.