• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mapping issue

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to hibernate and not very clear about how mapping works.

I have a 'User' table and a 'Role' table.

User table has fields USERID, USERNAME, ROLEID.

Role table has ROLEID, ROLENAME.

User table has a foreign key reference to the 'ROLE' table (ROLEID).

I am not sure how would the mapping files and the POJO classes would go for both the USER and the ROLE table. Will they have <many-to-one> or <one-to-many> references in the mapping files (which mapping USER or ROLE).

Also, when I do session.load(User.class,userId);
how do i get the ROLE NAME associated with that user.
Just to remind USER table has a ROLE ID and not a ROLE NAME.

Please help with some code snippet for mapping files and the POJO class for the above example

This is urgent!!!

Thanks in advance..
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, in your model, Role is the one side and User is the many. So in Role you use a OneToMany mapping to User, and in User you have a ManyToOne mapping to Role.

In your load to get the RoleName, you would probably call getRole from the loaded User class, and the return of that method is a Role object, and call getRoleName from that Role object.

Mark
 
rahul khanna
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark..

I was using Eclipse and figured out a tool 'Hibernate Synchronizer'.

It is a cool thing ..
it generated all the hbm files and the POJO classes for me once mapped to the database..
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, using a tool makes it even easier.

JBoss IDE is Eclipse with JBoss Plugins including very good ones for Hibernate.

Good Luck

Mark
 
rahul khanna
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks mark for the prompt reply..
 
A magnificient life is loaded with tough challenges. En garde tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic