Reputation: 21
I'm new to the Play Framework, Java and Hibernate JPA so appreciate any help (come over from PHP).
My goal is to have a tree of categories. It's essentially an infinite parent/child relationship.
Each category relationship needs to have some notes attached to it that are specific to the relationship and not to the individual categories that form the relationship.
So my Join/Relationship table in the database would look like:
| id | cat1id | cat2id | data1 | data2 | order |
| 1 | 1 | 3 | stuff | more | 1 |
| 2 | 1 | 4 | fish | fun | 2 |
| 3 | 3 | 2 | ants | whoa | 1 |
I tried doing @ManyToMany in the Category class and couldn't figure out how to get the extra fields
I now created a Category class that has all the category stuff and have a CategoryTree class that has the data above. CaaegoryTree cat1id is @ManyToOne to Category and CategoryTree cat2id is @ManyToOne to Category and the OneToMany for both from Category. That all works fine for the most part but just doesn't feel right.
Also, I want to have a property in the Category class called ParentCats and ChildCats where they each are a List or Set of categories that this Category object is a parent of and a child of. I can make all this happen with a function of course but like how with more normal entity relationships that this information is so easily retrieved in this manner.
Any thoughts, guidance, or pokes in the right direction are appreciated!
Upvotes: 2
Views: 1548
Reputation: 1094
For the first part, what you actually want in the OO sense is an association class. Unfortunately, these are not supported by JPA. Check this link: ManyToMany with additional columns which is more or less what you already have.
For the second part of your question, just use a self referencing, bi-directional association:
public class Category extends Model {
@ManyToMany(mappedBy="parentCats")
public Set<Category> subCats;
@ManyToMany
public Set<Category> parentCats;
}
Upvotes: 2