• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Hibernate: association refers to an unmapped class

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Henrique Sousa
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
author
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic