The only solution I could come up with is to execute a flush after
clearing the collection like so:

using (var tx = session.BeginTransaction()) {

                var p = this.session.Get<Parent>(1);
                p.Children.Clear();
                session.Flush();

                p.Children.Add(new Child() {Name = "Bob2", Parent = p});
                p.Children.Add(new Child() {Name = "Jones2", Parent = p});
                this.session.Update(p);
                this.session.Flush();

                tx.Commit();

}

Is this all I can do here? I feel this is going to get nasty in my
current application in my scenario :(

On Wed, Nov 26, 2008 at 7:14 PM, codemonkey <[EMAIL PROTECTED]> wrote:
>
> 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
> >
>



-- 
Stefan Sedich
Software Developer
http://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
-~----------~----~----~----~------~----~------~--~---

Reply via email to