I have the three entities Person, Task and Note that should behave as follows:
- One person can have multiple tasks
- One task can be assigned to multiple persons
- Each person can have for a task multiple notes attached.
Based on this I have the following tables:
Person Task Person_Task Note Person_Task_Note
------- ---- ----------- ----------- ----------------
id id id id id
name name person_id description person_task_id
task_id note_id
with the folowing relationships:
- Many-To-Many between
PersonandTaskusingPerson_Taskas join table - One-To-Many between
Person_TaskandNoteusingPerson_Task_Noteas join table
Now I’m trying to map this structure using JPA2 with Hibernate.
My goal would be to have in the code only the three base entities with the following options:
- Get all tasks for a person (
person.getTasks()) - Get all notes for a person’s task (
task.getNotes())
I am aware that I can map every single table to a class and then then retrive the necessary info with a fat JPA criteria query.
However, I would like to avoid (if possbile) creating classes for the join tables.
If I map the relationship between Person and Task using @ManyToMany I don’t currently see how I can proceed further to retrive the Note entries
Is it possbile to do this kind of mapping just by using the JPA annotations without writing classes for the join tables?
No, it’s not possible. You’ll have to map all the tables, and transform your ManyToMany between Person and Task into two OneToMany associations.