File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate: association refers to an unmapped class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate: association refers to an unmapped class" Watch "Hibernate: association refers to an unmapped class" New topic
Author

Hibernate: association refers to an unmapped class

Henrique Sousa
Ranch Hand

Joined: Apr 29, 2004
Posts: 92
Hello ranchers,

I am new to Hibernate, so I'm sorry if I make some obvious mistake here. I am trying to create a many-to-many relationship between users and user roles. This must be done often for authentication and authorization, but I could not find any examples.
Anyway, I mapped both entities with these config files:

User.hbm.xml:


and Role.hbm.xml:


Seems pretty simple. And then the classes:

hsousa.User


and hsousa.Role:


When I query for User instances and Hibernate initializes, it throws the following exception:


As you can see above, I have mapped the hsousa.Role class. Then why would it still fail? Help appreciated. Regards,


Henrique Sousa<br />SCJP 1.4<br /> <br />All men die, not all men really live - Braveheart, 1995
Henrique Sousa
Ranch Hand

Joined: Apr 29, 2004
Posts: 92
I tried a guess, and got it working: I declared both classes in a single file. But shouldn't I be able to use one file per class, as recommended by Hibernate docs?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by Henrique Sousa:
I tried a guess, and got it working: I declared both classes in a single file. But shouldn't I be able to use one file per class, as recommended by Hibernate docs?


Absolutely. Perhaps you didn't add both mapping files to the Configuration you used to create the SessionFactory?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Henrique Sousa
Ranch Hand

Joined: Apr 29, 2004
Posts: 92
Oh yeah... (feeling a little like Beavis & Butt-Head right now :roll: )

I totally forgot about the config file, thanks a lot. I'll organize my XML files better now. Of course, it worked once I added the Role.hbm.xml to hibernate.cfg.xml. duh
[ June 09, 2005: Message edited by: Henrique Sousa ]
Nanda Suraj
Greenhorn

Joined: May 04, 2005
Posts: 8
hi Henrique ,

I have got the same exception. I have given both the mapping inside the hibernate.cfg.xml. But it still shows the exception :

An association from the table refers to an unmapped class: java.lang.ExceptionInInitializerError

have any idea ?
David Bridgewater
author
Ranch Hand

Joined: Apr 29, 2004
Posts: 44
Hi Henrique...
I found your question and posted code enormously helpful.
Now that you are six months on with Hibernate, I'm hoping you might be able to help me with two questions on the "Users and Roles" set up.
(1) What is the best way to answer this question: which users belong in the manager role? (I think I can see my way through on this one - have a set for users defined on Role; just the reverse of what you already have for User).
(2) Trickier: what is the best way to get a list of users belonging both to the manager AND the systemadmin role? If I were writing the SQL directly I would have no problem... but how best to make Hibernate do the work?
Any thoughts you have would be gratefully acknowledged!
Many thanks,
David Bridgewater.
Henrique Sousa
Ranch Hand

Joined: Apr 29, 2004
Posts: 92
Hello, David

You will be able to get through with your issues with HQL. In case you didn't use, it is quite straightforward and I find it easier than SQL.

(1) Yes, you can have a set. Just make sure you have reverse="true". Another option is this simple query: "from User u where :role in u.roles". You can get further info from Hibernate HQL documentation.


I think you get the way I'm going, right? You cannot use a set for (2), but HQL will certainly do the trick for you. Just add a clause to the query and you have what you need.

I would just like to finish making it clear that this may not be the best way to do this; but it certainly is one way. Best regards
[ February 07, 2006: Message edited by: Henrique Sousa ]
David Bridgewater
author
Ranch Hand

Joined: Apr 29, 2004
Posts: 44
Thanks for that, Henrique.

I think your example code should read "from User where :role in elements(roles)", but I got the picture just fine from what you said. And sure enough, when I extend the code to read something like:

Query query = session.createQuery("from Roles where description = 'manager'");
Role manager = (Role)query.uniqueResult();
query = session.createQuery("from Roles where description = 'systemadmin'");
Role systemadmin = (Role) query.uniqueResult();
query = session.createQuery("from User where :manager in elements(roles) and :systemadmin in elements(roles)");
query.setEntity("manager", manager);
query.setEntity("systemadmin", systemadmin);
List users = query.list();

... it works just fine. (Apologies if the code above doesn't work as written - my real example is in a different business domain, so I'm translating - I think it's roughly right).
The resulting SQL doesn't even look that disgusting - there are a couple of subselects involved, but it's much as I would think of handcoding. The SQL may not be to everyone's taste, but then that's part of the fun of an O/R framework (increasing your DBA's blood pressure...)

Best,

David.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate: association refers to an unmapped class