If i swap the WithKeyRefrence to WithKeyProperty i get a mapping error
on start up.

It then cannot map the OrderProduct


I wouldnt know where to start making some test cases for this?  I dont
know what you would expect to receive from me?



On Mar 11, 3:37 pm, James Gregory <jagregory....@gmail.com> wrote:
> Neither of us said we don't like composite-ids, although that may be the
> case, we just don't have a great deal of experience using them. This is why
> we're not being all that helpful, because we don't fully understand them
> ourselves. You have to appreciate that for FNH to support a feature, all we
> have to do is output the xml that NHibernate expects, not to actually
> understand what NH does with it.
> What would be more useful than a console app would be a test, or set of
> tests, that we could add to the FNH codebase to ensure that if there is a
> bug it never regresses. A console app will require one of us to derive tests
> from it, which takes more work....
>
> read more »
>
> It may be my naïvety showing, but I believe you should be using
> WithKeyProperty rather than WithKeyReference. I could be wrong though.
>
>
>
> On Wed, Mar 11, 2009 at 3:14 PM, sianabanana <sianm...@hotmail.com> wrote:
>
> > No more responses?
>
> > Any one can shed any light.
>
> > Here is the code from the example console application if anyone wants
> > to have a go?
>
> > using System;
> > using System.Collections.Generic;
> > using System.Linq;
> > using System.Text;
> > using FluentNHibernate;
> > using Domain;
> > using FluentNHibernate.AutoMap;
> > using FluentNHibernate.Cfg.Db;
> > using FluentNHibernate.AutoMap.Alterations;
> > using Overrides;
> > using NHibernate;
>
> > namespace CompositeKeyExample
> > {
> >    class Program
> >    {
> >        public static ISession Session { get; set; }
>
> >        public static ITransaction Transaction { get; set; }
>
> >        static void Main(string[] args)
> >        {
> >            Session = SessionFactory.CreateSession();
> >            Transaction = Session.BeginTransaction();
>
> >            Order o = new Order();
> >            o.OrderDate = DateTime.Now;
>
> >             Product p = new Product();
> >            p.Name = "Custom Item";
>
> >             OrderProduct op = new OrderProduct();
> >            op.Product = p;
> >            op.Order = o;
> >            op.Quantity = 1;
>
> >             o.Products = new List<OrderProduct>();
> >            o.Products.Add(op);
>
> >             Session.SaveOrUpdate(o);
>
> >            //Order o = new Order();
> >             //o.OrderDate = DateTime.Now;
> >            //Session.SaveOrUpdate(o);
>
> >            //Product p = new Product();
> >            //p.Name = "Custom Item";
> >            //Session.SaveOrUpdate(p);
>
> >            //OrderProduct op = new OrderProduct();
> >            //op.Product = p;
> >            //op.Order = o;
> >            //op.Quantity = 1;
> >            //Session.SaveOrUpdate(op);
>
> >            //o.Products = new List<OrderProduct>();
> >            //o.Products.Add(op);
>
> >             try
> >            {
> >                Transaction.Commit();
> >            }
> >            catch(Exception ex)
> >            {
> >                Transaction.Rollback();
> >                throw ex;
> >            }
>
> >                //Session.Flush();
> >                Session.Close();
> >        }
>
> >        private static ISessionSource _factory;
> >        public static ISessionSource SessionFactory
> >        {
> >            get
> >            {
> >                if (_factory == null)
> >                {
>
> >                    IDictionary<string, string> _properties =
> > MsSqlConfiguration
> >                        .MsSql2005
> >                        .ConnectionString( x =>
> > x.FromConnectionStringWithKey("MsSqlConnStr"))
> >                        .ToProperties();
>
> >                    FluentNHibernate.AutoMap.AutoPersistenceModel
> > _mappings = AutoPersistenceModel
> >                        .MapEntitiesFromAssemblyOf<Order>()
> >                        .Where(t => t.Namespace == typeof
> > (Order).Namespace)
> >                        .UseOverridesFromAssemblyOf<OrderMapOverride>
> > ()
> >                        .WithConvention(convention =>
> >                         {
> >                            convention.GetPrimaryKeyNameFromType =
> > type => type.Name + "Id";
> >                            convention.GetForeignKeyNameOfParent =
> > type => type.Name + "Id";
> >                            convention.GetForeignKeyName = type =>
> > type.Name + "Id";
> >                            convention.GetManyToManyTableName =
> > (parent, child) => parent.Name + "_" + child.Name;
> >                            convention.OneToManyConvention = o =>
> > o.Cascade.All();
> >                            convention.OneToOneConvention = o =>
> > o.Cascade.All();
> >                            convention.ManyToOneConvention = o =>
> > o.Cascade.All();
> >                         });
>
> >                    _factory = new SessionSource(_properties,
> > _mappings);
> >                    _factory.BuildSchema();
> >                }
>
> >                return _factory;
> >            }
> >        }
> >    }
> > }
> > namespace Domain
> > {
> >    public class Order
> >    {
> >        public virtual int Id { get; set; }
>
> >        public virtual DateTime OrderDate { get; set; }
>
> >        public virtual IList<OrderProduct> Products { get; set; }
> >    }
>
> >    public class Product
> >    {
> >        public virtual int Id { get; set; }
> >        public virtual string Name { get; set; }
>
> >        public virtual IList<OrderProduct> Orders { get; set; }
> >    }
>
> >    public class OrderProduct
> >    {
> >        public virtual Order Order { get; set; }
> >        public virtual Product Product { get; set; }
> >        public virtual int Quantity { get; set; }
>
> >        public override bool Equals(object obj)
> >        {
> >            if (obj == null)
> >                return false;
>
> >            OrderProduct other = (OrderProduct)obj;
>
> >            return (Order.Id == other.Order.Id && Product.Id ==
> > other.Product.Id);
> >        }
>
> >        public override int GetHashCode()
> >        {
> >            return string.Format("{0}|{1}",
> >                Order.Id, Product.Id).GetHashCode();
> >        }
> >    }
> > }
>
> > namespace Overrides
> > {
> >    public class OrderMapOverride : IAutoMappingOverride<Order>
> >    {
> >        public void Override(AutoMap<Order> map)
> >        {
> >            map.HasMany(x => x.Products)
> >                .Inverse();
> >        }
> >    }
>
> >     public class ProductMapOverride : IAutoMappingOverride<Product>
> >    {
> >        public void Override(AutoMap<Product> map)
> >        {
> >            map.HasMany(x => x.Orders)
> >                .Inverse();
> >        }
> >    }
>
> >    public class OrderProductMapOverride :
> > IAutoMappingOverride<OrderProduct>
> >    {
> >        public void Override(AutoMap<OrderProduct> map)
> >        {
> >            map.UseCompositeId()
> >                .WithKeyReference(x => x.Order, "OrderId")
> >                .WithKeyReference(x => x.Product, "ProductId");
>
> >            //map.HasOne(x => x.Order)
> >            //    .WithForeignKey("OrderId");
>
> >            //map.HasOne(x => x.Product)
> >            //    .WithForeignKey("ProductId");
>
> >            //map.References(x => x.Order)
> >            //    .WithForeignKey("Id").TheColumnNameIs("OrderId");
> >            //map.References(x => x.Product)
> >            //    .WithForeignKey("Id").TheColumnNameIs("ProductId");
> >        }
> >    }
> > }
>
> > On Mar 10, 11:14 am, sianabanana <sianm...@hotmail.com> wrote:
> > > I do understand that you guys are againscompositekeys in genreral.
>
> > > But my query is 2 fold,
> > > * nhibernate resolves many to many relationships viacompositekeys,
> > > so why cant i do this?
> > > * if fluent nhibernate supportscompositekeys, even though you guys
> > > dont like them, then why doesnt this work.
>
> > > I know i can fix this by using an ID, but i think its besides the
> > > point, I think this is a genuine bug.
>
> > > I have compiled the example in to a really simple console app - if you
> > > want me to email it to you?
>
> > > I tried your idea, and that produced the same problem, it tires to
> > > update and not insert.
>
> > > Thanks very much for your help so far.
>
> > > On Mar 10, 10:25 am, Andrew Stewart <andrewnstew...@gmail.com> wrote:
>
> > > > Hi
> > > > Can I ask you to check a few things, we'll take your service out of the
> > > > equation to start with.
>
> > > > Order o = new Order();
> > > > o.OrderDate = DateTime.Now;
> > > > Session.SaveOrUpdate(o);
>
> > > > Product p = new Product();
> > > > p.Name = "Custom Item";
> > > > Session.SaveOrUpdate(p);
>
> > > > OrderProduct op = new OrderProduct();
> > > > op.Product = p;
> > > > op.Order = o;
> > > > op.Quantity = 1;
> > > > Session.SaveOrUpdate(op);
>
> > > > o.Products = new List<OrderProduct>();
> > > > o.Products.Add(op);
> > > > Session.Flush();
>
> > > > I'm just wondering if your parent objects need saving before your
> > > > OrderProducts, so that they have a id to save in the join table. I'm
> > with
> > > > James though, I've hardly usedcompositekeys. Oh and adding a normal id
> > to
> > > > your OrderProduct would probably resolve your issue, plus is the
> > recommended
> > > > best practice now a days.
>
> > > > Cheers
>
> > > > Andy
>
> > > > On Tue, Mar 10, 2009 at 10:12 AM, sianabanana <sianm...@hotmail.com>
> > wrote:
>
> > > > > Any ideas james,
>
> > > > > I have tested deleting updating and selecting, and this works.
>
> > > > > I just cant insert a record - as it tries to update.
>
> > > > > On Mar 8, 12:48 pm, James Gregory <jagregory....@gmail.com> wrote:
> > > > > > I'm not really familiar withcompositekeys in a production
> > environment
> > > > > > (nothing outside of writing the code to support it in FNH), could
> > you
> > > > > give
> > > > > > me an example of the actual code you're using to save your
> > entities? Even
> > > > > > more helpful would be if you could reduce your mappings down to the
> > bare
> > > > > > minimum needed to reproduce the problem.
>
> > > > > > On Sun, Mar 8, 2009 at 12:07 AM, sianabanana <sianm...@hotmail.com
>
> > > > > wrote:
>
> > > > > > > Any- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to fluent-nhibernate@googlegroups.com
To unsubscribe from this group, send email to 
fluent-nhibernate+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to