-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Retrieving data from composit table
PostPosted: Fri Oct 03, 2008 12:15 am 
Newbie

Joined: Tue Sep 09, 2008 3:20 am
Posts: 17
Hi,

I have two tables User and Role and a composite table UserRole having primary key ad UserId,RoleId and foreign keys to UserId and RoleId.

I have created classes and mapping files as billow, and retrieve roles for user using many-to-many relation,

Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Core.Modules.Security.DomainModel.User, Core" table="SYSUSER" lazy="false"  >
    <id name="_userCode" column="USERCODE" access="field" unsaved-value="0">
      <generator class="assigned" />
    </id>
    <property name="_userFullName" column="USERFULLNAME" access="field"/>
    
    <property name="_userTypeNum" column="USERTYPENUM" access="field"/>
      <bag name="_role" access="field" table="USERROLES" lazy="false" inverse="true" cascade="all" collection-type="Core.HelperClasses.CustomCollections.ObservableList`1[[Core.Modules.Security.DomainModel.Role, Core]], Core">
      <key column="USERCODE" />
      <many-to-many class="Core.Modules.Security.DomainModel.Role, Core" column="ROLECODE"/>
    </bag>
  </class>
</hibernate-mapping>


<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="Core.Modules.Security.DomainModel.Role, Core" table="SYSROLE" lazy="false">
      <id name="_id" column="ROLECODE" access="field" unsaved-value="0">
         <generator class="assigned" />
      </id>
      <property name="_roleDesc" column="ROLEDESC" access="field" not-null="false"/>
   </class>
</hibernate-mapping>


This works fine and gets me user with all hos roles. Now I have a column called IdDefault in UserRole table which tells me what is the default role of the user. How do i retrieve this data from the UserRole table since i don't have any mapping to this table

Thanks

Arvind


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 03, 2008 2:25 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
You have to create a mapping for that table if you have additional columns in it.
So create a UserRole entity and a mapping for it and change your many-to-many mappings to one-to-many mappings.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 03, 2008 4:22 am 
Newbie

Joined: Tue Sep 09, 2008 3:20 am
Posts: 17
Hi,

There is no direct relation between the User and Role table I have to access the roles for a user thow the UserRoles table if i add a one-to-may it does not retrieve the roles for the users.

Thanks

Arvind


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 03, 2008 4:50 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
You have to map it like the foreign keys in the database:

User -> one-to-many -> UserRole
Role -> one-to-many -> UserRole
UserRole -> many-to-one -> User
UserRole -> many-to-one -> Role

Of course you don't need all that relations. Just the ones you need in your code.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 03, 2008 5:00 am 
Newbie

Joined: Fri Sep 26, 2008 4:01 am
Posts: 10
Quote:
This works fine and gets me user with all hos roles. Now I have a column called IdDefault in UserRole table which tells me what is the default role of the user. How do i retrieve this data from the UserRole table since i don't have any mapping to this table



Buddy,the default role of the user must be field in the user table , may be a foriegn key to role tab.

Think there s a crack in the schema..
Check out..

Dont forget to rate


Top
 Profile  
 
 Post subject: Have a lokk at this --
PostPosted: Fri Oct 03, 2008 5:15 am 
Newbie

Joined: Fri Sep 26, 2008 4:01 am
Posts: 10
Have a look at this

[url]

http://www.hibernate.org/hib_docs/refer ... pping.html

[/url]

see

5.1.7. composite-id


Top
 Profile  
 
 Post subject: Have a lokk at this --
PostPosted: Fri Oct 03, 2008 5:16 am 
Newbie

Joined: Fri Sep 26, 2008 4:01 am
Posts: 10
Have a look at this

[url]

http://www.hibernate.org/hib_docs/refer ... pping.html

[/url]

see

5.1.7. composite-id


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.