I’ve got a class called “Domain”. Each Domain can have multiple sub-domains (of the same type).
I need to be able to determine sub-domains and root-domains. Sub-domains can have sub-domains themself. This can be quite a few levels deep.
Example:
Rootdomain
|- Subdomain 1
| |- Subdomain 2
| |
| |- Subdomain 3
|
|- Subdomain 4
| |- Subdomain 5
How do I model such a Java-class with Hibernate annotations?
The modelling would be quite simple:
Note that
parentis the property responsible for the database entry, i.e. you need to setparentfor a subdomain for the relation to be stored.What is not quite trivial is the queries, since SQL (and thus HQL and JPQL) doesn’t easily support tree queries. Hibernate can do this by lazily loading the next level but if you want to load a bunch of levels in one query, that’s where it becomes hard.