1)Actually, you *may* want to have order and suit twice - once as id properties for the key, and once as properties back to their objects, i.e. directly to the Order object and the Suit object (if you do in fact need to traverse this relationship in your work. If not, then leave this out) Note that if you do have them as separate objects, you must have the properties for the objects marked insert and update as false... and you'll also want to put some logic in your object property setters to keep the ids synced.
There are a few other potential problems I see with this composite key mapping... no version (guessing you are not handling this another way, like an interceptor), no separate class for your keys, things like that. There are a lot of resources out there for these things besides what I have, but I collected together a lot of composite key info together into one place and you may want to take a look-it covers all of these things I mentioned here and a few others that could bite you later: http://devlicio.us/blogs/anne_epstein/archive/2009/11/20/nhibernate-and-composite-keys.aspx Good luck! Anne On Fri, Jan 29, 2010 at 6:22 AM, Oskar Berggren <[email protected]>wrote: > Should you really list the composite-id properties once more as > regular properties also? > > Also see the note about Equals(): > http://nhforge.org/doc/nh/en/index.html#mapping-declaration-compositeid > > /Oskar > > > 2010/1/29 pootow <[email protected]>: > > We are using NHibernate with a legacy database, please help us: > > > > With this Entity: > > public class OrderSuitInfo > > { > > private int orderAmount = 1; > > > > public long OrderId { get; set; } > > > > public string Code { get; set; } > > > > public string Name { get; set; } > > > > public int OrderAmount > > { > > get { return orderAmount; } > > set { orderAmount = value; } > > } > > > > public int SuitId { get; set; } > > > > public decimal Price { get; set; } > > > > public decimal VPrice { get; set; } > > > > public override bool Equals(object obj) > > { > > return base.Equals(obj); > > } > > > > public override int GetHashCode() > > { > > return GetHashCode(); > > } > > } > > and this hbm: > > <class name="Vancl.Order.Domain.Model.Order.OrderSuitInfo, > > Vancl.Order.Domain" table="OrderSuit" lazy="false"> > > <composite-id> > > <key-property name ="OrderId" column="FormCode"></key- > > property> > > <key-property name ="SuitId" column="SuitRowNo"></key- > > property> > > <key-property name ="Code" column="SuitCode"></key-property> > > </composite-id> > > <property name ="OrderId" column="FormCode" /> > > <property name ="SuitId" column="SuitRowNo" /> > > <property name ="Code" column="SuitCode" /> > > <property name="Name" column="SuitName" /> > > <property name="VPrice" column="VPrice" /> > > <property name="Price" column="Price" /> > > </class> > > > > got this exception: > > System.IndexOutOfRangeException: Invalid index 6 for this > > SqlParameterCollection with Count=6. > > at System.Data.SqlClient.SqlParameterCollection.RangeCheck(Int32 > > index) > > at System.Data.SqlClient.SqlParameterCollection.GetParameter(Int32 > > index) > > at > > > System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item > > (Int32 index) > > at NHibernate.Type.Int64Type.Set(IDbCommand rs, Object value, Int32 > > index) > > at NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object > > value, Int32 index) > > at NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object > > value, Int32 index, ISessionImplementor session) > > at NHibernate.Type.ComponentType.NullSafeSet(IDbCommand st, Object > > value, Int32 begin, ISessionImplementor session) > > at > > NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object > > id, > > Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] > > includeColumns, Int32 table, IDbCommand statement, ISessionImplementor > > session, > > Int32 index) > > at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object > > id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, > > Object obj, ISessionImplementor session) > > at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object > > id, Object[] fields, Object obj, ISessionImplementor session) > > at NHibernate.Action.EntityInsertAction.Execute() > > at > > NHibernate.Engine.ActionQueue.Execute(IExecutable executable) > > at > > NHibernate.Engine.ActionQueue.ExecuteActions(IList list) > > at NHibernate.Engine.ActionQueue.ExecuteActions() > > at > > NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions > > (IEventSource session) > > at > > NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent > > event) > > at NHibernate.Impl.SessionImpl.Flush() > > at > > Spring.Data.NHibernate.HibernateAccessor.FlushIfNecessary(ISession > > session, Boolean existingTransaction) > > at Spring.Data.NHibernate.HibernateTemplate.Execute(IHibernateCallback > > action, Boolean exposeNativeSession) > > at Spring.Data.NHibernate.Generic.HibernateTemplate.Save(Object > > entity) > > at XX.Order.Data.NHibernate.Order.OrderRepos.SaveOrderSuit > > (OrderSuitInfo orderSuitInfo) > > at > > _dynamic_XX.Order.Data.NHibernate.Order.OrderRepos.SaveOrderSuit > > (Object , > > Object[] ) > > at Spring.Reflection.Dynamic.SafeMethod.Invoke(Object target, Object[] > > arguments) > > at Spring.Aop.Framework.DynamicMethodInvocation.InvokeJoinpoint() > > at > > Spring.Aop.Framework.AbstractMethodInvocation.Proceed() > > at > > XX.Aspects.Logging.CommonLoggingAroundAdvice.Invoke(IMethodInvocation > > invocation) > > at > > Spring.Aop.Framework.AbstractMethodInvocation.Proceed() > > at > > Spring.Aop.Framework.DynamicProxy.AdvisedProxy.Invoke(Object proxy, > > Object target, Type targetType, MethodInfo targetMethod, MethodInfo > > proxyMethod, Object[] args, IList interceptors) > > at CompositionAopProxy_f49685df44cf4701a3662367ba1f8883.SaveOrderSuit > > (OrderSuitInfo orderSuitInfo) > > at XX.Order.Domain.Model.Order.OrderService.SaveWholeOrder(OrderInfo > > orderInfo) in OrderService.cs: line 58 > > at > > XX.Order.Domain.Model.Order.OrderService.Create(OrderInfo orderInfo) > > in OrderService.cs: line 37 > > at > > XX.Services.OBSImpl.Order.OrderService.Create(CartDTO cart) in > > OrderService.cs: line 97 > > at > > XX.Order.Services.OBSImpl.Tests.OrderIntegrationTest.CreateOrderTest > > () > > in OrderIntegrationTest.cs: line 165 > > > > any ideas? > > > > -- > > You received this message because you are subscribed to the Google Groups > "nhusers" group. > > To post to this group, send email to [email protected]. > > To unsubscribe from this group, send email to > [email protected]<nhusers%[email protected]> > . > > For more options, visit this group at > http://groups.google.com/group/nhusers?hl=en. > > > > > > -- > You received this message because you are subscribed to the Google Groups > "nhusers" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<nhusers%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/nhusers?hl=en. > > -- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
