-->
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.  [ 2 posts ] 
Author Message
 Post subject: Writing Composite Primary Key in hbm.xml file and mapping it
PostPosted: Thu Sep 25, 2008 8:29 am 
Newbie

Joined: Mon Sep 08, 2008 2:37 am
Posts: 6
Hi,

i Have a table Called UserRoles. In this Table i am Having compositeprimarykey(UserId,RoleCode).
UserId,Rolecode is also Foreign Keys Which Refers to OtherTables called Users and Roles Respectively.

Code:
class Userroles :EntityBase
    {
        private User user;
        private Roles role;
        //private bool _isActive;
        private string _createdBy;
        private DateTime _createdDateTime;
        private string _updatedBy;
        private DateTime _updatedDateTime;

        [DataMember]
        public virtual User User
        {
            get
            {
                //if (addresstype == null)
                //{
                //    addresstype = new AddressType();
                //}
                return user;
            }
            set
            {
                if (user == value) return;
                user = value;
                Notify("User");
            }
        }

        [DataMember]
        public virtual Roles Roles
        {
            get
            {
                //if (addresstype == null)
                //{
                //    addresstype = new AddressType();
                //}
                return role;
            }
            set
            {
                if (role == value) return;
                role = value;
                Notify("Roles");
            }
        }
}

public class User : EntityBase
    {  // Some Properties Here}
public class Roles : EntityBase
    { //Some Properties Here}



Code:
UserRoles.hbm.xml

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Core.Modules.Security.DomainModel.Userroles, Core" table="SYSUSERROLES" lazy="false">
    <composite-id>
      <key-property name="_user" column="USERCODE"/>
      <key-property name="_role" column="ROLECODE"/>
    </composite-id>
<many-to-one name="User" class="Core.Modules.Security.DomainModel.User, Core" column="USERCODE"/>
    <many-to-one name="Roles" class="Core.Modules.Security.DomainModel.Roles, Core" column="ROLECODE"/>
  </class>
</hibernate-mapping>



Is this mapping Correct?

_________________
loknath


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2008 9:49 am 
Regular
Regular

Joined: Tue Jul 29, 2008 3:30 am
Posts: 74
I think you are searching for this:
Code:
<composite-id>
    <key-many-to-one name="User" column="USERCODE"/>
    <key-many-to-one name="Role" column="ROLECODE"/>
</composite-id>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.