Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate reverse engineering

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.

like image 495
EugeneP Avatar asked Oct 19 '25 14:10

EugeneP


2 Answers

Why did it not contain the foreign key as a class property?

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

And how do I join these two tables in HQL without that glue?

The glue is there, but you have to think object (and association). For example (see 14.3. Associations and joins):

from Category as category join category.user as user

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:

from Category category where category.user.id =: id
like image 60
Pascal Thivent Avatar answered Oct 21 '25 04:10

Pascal Thivent


Hibernate automatically handles the foreign key for you in this case. On the Java side, you don't need to think about foreign keys, but aggregation. That is why Category contains a (reference to a) User object, not an FK. The binding between the user property of class Category and the FK column in the USER table is specified in the Hibernate mapping.

If you then create a Criteria or a Query using these classes, Hibernate will automatically generate the SQL query using the proper FK. I don't have experience with HQL, but I am pretty sure Hibernate handles that one correctly as well.

Update: HQL example:

from Category as category inner join fetch category.user as user

Example adapted from here.

For more details on the mapping, see the Hibernate Reference, chapters 5 to 7.

like image 37
Péter Török Avatar answered Oct 21 '25 02:10

Péter Török