-->
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: [Fluent NHibernate] Table with composite Reference ID
PostPosted: Thu Nov 04, 2010 6:33 pm 
Newbie

Joined: Thu Nov 04, 2010 6:07 pm
Posts: 1
EDIT: Sorry Wrong Forum, Moderators could you please move the thread to NHibernate?

Hi guys I just started with NHibernate and I'm really amazed

This is my problem, I have two classes

Code:
    public class RegimentDb
    {
        public virtual Guid Id { get; set; }
        public virtual int PosX { get; set; }
        public virtual int PosY { get; set; }
   }


and

Code:
        public virtual RegimentDb Regiment { get; set; }
        public virtual int UnitType { get; set; }
        public virtual int Number { get; set; }


The logic behind is that every Regiment has Units - for example Regiment 1 has 500 Units of UnitType 3 and 200 Units of UnitType 6

My problem is that NHibernate requires a Unique ID, but currently RegimentUnit has no unique ID. But then I noticed that the combination Regiment(ID) and UnitType is unique so I tried to make
a Composite ID

Code:
    public class RegimentMap : ClassMap<RegimentDb>
    {
        public RegimentMap()
        {
            Table(DB.Tbl.Regiments.Name);
            Id(x => x.Id);
            Map(x => x.PosX);
            Map(x => x.PosY);

            HasMany(x => x.Units)
                .Inverse()
                .Cascade.All();
        }
    }

   public class RegimentUnitMap : ClassMap<RegimentUnitDb>
    {
        public RegimentUnitMap()
        {
            Table(DB.Tbl.RegimentUnits.Name);

            CompositeId()
                .KeyReference(x => x.Regiment)
                .KeyProperty(x => x.UnitType);

            Map(x => x.UnitType);
            Map(x => x.Number);
           
            //References(x => x.Regiment);
        }
    }


I hope that my understanding of Composite IDs is correct. Now I get the error:
composite-id class must override Equals()
I have read that a composite ID must have a own equals and hash method and the description of the method says you should avoid coposite IDs, why is that the case?

Is there maybe something wrong in my code or is there a better way to solve this problem (except creating a own ID for RegimentUnit) ?

Desert Fox


Top
 Profile  
 
 Post subject: Re: [Fluent NHibernate] Table with composite Reference ID
PostPosted: Wed Nov 09, 2011 5:15 pm 
Newbie

Joined: Wed Nov 09, 2011 5:04 pm
Posts: 1
I think you must override GetHashCode and Equals in your class

Code:
        public virtual RegimentDb Regiment { get; set; }
        public virtual int UnitType { get; set; }
        public virtual int Number { get; set; }

        public override int GetHashCode()
        {
            int hashCode = 0;
            hashCode = hashCode ^ Regiment.GetHashCode() ^ UnitType.GetHashCode();
            return hashCode;
        }

        public override bool Equals(object obj)
        {
            var toCompare = obj as RegimenUnittDb;
            if (toCompare == null)
            {
                return false;
            }
            return (this.GetHashCode() != toCompare.GetHashCode());
        }


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.