I'm trying to map a simple 1-2-m collection using the <composite-element> element. I've created a simple example (see below) to try to see what is going on.
I can create a parent and add 2 child items and these are persisted correctly. However if I try to then remove a single item, NHibernate deleted both items - SQL output pasted below.
I suspect this has something to do with the 'identity' of the child items since there is no explicit key for these items, but I'm struggling to find any good examples of this in practice.
Can anybody explain what is going on here? An example would be even better.
Mapping file:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Examples.CompositeElement.CompositeElementParent, Examples"
table="CompositeElementParent" lazy="false">
<id name="Id" column="ParentID">
<generator class="identity" />
</id>
<property name="Name" column="Name" />
<list name="Children" table="CompositeElementChild" access="nosetter.camelcase-underscore">
<key column="ParentID" />
<index column="[Index]" />
<composite-element class="Examples.CompositeElement.CompositeElementChild, Examples">
<property name="Name" column="Name"/>
</composite-element>
</list>
</class>
</hibernate-mapping>
Database:Code:
CREATE TABLE [dbo].[CompositeElementParent](
[ParentID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_CompositeElementParent] PRIMARY KEY CLUSTERED
(
[ParentID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[CompositeElementChild](
[ChildID] [int] IDENTITY(1,1) NOT NULL,
[ParentID] [int] NOT NULL,
[Index] [int] NOT NULL,
[Name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
CONSTRAINT [PK_CompositeElementChild] PRIMARY KEY CLUSTERED
(
[ChildID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
ALTER TABLE [dbo].[CompositeElementChild] WITH CHECK ADD CONSTRAINT [FK_CompositeElementChild_CompositeElementParent] FOREIGN KEY([ParentID])
REFERENCES [dbo].[CompositeElementParent] ([ParentID])
SQL Output:Code:
NHibernate: INSERT INTO NHibernateMappingExamples.dbo.CompositeElementParent (Name) VALUES (@p0); select SCOPE_IDENTITY()
@p0 = 'Parent'
NHibernate: INSERT INTO NHibernateMappingExamples.dbo.CompositeElementChild (ParentID, [Index], Name) VALUES (@p0, @p1, @p2)
@p0 = '22'
@p1 = '0'
@p2 = 'Child 2'
NHibernate: INSERT INTO NHibernateMappingExamples.dbo.CompositeElementChild (ParentID, [Index], Name) VALUES (@p0, @p1, @p2)
@p0 = '22'
@p1 = '1'
@p2 = ''
NHibernate: SELECT compositee0_.ParentID as ParentID0_, compositee0_.Name as Name0_0_ FROM NHibernateMappingExamples.dbo.CompositeElementParent compositee0_ WHERE compositee0_.ParentID=@p0
@p0 = '22'
NHibernate: SELECT children0_.ParentID as ParentID__0_, children0_.Name as Name0_, children0_.[Index] as column3___0_ FROM NHibernateMappingExamples.dbo.CompositeElementChild children0_ WHERE children0_.ParentID=@p0
@p0 = '22'
NHibernate: DELETE FROM NHibernateMappingExamples.dbo.CompositeElementChild WHERE ParentID = @p0 AND [Index] = @p1
@p0 = '22'
@p1 = '1'
NHibernate: DELETE FROM NHibernateMappingExamples.dbo.CompositeElementChild WHERE ParentID = @p0 AND [Index] = @p1
@p0 = '22'
@p1 = '0'
NHibernate: SELECT compositee0_.ParentID as ParentID0_, compositee0_.Name as Name0_0_ FROM NHibernateMappingExamples.dbo.CompositeElementParent compositee0_ WHERE compositee0_.ParentID=@p0
@p0 = '22'
NHibernate: SELECT children0_.ParentID as ParentID__0_, children0_.Name as Name0_, children0_.[Index] as column3___0_ FROM NHibernateMappingExamples.dbo.CompositeElementChild children0_ WHERE children0_.ParentID=@p0
@p0 = '22'