[fluent-nhib] Re: Composite Key for resolving Many to Many Relationship

2009-03-13 Thread James Gregory
Just replied to another thread about this. We don't support complex
composite-ids (one's that are essentially components) right now. I would
wholeheartedly recommend that you don't use composite-ids if at all
possible, use surrogate keys.

On Fri, Mar 13, 2009 at 9:56 PM, sianabanana  wrote:

>
> Looking in to this some more, i dont think what im trying to acheive
> can be done.
>
> I found this issue in the log which seems to describe the problem im
> having.
> http://code.google.com/p/fluent-nhibernate/issues/detail?id=27
>
> I think if i tried to make a composite id that was of simple types
>
> like
> OrderID and ProductId it might work.
> But i think the problem is that it cannot cope with the complex types
> that im trying to implement.
> Order and Product.
>
> Anyone else have any ideas about this?
>
> On Mar 11, 3:50 pm, sianabanana  wrote:
> > I think thats where i was about to look next.
> >
> > I will do this and then update.
> >
> > Thanks again for the support so far,
> >
> > On Mar 11, 3:48 pm, James Gregory  wrote:
> >
> >
> >
> > > Well, if you're not able to write tests, then another way would be to
> get
> > > the behavior you desire to work in standard NHibernate using the xml
> > > mappings. If you can succeed at that, then we can compare the mappings
> > > you're using with that to what FNH is generating
> >
> > > read more »
> >
> > > On Wed, Mar 11, 2009 at 3:42 PM, sianabanana 
> wrote:
> >
> > > > 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  wrote:
> > > > > Neither of us said we don't likecomposite-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  >
> > > > 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();
> > > > > >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);
> >
> > > > > > 

[fluent-nhib] Re: Composite Key for resolving Many to Many Relationship

2009-03-13 Thread sianabanana

Looking in to this some more, i dont think what im trying to acheive
can be done.

I found this issue in the log which seems to describe the problem im
having.
http://code.google.com/p/fluent-nhibernate/issues/detail?id=27

I think if i tried to make a composite id that was of simple types

like
OrderID and ProductId it might work.
But i think the problem is that it cannot cope with the complex types
that im trying to implement.
Order and Product.

Anyone else have any ideas about this?

On Mar 11, 3:50 pm, sianabanana  wrote:
> I think thats where i was about to look next.
>
> I will do this and then update.
>
> Thanks again for the support so far,
>
> On Mar 11, 3:48 pm, James Gregory  wrote:
>
>
>
> > Well, if you're not able to write tests, then another way would be to get
> > the behavior you desire to work in standard NHibernate using the xml
> > mappings. If you can succeed at that, then we can compare the mappings
> > you're using with that to what FNH is generating
>
> > read more »
>
> > On Wed, Mar 11, 2009 at 3:42 PM, sianabanana  wrote:
>
> > > 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  wrote:
> > > > Neither of us said we don't likecomposite-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 
> > > 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();
> > > > >            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();
> > > > >            //o.Products.Add(op);
>
> > > > >             try
> > > > >            {
> > > > >                Transaction.Commit();
> > > > >            }
> > > > >            catch(Exception ex)
> > > > >            {
> > > > >                Transaction.Rollback();
> > > > >                throw ex;
> > > > >            }
>
> > > > >                //Session.Flush();
> > > > >                Session.Close();
> > > > >        }
>
> > > > >        private static ISessionSource _factory;
> > > > >        public static ISessio

[fluent-nhib] Re: Composite Key for resolving Many to Many Relationship

2009-03-11 Thread sianabanana

I think thats where i was about to look next.



I will do this and then update.



Thanks again for the support so far,


On Mar 11, 3:48 pm, James Gregory  wrote:
> Well, if you're not able to write tests, then another way would be to get
> the behavior you desire to work in standard NHibernate using the xml
> mappings. If you can succeed at that, then we can compare the mappings
> you're using with that to what FNH is generating
>
> read more »
>
>
>
> On Wed, Mar 11, 2009 at 3:42 PM, sianabanana  wrote:
>
> > 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  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 
> > 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();
> > > >            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();
> > > >            //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 _properties =
> > > > MsSqlConfiguration
> > > >                        .MsSql2005
> > > >                        .ConnectionString( x =>
> > > > x.FromConnectionStringWithKey("MsSqlConnStr"))
> > > >                        .ToProperties();
>
> > > >                    FluentNHibernate.AutoMap.AutoPersistenceModel
> > > > _mappings = AutoPersistenceModel
> > > >                        .MapEntitiesFromAssemblyOf()
> > > >                        .Where(t => t.Namespace == typeof
> > > > (Order).Namespace)
> > > >                        

[fluent-nhib] Re: Composite Key for resolving Many to Many Relationship

2009-03-11 Thread James Gregory
Well, if you're not able to write tests, then another way would be to get
the behavior you desire to work in standard NHibernate using the xml
mappings. If you can succeed at that, then we can compare the mappings
you're using with that to what FNH is generating.

On Wed, Mar 11, 2009 at 3:42 PM, sianabanana  wrote:

>
> 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  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 
> 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();
> > >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();
> > >//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 _properties =
> > > MsSqlConfiguration
> > >.MsSql2005
> > >.ConnectionString( x =>
> > > x.FromConnectionStringWithKey("MsSqlConnStr"))
> > >.ToProperties();
> >
> > >FluentNHibernate.AutoMap.AutoPersistenceModel
> > > _mappings = AutoPersistenceModel
> > >.MapEntitiesFromAssemblyOf()
> > >.Where(t => t.Namespace == typeof
> > > (Order).Namespace)
> > >.UseOverridesFromAssemblyOf
> > > ()
> > >.WithConvention(convention =>
> > > {
> > >convention.GetPrimaryKeyNameFromType =
> > > type => type.Name + "Id";
> > >convention.GetForeignKeyNameOfParent =
> > > type => type.Name + "Id";
> > >

[fluent-nhib] Re: Composite Key for resolving Many to Many Relationship

2009-03-11 Thread sianabanana

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  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  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();
> >            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();
> >            //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 _properties =
> > MsSqlConfiguration
> >                        .MsSql2005
> >                        .ConnectionString( x =>
> > x.FromConnectionStringWithKey("MsSqlConnStr"))
> >                        .ToProperties();
>
> >                    FluentNHibernate.AutoMap.AutoPersistenceModel
> > _mappings = AutoPersistenceModel
> >                        .MapEntitiesFromAssemblyOf()
> >                        .Where(t => t.Namespace == typeof
> > (Order).Namespace)
> >                        .UseOverridesFromAssemblyOf
> > ()
> >                        .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);
> >        

[fluent-nhib] Re: Composite Key for resolving Many to Many Relationship

2009-03-11 Thread James Gregory
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.

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  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();
>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();
>//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 _properties =
> MsSqlConfiguration
>.MsSql2005
>.ConnectionString( x =>
> x.FromConnectionStringWithKey("MsSqlConnStr"))
>.ToProperties();
>
>FluentNHibernate.AutoMap.AutoPersistenceModel
> _mappings = AutoPersistenceModel
>.MapEntitiesFromAssemblyOf()
>.Where(t => t.Namespace == typeof
> (Order).Namespace)
>.UseOverridesFromAssemblyOf
> ()
>.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 Products { get; set; }
>}
>
>public class Product
>{
>public virtual int Id { get; set; }
>public virtual string Name { get; set; }
>
>public virtual IList Orders { get; set; 

[fluent-nhib] Re: Composite Key for resolving Many to Many Relationship

2009-03-11 Thread sianabanana

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();
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();
//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 _properties =
MsSqlConfiguration
.MsSql2005
.ConnectionString( x =>
x.FromConnectionStringWithKey("MsSqlConnStr"))
.ToProperties();

FluentNHibernate.AutoMap.AutoPersistenceModel
_mappings = AutoPersistenceModel
.MapEntitiesFromAssemblyOf()
.Where(t => t.Namespace == typeof
(Order).Namespace)
.UseOverridesFromAssemblyOf
()
.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 Products { get; set; }
}

public class Product
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }

public virtual IList 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
{
public void Override(AutoMap map)
{
map.HasMany(x => x.Products)
.Inverse();
}
}

public class ProductMapOverride : IAutoMappingOverride
{
public void Override(AutoMap map)
{
map.HasMany(x => x.Orders)
.Inverse();

[fluent-nhib] Re: Composite Key for resolving Many to Many Relationship

2009-03-10 Thread sianabanana

I do understand that you guys are agains composite keys in genreral.

But my query is 2 fold,
* nhibernate resolves many to many relationships via composite keys,
so why cant i do this?
* if fluent nhibernate supports composite keys, 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  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();
> 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 used composite keys. 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  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  wrote:
> > > I'm not really familiar with composite keys 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 
> > wrote:
>
> > > > Any ideas? I could really do with some help on this one.
> > > > Much appriciated :)
>
> > > > On Mar 6, 11:40 pm, sianabanana  wrote:
> > > > > I have a more complex many to many relationship and need to create
> > the
> > > > > link between the manys as an object to store further data.
>
> > > > > Order  - id, date
>
> > > > > Order Product -  orderid, productid, quantity
>
> > > > > Product - id, name
>
> > > > > I am using auto mapping, and i am overriding the mapping to specify
> > > > > anything that doesnt fall in to the convention.
>
> > > > > No matter what conbination of properties i use, i cannot get the
> > order
> > > > > product object to persis to the database.
>
> > > > > When profiling, the order object causes an insert, the product causes
> > > > > an insert
> > > > > but the order product causes an update - but there is nothing to
> > > > > update and this throws the following error. Unexpected row count: 0;
> > > > > expected: 1 - as you would expect.
>
> > > > > I would love if someone could show me a working example of how to get
> > > > > this working.
>
> > > > > public class Order
> > > > >     {
> > > > >         public virtual int Id { get; set; }
>
> > > > >         public virtual DateTime OrderDate { get; set; }
>
> > > > >         public virtual IList Products { get; set; }
>
> > > > > }
>
> > > > > public class Product
> > > > >     {
> > > > >         public virtual int Id { get; set; }
> > > > >         public virtual string Name { get; set; }
>
> > > > >         public virtual IList 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();
> > > > >         }
> > > > >     }
>
> > > > >         public class OrderMapOverride : IAutoMappingOverride
> > > > >     {
> > > > >         public void Override(AutoMap map)
> > > > >         {
> > > > >             map.HasMany(x => x.Products)
> > > > >                 .Inverse();
> > > > >         }
> > > > >     }
>
> > > > > public class OrderProductMapOverride :
> > > > > IAutoMappingOverride
> > > > >     {
> > > > >         p

[fluent-nhib] Re: Composite Key for resolving Many to Many Relationship

2009-03-10 Thread Andrew Stewart
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();
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 used composite keys. 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  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  wrote:
> > I'm not really familiar with composite keys 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 
> wrote:
> >
> > > Any ideas? I could really do with some help on this one.
> > > Much appriciated :)
> >
> > > On Mar 6, 11:40 pm, sianabanana  wrote:
> > > > I have a more complex many to many relationship and need to create
> the
> > > > link between the manys as an object to store further data.
> >
> > > > Order  - id, date
> >
> > > > Order Product -  orderid, productid, quantity
> >
> > > > Product - id, name
> >
> > > > I am using auto mapping, and i am overriding the mapping to specify
> > > > anything that doesnt fall in to the convention.
> >
> > > > No matter what conbination of properties i use, i cannot get the
> order
> > > > product object to persis to the database.
> >
> > > > When profiling, the order object causes an insert, the product causes
> > > > an insert
> > > > but the order product causes an update - but there is nothing to
> > > > update and this throws the following error. Unexpected row count: 0;
> > > > expected: 1 - as you would expect.
> >
> > > > I would love if someone could show me a working example of how to get
> > > > this working.
> >
> > > > public class Order
> > > > {
> > > > public virtual int Id { get; set; }
> >
> > > > public virtual DateTime OrderDate { get; set; }
> >
> > > > public virtual IList Products { get; set; }
> >
> > > > }
> >
> > > > public class Product
> > > > {
> > > > public virtual int Id { get; set; }
> > > > public virtual string Name { get; set; }
> >
> > > > public virtual IList 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();
> > > > }
> > > > }
> >
> > > > public class OrderMapOverride : IAutoMappingOverride
> > > > {
> > > > public void Override(AutoMap map)
> > > > {
> > > > map.HasMany(x => x.Products)
> > > > .Inverse();
> > > > }
> > > > }
> >
> > > > public class OrderProductMapOverride :
> > > > IAutoMappingOverride
> > > > {
> > > > public void Override(AutoMap 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");
> > > > }
> > > > }
> >
> > > > public class ProductMapOverride : IAutoMappingOverride
> > > > {
> > > > public 

[fluent-nhib] Re: Composite Key for resolving Many to Many Relationship

2009-03-10 Thread sianabanana

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  wrote:
> I'm not really familiar with composite keys 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  wrote:
>
> > Any ideas? I could really do with some help on this one.
> > Much appriciated :)
>
> > On Mar 6, 11:40 pm, sianabanana  wrote:
> > > I have a more complex many to many relationship and need to create the
> > > link between the manys as an object to store further data.
>
> > > Order  - id, date
>
> > > Order Product -  orderid, productid, quantity
>
> > > Product - id, name
>
> > > I am using auto mapping, and i am overriding the mapping to specify
> > > anything that doesnt fall in to the convention.
>
> > > No matter what conbination of properties i use, i cannot get the order
> > > product object to persis to the database.
>
> > > When profiling, the order object causes an insert, the product causes
> > > an insert
> > > but the order product causes an update - but there is nothing to
> > > update and this throws the following error. Unexpected row count: 0;
> > > expected: 1 - as you would expect.
>
> > > I would love if someone could show me a working example of how to get
> > > this working.
>
> > > public class Order
> > >     {
> > >         public virtual int Id { get; set; }
>
> > >         public virtual DateTime OrderDate { get; set; }
>
> > >         public virtual IList Products { get; set; }
>
> > > }
>
> > > public class Product
> > >     {
> > >         public virtual int Id { get; set; }
> > >         public virtual string Name { get; set; }
>
> > >         public virtual IList 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();
> > >         }
> > >     }
>
> > >         public class OrderMapOverride : IAutoMappingOverride
> > >     {
> > >         public void Override(AutoMap map)
> > >         {
> > >             map.HasMany(x => x.Products)
> > >                 .Inverse();
> > >         }
> > >     }
>
> > > public class OrderProductMapOverride :
> > > IAutoMappingOverride
> > >     {
> > >         public void Override(AutoMap 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");
> > >         }
> > >     }
>
> > > public class ProductMapOverride : IAutoMappingOverride
> > >     {
> > >         public void Override(AutoMap map)
> > >         {
> > >             map.HasMany(x => x.Orders)
> > >                 .Inverse();
> > >         }
> > >     }
>
> > > ps. i set the following in my conventions.
> > > 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();
>
> > > I have puzzled over this for days and really could do with some help
> > > on this one.
>
> > > Thanks- 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.co

[fluent-nhib] Re: Composite Key for resolving Many to Many Relationship

2009-03-08 Thread sianabanana

Thanks for your reply james,

The above code is the acual code for my domain objects and my mapping
override classes.


I would then call the following code to pouplate the objects

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();
o.Products.Add(op);

new OrderService().Save(o);

My order service, passes through to a repository object that in turn
calls
Session.SaveOrUpdate(item);

Is this enough for you, It Really is the barest minimum?

When  you say you are not familiar with composite keys in a production
environment, would you ad an Id to the Order Product to resolve the
issue?

The reason i didnt want to do this was that previously, i didnt need
the quantity field. I had just implemented a many to many between
Order and Product.  This obviously didnt have a Order Product object
and nhibernate was clever enough to go to my Order_Product table in
the database and make the connection for its self.
The table it created had composite keys.  Orderid and productid. ( I
used the CreateSchema method)

Later, my requirement grew to add a quantity field to the
Order_Product link, thus needing to actually make this an object in
the code so that i could add the quantity property.

I left the IDs as they were and added another column in the
database.

So this is why i am now at this point.

Using composite keys in this case makes sense to me, as the order
product is the product of a many to many relationship between orde and
product, and this way would ensure the data didnt have any duplicates
in it.
I know i could use an index for this too, but I just feel that this is
the correct use of composite keys.

Please tell me if im wrong?


On Mar 8, 12:48 pm, James Gregory  wrote:
> I'm not really familiar with composite keys 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  wrote:
>
> > Any ideas? I could really do with some help on this one.
> > Much appriciated :)
>
> > On Mar 6, 11:40 pm, sianabanana  wrote:
> > > I have a more complex many to many relationship and need to create the
> > > link between the manys as an object to store further data.
>
> > > Order  - id, date
>
> > > Order Product -  orderid, productid, quantity
>
> > > Product - id, name
>
> > > I am using auto mapping, and i am overriding the mapping to specify
> > > anything that doesnt fall in to the convention.
>
> > > No matter what conbination of properties i use, i cannot get the order
> > > product object to persis to the database.
>
> > > When profiling, the order object causes an insert, the product causes
> > > an insert
> > > but the order product causes an update - but there is nothing to
> > > update and this throws the following error. Unexpected row count: 0;
> > > expected: 1 - as you would expect.
>
> > > I would love if someone could show me a working example of how to get
> > > this working.
>
> > > public class Order
> > >     {
> > >         public virtual int Id { get; set; }
>
> > >         public virtual DateTime OrderDate { get; set; }
>
> > >         public virtual IList Products { get; set; }
>
> > > }
>
> > > public class Product
> > >     {
> > >         public virtual int Id { get; set; }
> > >         public virtual string Name { get; set; }
>
> > >         public virtual IList 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();
> > >         }
> > >     }
>
> > >         public class OrderMapOverride : IAutoMappingOverride
> > >     {
> > >         public void Override(AutoMap map)
> > >         {
> > >             map.HasMany(x => x.Products)
> > >                 .Inverse();
> > >         }
> > >     }
>
> > > public class OrderProductMapOverride :
> > > IAutoMappingOverride
> > >     {
> > >         public void Override(AutoMap map)
> > >         {
> > >             map.UseCompositeId()
> > >                 .WithKeyReference(x => x.Order, "OrderId")
> > >                 .WithKeyRefer

[fluent-nhib] Re: Composite Key for resolving Many to Many Relationship

2009-03-08 Thread James Gregory
I'm not really familiar with composite keys 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  wrote:

>
> Any ideas? I could really do with some help on this one.
> Much appriciated :)
>
> On Mar 6, 11:40 pm, sianabanana  wrote:
> > I have a more complex many to many relationship and need to create the
> > link between the manys as an object to store further data.
> >
> > Order  - id, date
> >
> > Order Product -  orderid, productid, quantity
> >
> > Product - id, name
> >
> > I am using auto mapping, and i am overriding the mapping to specify
> > anything that doesnt fall in to the convention.
> >
> > No matter what conbination of properties i use, i cannot get the order
> > product object to persis to the database.
> >
> > When profiling, the order object causes an insert, the product causes
> > an insert
> > but the order product causes an update - but there is nothing to
> > update and this throws the following error. Unexpected row count: 0;
> > expected: 1 - as you would expect.
> >
> > I would love if someone could show me a working example of how to get
> > this working.
> >
> > public class Order
> > {
> > public virtual int Id { get; set; }
> >
> > public virtual DateTime OrderDate { get; set; }
> >
> > public virtual IList Products { get; set; }
> >
> > }
> >
> > public class Product
> > {
> > public virtual int Id { get; set; }
> > public virtual string Name { get; set; }
> >
> > public virtual IList 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();
> > }
> > }
> >
> > public class OrderMapOverride : IAutoMappingOverride
> > {
> > public void Override(AutoMap map)
> > {
> > map.HasMany(x => x.Products)
> > .Inverse();
> > }
> > }
> >
> > public class OrderProductMapOverride :
> > IAutoMappingOverride
> > {
> > public void Override(AutoMap 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");
> > }
> > }
> >
> > public class ProductMapOverride : IAutoMappingOverride
> > {
> > public void Override(AutoMap map)
> > {
> > map.HasMany(x => x.Orders)
> > .Inverse();
> > }
> > }
> >
> > ps. i set the following in my conventions.
> > 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();
> >
> > I have puzzled over this for days and really could do with some help
> > on this one.
> >
> > Thanks
> >
>

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[fluent-nhib] Re: Composite Key for resolving Many to Many Relationship

2009-03-07 Thread sianabanana

Any ideas? I could really do with some help on this one.
Much appriciated :)

On Mar 6, 11:40 pm, sianabanana  wrote:
> I have a more complex many to many relationship and need to create the
> link between the manys as an object to store further data.
>
> Order  - id, date
>
> Order Product -  orderid, productid, quantity
>
> Product - id, name
>
> I am using auto mapping, and i am overriding the mapping to specify
> anything that doesnt fall in to the convention.
>
> No matter what conbination of properties i use, i cannot get the order
> product object to persis to the database.
>
> When profiling, the order object causes an insert, the product causes
> an insert
> but the order product causes an update - but there is nothing to
> update and this throws the following error. Unexpected row count: 0;
> expected: 1 - as you would expect.
>
> I would love if someone could show me a working example of how to get
> this working.
>
> public class Order
>     {
>         public virtual int Id { get; set; }
>
>         public virtual DateTime OrderDate { get; set; }
>
>         public virtual IList Products { get; set; }
>
> }
>
> public class Product
>     {
>         public virtual int Id { get; set; }
>         public virtual string Name { get; set; }
>
>         public virtual IList 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();
>         }
>     }
>
>         public class OrderMapOverride : IAutoMappingOverride
>     {
>         public void Override(AutoMap map)
>         {
>             map.HasMany(x => x.Products)
>                 .Inverse();
>         }
>     }
>
> public class OrderProductMapOverride :
> IAutoMappingOverride
>     {
>         public void Override(AutoMap 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");
>         }
>     }
>
> public class ProductMapOverride : IAutoMappingOverride
>     {
>         public void Override(AutoMap map)
>         {
>             map.HasMany(x => x.Orders)
>                 .Inverse();
>         }
>     }
>
> ps. i set the following in my conventions.
> 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();
>
> I have puzzled over this for days and really could do with some help
> on this one.
>
> Thanks
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---