Forums Register Login

Hibernate Filter (with outer join) using Annotations on OneToMany relationship

+Pie Number of slices to send: Send
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):
- Company
- Employee (Many Employees per Company)
- Project
- 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.
He was expelled for perverse baking experiments. This tiny ad is a model student:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1846 times.
Similar Threads
Hibernate: table per class inheritance
Hibernate: cascade deletion of a many-to-many relationship
question about Enity mapping
composite-id, one-to-one, formula, and an outer join
Doubt regarding joins in SQL...
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 20:14:02.