Skip to content

NH-3931 - Invalid order of child inserts #582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/NHibernate.Test/Insertordering/AnimalModel/Animal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace NHibernate.Test.Insertordering.AnimalModel
{
public class Animal
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual Person Owner { get; set; }
}
}
7 changes: 7 additions & 0 deletions src/NHibernate.Test/Insertordering/AnimalModel/Cat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace NHibernate.Test.Insertordering.AnimalModel
{
public class Cat : Animal
{
public virtual string EyeColor { get; set; }
}
}
7 changes: 7 additions & 0 deletions src/NHibernate.Test/Insertordering/AnimalModel/Dog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace NHibernate.Test.Insertordering.AnimalModel
{
public class Dog : Animal
{
public virtual string Country { get; set; }
}
}
96 changes: 96 additions & 0 deletions src/NHibernate.Test/Insertordering/AnimalModel/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System.Collections;
using NHibernate.Cfg;
using NUnit.Framework;

namespace NHibernate.Test.Insertordering.AnimalModel
{
[TestFixture]
public class Fixture : TestCase
{
protected override IList Mappings
{
get { return new[] { "Insertordering.AnimalModel.Mappings.hbm.xml" }; }
}

protected override string MappingsAssembly
{
get { return "NHibernate.Test"; }
}

protected override void Configure(Configuration configuration)
{
configuration.DataBaseIntegration(x =>
{
x.BatchSize = 10;
x.OrderInserts = true;
});
}

protected override void OnTearDown()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
session.Delete("from Animal");
session.Delete("from Person");
tran.Commit();
}
}

[Test]
public void ElaboratedModel()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
var personWithAnimals = new PersonWithAnimals { Name = "fabio" };
var personWithCats = new PersonWithCats { Name = "dario" };
var personWithSivasKangals = new PersonWithSivasKangals { Name = "tuna" };
var personWithDogs = new PersonWithDogs { Name = "davy" };

var animalForAnimals = new Animal { Name = "Pasha", Owner = personWithAnimals };
var dogForAnimals = new Dog { Name = "Efe", Country = "Turkey", Owner = personWithAnimals };
var catForAnimals = new Cat { Name = "Tekir", EyeColor = "green", Owner = personWithAnimals };
var sivasKangalForAnimals = new SivasKangal { Name = "Karabas", Country = "Turkey", HouseAddress = "Atakoy", Owner = personWithAnimals };

personWithAnimals.AnimalsGeneric.Add(animalForAnimals);
personWithAnimals.AnimalsGeneric.Add(dogForAnimals);
personWithAnimals.AnimalsGeneric.Add(catForAnimals);
personWithAnimals.AnimalsGeneric.Add(sivasKangalForAnimals);

var animalForCats = new Animal { Name = "Pasha2", Owner = personWithCats };
var catForCats = new Cat { Name = "Tekir2", EyeColor = "green", Owner = personWithCats };
var dogForCats = new Dog { Name = "Efe2", Country = "Turkey", Owner = personWithCats };
personWithCats.AnimalsGeneric.Add(catForCats);

var catForDogs = new Cat { Name = "Tekir3", EyeColor = "blue", Owner = personWithDogs };
var dogForDogs = new Dog { Name = "Efe3", Country = "Turkey", Owner = personWithDogs };
var sivasKangalForDogs = new SivasKangal { Name = "Karabas3", Country = "Turkey", HouseAddress = "Atakoy", Owner = personWithDogs };
personWithDogs.AnimalsGeneric.Add(dogForDogs);
personWithDogs.AnimalsGeneric.Add(sivasKangalForDogs);

var animalForSivasKangals = new Animal { Name = "Pasha4", Owner = personWithSivasKangals };
var dogForSivasKangals = new Dog { Name = "Efe4", Country = "Turkey", Owner = personWithSivasKangals };
var catForSivasKangals = new Cat { EyeColor = "red", Name = "Tekir4", Owner = personWithSivasKangals };
var sivasKangalForSivasKangals = new SivasKangal { Name = "Karabas4", Country = "Turkey", HouseAddress = "Atakoy", Owner = personWithSivasKangals };
personWithSivasKangals.AnimalsGeneric.Add(sivasKangalForSivasKangals);

session.Save(animalForCats);
session.Save(dogForCats);

session.Save(catForDogs);

session.Save(animalForSivasKangals);
session.Save(dogForSivasKangals);
session.Save(catForSivasKangals);

session.Save(personWithAnimals);
session.Save(personWithCats);
session.Save(personWithDogs);
session.Save(personWithSivasKangals);

Assert.DoesNotThrow(() => { tran.Commit(); });
}
}
}
}
65 changes: 65 additions & 0 deletions src/NHibernate.Test/Insertordering/AnimalModel/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernate.Test"
namespace="NHibernate.Test.Insertordering.AnimalModel">

<class name="Animal" discriminator-value="0">
<id name="Id" generator="guid.comb" />
<discriminator column="AnimalType" type="int" force="true"/>
<property name="Name" type="string"/>
<many-to-one name="Owner" class="Person" column="OwnerId"/>
<subclass name="Cat" discriminator-value="1"/>
<subclass name="Dog" discriminator-value="2">
<property name="Country"/>
<subclass name="SivasKangal" discriminator-value="3">
<property name="HouseAddress"></property>
</subclass>
</subclass>
</class>
<class name="Person" discriminator-value="0">
<id name="Id" generator="guid.comb" />

<discriminator column="PersonType" type="int"/>
<property name="Name"/>
<bag name="AnimalsGeneric" lazy="true" inverse="true" cascade="save-update">
<key column="OwnerId" />
<one-to-many class="Animal"/>
</bag>
<subclass name="PersonWithAnimals" discriminator-value="1">

</subclass>
<subclass name="PersonWithCats" discriminator-value="2">
<bag name="CatsGeneric" lazy="true" inverse="true" cascade="save-update">
<key column="OwnerId" />
<one-to-many class="Cat"/>
</bag>
</subclass>
<subclass name="PersonWithDogs" discriminator-value="3">
<bag name="DogsGeneric" lazy="true" inverse="true" cascade="save-update">
<key column="OwnerId" />
<one-to-many class="Dog"/>
</bag>
</subclass>
<subclass name="PersonWithSivasKangals" discriminator-value="4">
<bag name="SivasKangalsGeneric" lazy="true" inverse="true" cascade="save-update">
<key column="OwnerId" />
<one-to-many class="SivasKangal"/>
</bag>

</subclass>
<subclass name="PersonWithAllTypes" discriminator-value="5">
<bag name="DogsGeneric" lazy="true" inverse="true">
<key column="OwnerId" />
<one-to-many class="Dog"/>
</bag>
<bag name="CatsGeneric" lazy="true" inverse="true">
<key column="OwnerId" />
<one-to-many class="Cat"/>
</bag>
<bag name="SivasKangalsGeneric" lazy="true" inverse="true">
<key column="OwnerId" />
<one-to-many class="SivasKangal"/>
</bag>
</subclass>
</class>
</hibernate-mapping>
12 changes: 12 additions & 0 deletions src/NHibernate.Test/Insertordering/AnimalModel/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;

namespace NHibernate.Test.Insertordering.AnimalModel
{
public class Person
{
public virtual Guid Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<Animal> AnimalsGeneric { get; set; } = new List<Animal>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;

namespace NHibernate.Test.Insertordering.AnimalModel
{
public class PersonWithAllTypes : Person
{
public virtual IList<Dog> DogsGeneric { get; set; }
public virtual IList<SivasKangal> SivasKangalsGeneric { get; set; }
public virtual IList<Cat> CatsGeneric { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace NHibernate.Test.Insertordering.AnimalModel
{
public class PersonWithAnimals : Person
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace NHibernate.Test.Insertordering.AnimalModel
{
public class PersonWithCats : Person
{
public virtual IList<Cat> CatsGeneric { get; set; } = new List<Cat>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace NHibernate.Test.Insertordering.AnimalModel
{
public class PersonWithDogs : Person
{
public virtual IList<Dog> DogsGeneric { get; set; } = new List<Dog>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace NHibernate.Test.Insertordering.AnimalModel
{
public class PersonWithSivasKangals : Person
{
public virtual IList<SivasKangal> SivasKangalsGeneric { get; set; } = new List<SivasKangal>();
}
}
7 changes: 7 additions & 0 deletions src/NHibernate.Test/Insertordering/AnimalModel/SivasKangal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace NHibernate.Test.Insertordering.AnimalModel
{
public class SivasKangal : Dog
{
public virtual string HouseAddress { get; set; }
}
}
98 changes: 98 additions & 0 deletions src/NHibernate.Test/Insertordering/FamilyModel/Fixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System.Collections;
using System.Linq;
using NHibernate.Cfg;
using NHibernate.Linq;
using NUnit.Framework;

namespace NHibernate.Test.Insertordering.FamilyModel
{
[TestFixture]
public class Fixture : TestCase
{
protected override IList Mappings
{
get { return new[] { "Insertordering.FamilyModel.Mappings.hbm.xml" }; }
}

protected override string MappingsAssembly
{
get { return "NHibernate.Test"; }
}

protected override void Configure(Configuration configuration)
{
configuration.DataBaseIntegration(x =>
{
x.BatchSize = 10;
x.OrderInserts = true;
// Uncomment batcher lines here and in OnSetUp and OnTearDown for debugging purpose.
//x.Batcher<InsertOrderingFixture.StatsBatcherFactory>();
});
}

protected override void OnSetUp()
{
/*
InsertOrderingFixture.StatsBatcher.Reset();
InsertOrderingFixture.StatsBatcher.StatsEnabled = true;
*/
}

protected override void OnTearDown()
{
//InsertOrderingFixture.StatsBatcher.StatsEnabled = false;

using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
session.Delete("from System.Object");
tran.Commit();
}
}

[Test]
public void CircularReferences()
{
using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
var jeanne = new Woman { Name = "Jeanne" };
var paul = new Man { Name = "Paul" };

var paulette = new Woman { Name = "Paulette", Mother = jeanne, Father = paul };
var monique = new Woman { Name = "Monique", Mother = jeanne, Father = paul };
var alice = new Woman { Name = "Alice", Mother = jeanne, Father = paul };
var yves = new Man { Name = "Yves", Mother = jeanne, Father = paul };
var denis = new Man { Name = "Denis", Mother = jeanne, Father = paul };

var laure = new Woman { Name = "Laure", Mother = paulette };
var valerie = new Woman { Name = "Val�rie", Mother = paulette };
var caroline = new Woman { Name = "Caroline", Mother = monique };
var cathy = new Woman { Name = "Cathy", Father = yves };
var helene = new Woman { Name = "H�l�ne", Father = yves };
var nicolas = new Man { Name = "Nicolas", Mother = monique };
var frederic = new Man { Name = "Fr�d�ric", Mother = monique };
var arnaud = new Man { Name = "Arnaud", Father = denis };

session.Save(alice);
session.Save(laure);
session.Save(valerie);
session.Save(caroline);
session.Save(cathy);
session.Save(helene);
session.Save(nicolas);
session.Save(frederic);
session.Save(arnaud);

Assert.DoesNotThrow(() => { tran.Commit(); });
}

using (var session = OpenSession())
using (var tran = session.BeginTransaction())
{
Assert.AreEqual(9, session.Query<Woman>().Count());
Assert.AreEqual(6, session.Query<Man>().Count());
}
}
}
}
21 changes: 21 additions & 0 deletions src/NHibernate.Test/Insertordering/FamilyModel/Mappings.hbm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="NHibernate.Test"
namespace="NHibernate.Test.Insertordering.FamilyModel">

<class name="Woman">
<id name="Id" generator="guid.comb" />

<property name="Name"/>
<many-to-one name="Mother" cascade="save-update" />
<many-to-one name="Father" cascade="save-update" />
</class>

<class name="Man">
<id name="Id" generator="guid.comb" />

<property name="Name"/>
<many-to-one name="Mother" cascade="save-update" />
<many-to-one name="Father" cascade="save-update" />
</class>
</hibernate-mapping>
Loading