views:

654

answers:

1

I have a legacy table with composite keys that are mapped to 3 other tables, since this table have other attributes in it, since it is not a simple mapping table, I can't use the many-to-many set solution to map this.

The following is what I have done:

<class name="classA" table="A">
<composite-id name="ID" class="AKey">
  <key-many-to-one name="Id_one" class="One" column="Id_one" />
  <key-many-to-one name="Id_two" class="Two" column="Id_two" />
  <key-many-to-one name="Id_three" class="Three" column="Id_three" />
</composite-id>

AKey is merely a struct that holds the three ids, and Id_one, Id_two, and Id_three are all defined as int in their respective class.

public struct Akey {
    public int Id_one { get; set; }
    public int Id_two { get; set; }
    public int Id_three { get; set; }
}

This compiles fine, but when I try to run it, it gives me an error message:

NHibernate.QueryException : Type mismatch in NHibernate.Criterion.SimpleExpression: ID expected type AKey, actual type System.Int32

Please advise on what I have done wrong or missed.

Thanks a bunch!

+1  A: 

If you are going to use key-many-to-one you would put the class:

public class Akey {
    public virtual One One {get; set;}
    public virtual Two Two {get; set;}
    public virtual Three Three {get; set;}
}

Otherwise if you want the Id you just map them as properties of classA:

 <composite-id>
     <key-property name="Id_one" column="Id_one" />
     <key-property name="Id_two" column="Id_two" />
     <key-property name="Id_three" column="Id_three" />
 </composite-id>

.

public class classA {
    public virtual int Id_one {get; set;}
    public virtual int Id_two {get; set;}
    public virtual int Id_three {get; set;}

    // ... rest of props ...
}

Or as a composite like you have:

 <composite-id name="ID" class="AKey">
     <key-property name="Id_one" column="Id_one" />
     <key-property name="Id_two" column="Id_two" />
     <key-property name="Id_three" column="Id_three" />
 </composite-id>

.

public class AKey {
    public virtual int Id_one {get; set;}
    public virtual int Id_two {get; set;}
    public virtual int Id_three {get; set;}
}

public class classA {
    public virtual AKey ID {get; set;}

    // ... rest of props ...
}

Finally ...

 <composite-id>
   <key-many-to-one name="Id_one" class="One" column="Id_one" />
   <key-many-to-one name="Id_two" class="Two" column="Id_two" />
   <key-many-to-one name="Id_three" class="Three" column="Id_three" />
 </composite-id>

.

public class classA {
    public virtual One One {get; set;}
    public virtual Two Two {get; set;}
    public virtual Three Three {get; set;}

    // ... rest of props ...
}

Going to punt on whether you can use a struct because I'm not competent on them in C#.

eyston
Thanks for your reply! The last part helped me solve my issue, now I can finally move on! =P
Akey
Yah... I realized after I wrote it that the last one shoulda been the first one!
eyston