I have a structure where the main table is USER, other tables include CATEGORY (contains user_id).
What I got after the standard reverse engineering procedure was:
- the class User contained a collection of categories,
- the class Category didn’t contain the foreign key (user_id) but it did contain the User object.
Why did it not contain the foreign key as a class property?
And how do I join these two tables in HQL without that glue?
HQL – please explain this part.
Because Hibernate is an Object-Relational Mapping tool, Hibernate allows to work with an object model (i.e. objects and relations between objects) and map them to the database representation (data rows of tables). The whole point of Hibernate is to bridge the gap between the object-oriented paradigm and the relational paradigm (the famous Object-Relational Impedance Mismatch). And in your case, this following object model is the expected (and right) object representation:
alt text http://img251.imageshack.us/img251/6335/110b578b.png
The glue is there, but you have to think object (and association). For example (see 14.3. Associations and joins):
Note that HQL supports two forms of association joining: implicit and explicit (see 14.4. Forms of join syntax). The above example uses an explicit join. The implicit form doesn’t use the join keyword but a dot-notation: