It's not a secret anymore!*
The moose likes Object Relational Mapping and the fly likes Need help for Many to Many association Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Need help for Many to Many association" Watch "Need help for Many to Many association" New topic
Author

Need help for Many to Many association

Philip Zac
Ranch Hand

Joined: Apr 27, 2008
Posts: 66
Hello to all javaranchers,

I have a created two entities called User and Role. A user can have many roles and a role can have many users.


User.java





Role.java



Now I got three tables. ie app_user (User) table, role table and user_role table.




Now, we know that the user_role does not have entity class (model). When I write in my DAOHibernate, I want to get access to this user_role table in order to get a list of username having role as 'user'. How can I write it in my code....



I hope I made my doubt clear to you all. So Please help . Thank you.

Philip

Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Howdy Greenhorn!

So, you're basic question is "how do I get to the join table?" Well, essentially, you don't! The join table is a database artifact, but there's really no equivalent, object oriented term or artifact in Java. That's sorta why we need Hibernate - so we can solve our problems using Java terms, technology and facilities, and not have to burden ourselves with that whole silly and non-sensical world database developers have created around their little databases.

What you want to do is find all the User's who are in a certain role, or all of the Role's a certain user is in, correct?

Well, one easy way might just be do do a criteria query using an example object. You could create a Role, User user = new User();

Then, you could set a property, such as user.setRole("Cherry Picker");

Then just perform the criteria query. It might look like this.




So, as you can see, we never actually get to the join table. Instead, we maintain an object oriented approach to our problem domain. Hibernate then translates this object oriented approach to the relational database environment.

I have a few Hibernate examples that use the criteria API on my website that might help you out. That is where I copied this code:

Hibernate Example Demonstrating How to Use the Criteria API

Here's also a little Hibernate tutorial that demonstrates some of the finer points of many-to-many mappings:

Hibernate Tutorial on Mapping Many-to-Many Associations

I hope this helps!

Kindest regards,

-Cameron McKenzie
[ May 11, 2008: Message edited by: Cameron Wallace McKenzie ]
Philip Zac
Ranch Hand

Joined: Apr 27, 2008
Posts: 66
Hello Cameron Wallace McKenzie,

Your tutorial was a great read and very informative. But my doubt rose again. Where the hell is HibernateUtil class lib? or do we have to create one?

If we have to create one, would you kindly show me one.


By the way, one of my friend told me that the best way to get rid of the trouble is to use one to many and many to one mapping..What's is your opinion?

Thanks once again.
Philip
Philip Zac
Ranch Hand

Joined: Apr 27, 2008
Posts: 66
Hello Cameron Wallace McKenzie,

Sorry about the previous reply. I just had a blind eyes. Finally found in your tutorial on how to create the HibernateUtil.class.

I am still working on it. Will inform you on it...


Thanks once again.

Philip
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

My, oh my.

You've actually caught me on what I think is one of the toughest things about learning Hibernate. It seems that every example uses a HibernateUtil class to hide the underlying plumbing of creating a Session, which is necessary in examples, but at the same time, people who are new to Hibernate are easily left with the impression that HibernateUtil is part of the API, which it is not. As you've found out, you need to create your own.

To be honest, I don't even like the name HibernateUtil. I prefer HibernateService, or something like that, but what can you do.

Here's a very, very scaled down, non-production ready, HibernateUtil class in case anyone is looking for one.




-Cameron McKenzie
[ May 12, 2008: Message edited by: Cameron Wallace McKenzie ]
Philip Zac
Ranch Hand

Joined: Apr 27, 2008
Posts: 66
Hello Cameron,

Thanks for the help. I just want your opinion. Does your book covers all the Hibernate details like the hardcore one "java persistence with Hibernate" by Christain Bauer and Gavin King. To be honest, this hardcore book is pretty much tough for beginners like me.

Anyway, Good Day.

Philip
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Originally posted by Cameron Wallace McKenzie:
To be honest, I don't even like the name HibernateUtil. I prefer HibernateService, or something like that, but what can you do.

Heh, I have a long-standing aversion to needlessly freeze-dried names like "util" and ended up naming mine HibernateValet. A bit whimsical perhaps, but then, so am I.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help for Many to Many association