Here is a simplified example:
Classes:
public class Parent {
public int Id { get; set; }
public string Name { get; set; }
public IList<Child> Children { get; set; }
}
public class Child {
public int Id { get; set; }
public string Name { get; set; }
public Parent Parent { get; set; }
}
Mapping:
<class name="NHibernateDocumentTest.Parent, NHibernateDocumentTest"
table="Parent" lazy="false">
<id name="Id" column="Id" type="integer">
<generator class="native" />
</id>
<property name="Name" column="Name" type="string" />
<bag name="Children" inverse="true" lazy="true" cascade="all-
delete-orphan">
<key column="ParentId" />
<one-to-many class="NHibernateDocumentTest.Child,
NHibernateDocumentTest" />
</bag>
</class>
<class name="NHibernateDocumentTest.Child, NHibernateDocumentTest"
table="Child" lazy="false">
<id name="Id" column="Id" type="integer">
<generator class="native" />
</id>
<property name="Name" column="Name" type="string" />
<many-to-one name="Parent" column="ParentId" cascade="none" not-
null="true" />
</class>
Code:
var p = this.session.Get<Parent>(1);
p.Children.Clear();
p.Children.Add(new Child() { Name = "Bob", Parent = p});
p.Children.Add(new Child() { Name = "Jones", Parent = p });
this.session.Update(p);
this.session.Flush();
Sql Executed:
NHibernate: SELECT parent0_.Id as Id1_0_, parent0_.Name as Name1_0_
FROM DocumentTest.dbo.Parent parent0_ WHERE [EMAIL PROTECTED]; @p0 = '1'
NHibernate: SELECT children0_.ParentId as ParentId1_, children0_.Id as
Id1_, children0_.Id as Id0_0_, children0_.Name as Name0_0_,
children0_.ParentId as ParentId0_0_ FROM DocumentTest.dbo.Child
children0_ WHERE [EMAIL PROTECTED]; @p0 = '1'
NHibernate: INSERT INTO DocumentTest.dbo.Child (Name, ParentId) VALUES
(@p0, @p1); select SCOPE_IDENTITY(); @p0 = 'Bob', @p1 = '1'
NHibernate: INSERT INTO DocumentTest.dbo.Child (Name, ParentId) VALUES
(@p0, @p1); select SCOPE_IDENTITY(); @p0 = 'Jones', @p1 = '1'
NHibernate: DELETE FROM DocumentTest.dbo.Child WHERE Id = @p0; @p0 =
'19'
NHibernate: DELETE FROM DocumentTest.dbo.Child WHERE Id = @p0; @p0 =
'20'
Now is there anyway for the delete to be called before the insert? Or
am I missing the point here?
Cheers
Stefan
On Nov 26, 3:55 pm, "Stefan Sedich" <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I have a relationship like so:
>
> StudyPlan -> StudyPlanInstitutionFees
>
> I run a method that does something like this:
>
> var studyPlan = this.studyPlanRepository.SelectById(studyPlanId);
> studyPlan.StudyPlanInstitutionFees.Clear();
>
> // ADD SOME NEW FEES HERE
>
> this.studyPlanRepository.Update(studyPlan);
>
> Now the issue is I am deleting existing fees, and I have a unique key
> constraint on InstititutionId in the database, and it is trying to
> insert first. Am I missing something so that it will delete the old
> items first?
>
> Mapping for collection:
>
> <bag name="StudyPlanInstitutionFees" inverse="true" lazy="true"
> cascade="all-delete-orphan">
> <key column="StudyPlanId" />
> <one-to-many
> class="Phoenix.Link2Uni.ApplicationManager.Domain.StudyPlanInstitutionFee,
> Phoenix.Link2Uni.ApplicationManager.Domain" />
> </bag>
>
> <class name="StudyPlanInstitutionFee" table="StudyPlanInstitutionFees"
> lazy="false">
> <!-- Id -->
> <id name="Id" column="Id" type="int">
> <generator class="native" />
> </id>
>
> <!-- Properties -->
> <property name="InstitutionName" column="InstitutionName"
> type="string" not-null="true" />
> <property name="InstitutionId" column="InstitutionId" type="int"
> not-null="true" unique="true" />
> <property name="Fee" column="Fee" type="Decimal" not-null="true" />
> <property name="OrphanFlag" column="OrphanFlag" type="Boolean"
> not-null="true" update="false" insert="false" />
>
> <!-- Many to one mappings -->
> <many-to-one name="StudyPlan" column="StudyPlanId" cascade="none"
> not-null="true" fetch="join" />
> </class>
>
> Cheers
>
> --
> Stefan Sedich
> Software Developerhttp://weblogs.asp.net/stefansedich
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---