Yes. You have to have a unidirectional relation.
What you have implemented is a shallow-bidirectional-association:
public class Child
{
public Guid Id;
public Guid ParentId;
public string Description;
}
What is "ParentId" ?
On Mon, Feb 7, 2011 at 10:51 AM, bstack <[email protected]> wrote:
> Thanks Fabio.
>
> However, is there any way to do this avoiding a bi-directional
> association?
> I dont want to add a reference to Parent in the Child class.
>
> Note I am trying to do something very simple:
>
> 1. Create a Parent entity
> 2. Create a collection of Child entities associating them with the
> parent.
> 3. Get NHibernate to just generate INSERT statements.
>
> I was trying to do this previously by using POID = "assigned" - which
> works however SELECT statements are also generated.
> See
> http://groups.google.com/group/nhusers/browse_thread/thread/c614aa5b5a91579
>
> Thanks,
> Billy
>
>
>
>
> On Feb 7, 1:34 pm, Fabio Maulo <[email protected]> wrote:
> > http://nhforge.org/doc/nh/en/index.html#collections-example
> >
> >
> >
> >
> >
> > On Mon, Feb 7, 2011 at 9:34 AM, bstack <[email protected]> wrote:
> > > All,
> >
> > > I have the following entities:
> >
> > > public class Parent
> > > {
> > > public Guid Id;
> >
> > > public string Description;
> >
> > > public IList<Child> Children;
> > > }
> >
> > > public class Child
> > > {
> > > public Guid Id;
> >
> > > public Guid ParentId;
> >
> > > public string Description;
> > > }
> >
> > > I have the following mappings:
> >
> > > <?xml version="1.0" encoding="utf-8" ?>
> > > <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-
> > > access="field">
> > > <class name="ClassLibrary1.Parent, ClassLibrary1" table="dbo.
> > > [TEMP_Parent]" lazy="false" >
> > > <id name="Id">
> > > <generator class="guid.comb" />
> > > </id>
> > > <property name="Description" />
> > > <bag name="Children" cascade="all" lazy="false" >
> > > <key column="ParentId" foreign-key="true"/>
> > > <one-to-many class="ClassLibrary1.Child" />
> > > </bag>
> > > </class>
> > > </hibernate-mapping>
> >
> > > <?xml version="1.0" encoding="utf-8" ?>
> > > <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-
> > > access="field">
> > > <class name="ClassLibrary1.Child, ClassLibrary1" table="dbo.
> > > [TEMP_Child]" lazy="false">
> > > <id name="Id">
> > > <generator class="guid.comb" />
> > > </id>
> > > <property name="ParentId" />
> > > <property name="Description" />
> > > </class>
> > > </hibernate-mapping>
> >
> > > When I run the following test I get an error:
> >
> > > [Test]
> > > public void Test1()
> > > {
> > > var _parent = new Parent();
> > > _parent.Description = "Parent desc";
> > > _parent.Children = new List<Child>();
> > > _parent.Children.Add(new Child
> > > {
> > > Description = "child desc 1"
> > > });
> > > _parent.Children.Add(new Child
> > > {
> > > Description = "child desc 2"
> > > });
> >
> > > var _repository = new Repository(this.c_sessionManager);
> > > _repository.Save(_parent);
> > > }
> >
> > > NHibernate: INSERT INTO dbo.[TEMP_Parent] (Description, Id) VALUES
> > > (@p0, @p1);@p0 = 'Parent desc', @p1 = 8c7f22c3-1a86-478a-
> > > b8a7-9e8300cdc91b
> > > NHibernate: INSERT INTO dbo.[TEMP_Child] (ParentId, Description, Id)
> > > VALUES (@p0, @p1, @p2);@p0 = 00000000-0000-0000-0000-000000000000, @p1
> > > = 'child desc 1', @p2 = db3c4259-58bb-4ca0-80e5-9e8300cdc924
> > > 12:29:14,711 ERROR [ 7] AbstractBatcher [(null)]- Could not execute
> > > command: INSERT INTO dbo.[TEMP_Child] (ParentId, Description, Id)
> > > VALUES (@p0, @p1, @p2)
> > > System.Data.SqlClient.SqlException (0x80131904): The INSERT statement
> > > conflicted with the FOREIGN KEY constraint "PK_TEMP_Child_ForeignKey".
> > > The conflict occurred in database "OLCTG_v2", table "dbo.TEMP_Parent",
> > > column 'Id'.
> > > The statement has been terminated.
> >
> > > The childs foreign key back to the parent is never populated.
> > > Any suggestions to resolve this issue?
> >
> > > Cheers,
> > > Billy Stack
> >
> > > --
> > > 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.
> >
> > --
> > Fabio Maulo- Hide quoted text -
> >
> > - Show quoted text -
>
> --
> 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.
>
>
--
Fabio Maulo
--
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.