-->
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.  [ 5 posts ] 
Author Message
 Post subject: compositeid attributes mapping
PostPosted: Tue Sep 12, 2006 2:42 am 
Newbie

Joined: Tue Sep 12, 2006 2:21 am
Posts: 1
May I know how I can create a composite id using the NHibernate.Mapping.Attributes.CompositeId and NHibernate.Mapping.Attributes.KeyProperty? I've tried with the following codes, but it don't seems to work:

[CompositeId(1)]
[KeyProperty(2, Name = "Code", Column = "`Code`")]

public string Code
{
get { return _code; }
set { _code = value; }
}
[KeyProperty(3, Name = "CommunityID", Column = "`CommunityID`", Access = "property")]

public string CommunityID
{
get { return _communityID; }
set { _communityID = value; }
}

both Code and CommunityID is the key for that particular object. May I know the right way of doing this, in order to get the mapping as follows:

<composite-id>
<key-property name="Code" column="`Code`" />
<key-property name="CommunityID" column="`CommunityID`" />
</composite-id>

Thanks!


Top
 Profile  
 
 Post subject: Pack them up together
PostPosted: Tue Mar 27, 2007 11:56 am 
Newbie

Joined: Tue Mar 27, 2007 11:52 am
Posts: 1
Hi,

I had the same issue. I've succeeded by putting them all at the top of my file instead of decorating the fields.


[CompositeId(1)]
[KeyProperty(2, Name = "Code", Column = "`Code`")]
[KeyProperty(3, Name = "CommunityID", Column = "`CommunityID`", Access = "property")]

public string Code
{
get { return _code; }
set { _code = value; }
}




Instead of:
[CompositeId(1)]
[KeyProperty(2, Name = "Code", Column = "`Code`")]

public string Code
{
get { return _code; }
set { _code = value; }
}
[KeyProperty(3, Name = "CommunityID", Column = "`CommunityID`", Access = "property")]

Have fun :-)


Top
 Profile  
 
 Post subject: Did you make it work?
PostPosted: Mon Feb 11, 2008 2:50 pm 
Newbie

Joined: Tue Feb 05, 2008 8:20 pm
Posts: 5
Hi,
Did you make it work?
I am doing some test with attributes and composite-id and i havenĀ“t been able to make it work, because I can't find a documentation of how to implement it.
1. I don't know what attribute to use in my class containing the Primary Key.[class] or [component]?
2. Which attribute should I use when I make a reference to the Primary class [CompositeElement]?
This is what I have, but it is not working:

Code:
   
[Serializable]
    [Class(Name = "Id")]
    public class SonPK
    {
       
        public SonPK()
        { }

       
        [NHibernate.Mapping.Attributes.CompositeId(1)]       
        [KeyManyToOne(2, Name = "Mother", Class = "Mother", Column = "MotherId")]
        [KeyManyToOne(3, Name = "Father", Class = "Father", Column = "FatherId")]       
        [KeyProperty(4, Name = "IdSon", Column = "SonId")]
       
        private int idSon;
        public virtual int IdSon
        {
            get { return idSon; }
            set { idSon = value; }
        }

        private Mother mother;       
        public virtual Mother Mother
        {
            get { return mother; }
            set { mother = value; }
        }

        private Father father;       
        public virtual Father Father
        {
            get { return father; }
            set { father = value; }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            SonPK SonPKObj = obj as SonPK;
            if (SonPKObj == null)
                return false;

            if ((this.idSon == SonPKObj.IdSon) && (this.mother.MotherId == SonPKObj.mother.MotherId) && (this.father.FatherId == SonPKObj.father.FatherId))
            {
                return true;
            }

            return false;
        }
    }
    [Class(Table = "Sons")]
    public class SonHer
    {
        public SonHer()
        {
            id = new SonPK();
        }

        private SonPK id;

     
        [NHibernate.Mapping.Attributes.CompositeElement(ClassType = typeof(SonPK))]       
        public virtual SonPK Id
        {
            get { return this.id; }
            set { this.id = value; }
        }
    }


Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 19, 2008 5:18 am 
Newbie

Joined: Tue Feb 19, 2008 5:13 am
Posts: 2
The key to making this work, which I discovered by coincidence" is to define "KeyProperty" before defining "KeyManyToOne"


So instead of this:

[NHibernate.Mapping.Attributes.CompositeId(1)]
[KeyManyToOne(2, Name = "Mother", Class = "Mother", Column = "MotherId")]
[KeyManyToOne(3, Name = "Father", Class = "Father", Column = "FatherId")]
[KeyProperty(4, Name = "IdSon", Column = "SonId")]

Do this:

[NHibernate.Mapping.Attributes.CompositeId(1)]
[KeyProperty(2, Name = "IdSon", Column = "SonId")]
[KeyManyToOne(3, Name = "Mother", Class = "Mother", Column = "MotherId")]
[KeyManyToOne(4, Name = "Father", Class = "Father", Column = "FatherId")]


...works for me!


Top
 Profile  
 
 Post subject: Attributes and Composite-Id
PostPosted: Tue Feb 19, 2008 2:38 pm 
Newbie

Joined: Tue Feb 05, 2008 8:20 pm
Posts: 5
Thanks for your reply,
I still have an error:
Code:
Unknown entity class: IIDEA.EALIB.AplicacionPrueba.PruebaAtrubutes.SonHer


But I get this error when I call the save method. When I load the class everything is fine.
I also think my problem may be when I make a reference in the class that calls the primary key class:
In this part:
Code:
[Class(Table = "Sons")]
    public class SonHer
    {
        public SonHer()
        {
            id = new SonPK();
        }

        private SonPK id;

     
       [NHibernate.Mapping.Attributes.CompositeElement(ClassType = typeof(SonPK))]       
        public virtual SonPK Id
        {
            get { return this.id; }
            set { this.id = value; }
        }
}


Is it right to use [NHibernate.Mapping.Attributes.CompositeElement(ClassType = typeof(SonPK))] to reference the composite class?
Thanks


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