File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Hibernate (HQL) and many-to-many issues Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate (HQL) and many-to-many issues" Watch "Hibernate (HQL) and many-to-many issues" New topic

Hibernate (HQL) and many-to-many issues

Michael Stringham

Joined: Nov 07, 2008
Posts: 1
I am using the "petclinic" app to learn more about Hibernate and Spring MVC (Hibernate3, and Spring 2.5). It works beautifully, and now I'm customizing/extending this app...

Simply: I have a USER table (tt_user) and a GROUP table (tt_group), and a USERGROUP many-to-many relationship table (tt_usergroup).

I want a list of users that have not yet been added to a given group. Here is the SQL:

My HQL conversion went something like this:

I realized that I may need to have an object to represent the tt_usergroup table, so I created UserGroup. But do I need to? Up until now I haven't needed an explicit object for this table...

Here are my hibernate mappings for User and Group:

At this point, I tried to create a class mapping for the UserGroup object/table:

but that didn't work... it has 2 keys (the only 2 fields in the table...)

Perhaps this approach is wrong?

[ November 07, 2008: Message edited by: Michael Stringham ]
[ November 07, 2008: Message edited by: Michael Stringham ]
Luis Casanueva

Joined: Dec 09, 2008
Posts: 1

String sql = = "select user from User as user where "+groupID+" not in elements (user.groupsInternal)";

No need to have an object to represent the tt_usergroup table

I found this in the book Harnessing Hibernate. Highly recomended


Luis Casanueva
I agree. Here's the link:
subject: Hibernate (HQL) and many-to-many issues
Similar Threads
Hibernate - Using Join table - one-to-many association - Lazy Exception
Hibernate - Force foreign key lookup on insert
Delete/update associations question
Hibernate Association table - extra column
Hibernate Mapping (Many-To-One Association). Problem. This forum is my last hope.