Note that there are some explanatory texts on larger screens.

plurals
  1. POOne to Many Persistence Specification not working
    primarykey
    data
    text
    <p>I'm trying to work out what the persistence specification should be for the following one to many relationship in fluent nhibernate...</p> <p>Sorry for the wall of code... I've tried lots of ways of doing this, none of which have worked...</p> <p>Also would anyone be able to tell me why the first select is emitted each time this (doesn't) run?! This should be incredibly simple but I've spent a couple of hours on it now and I'm just about ready to give up on Fluent NHibernate... might be easier to just write some SQL!!!!!</p> <pre><code>public class Order { public virtual int OrderId { get; set; } private IList&lt;Item&gt; items = new List&lt;Item&gt;(); public virtual IEnumerable&lt;Item&gt; Items { get { return items; } } public virtual void AddItem(Item item) { items.Add(item); item.Order = this; } public virtual void RemoveItem(Item item) { item.Order = null; items.Remove(item); } } public class OrderMap : ClassMap&lt;Order&gt; { public OrderMap() { Id(x =&gt; x.OrderId).GeneratedBy.Assigned(); HasMany(x =&gt; x.Items) .Inverse() .Cascade.All() .Access.CamelCaseField(); } } [TestClass] public class OrderMapTest : BaseDatabaseTest { [TestMethod] public void ShouldMapOrder() { new PersistenceSpecification&lt;Order&gt;(_session) .CheckProperty(x =&gt; x.OrderId, 1) .CheckList(x =&gt; x.Items, new List&lt;Item&gt;() { new Item() { Id = 1, Order = new Order() { OrderId = 1 } } }) .VerifyTheMappings(); //Test Name: ShouldMapOrder //Test Outcome: Failed //Result Message: //Test method Jobs.Test.DatabaseTests.Entity.Base.OrderMapTest.ShouldMapOrder threw exception: //NHibernate.Exceptions.GenericADOException: could not update: [Jobs.Test.DatabaseTests.Entity.Base.Item#1][SQL: UPDATE "Item" SET Order_id = ? WHERE Id = ?] ---&gt; System.Data.SQLite.SQLiteException: constraint failed //foreign key constraint failed //Result StandardOutput: //NHibernate: // SELECT // order_.OrderId // FROM // "Order" order_ // WHERE // order_.OrderId=@p0; // @p0 = 1 [Type: Int32 (0)] //NHibernate: // INSERT // INTO // "Item" // (Order_id, Id) // VALUES // (@p0, @p1); // @p0 = NULL [Type: Int32 (0)], @p1 = 1 [Type: Int32 (0)] //NHibernate: // UPDATE // "Item" // SET // Order_id = @p0 // WHERE // Id = @p1; // @p0 = 1 [Type: Int32 (0)], @p1 = 1 [Type: Int32 (0)] } } public class Item { public virtual int Id { get; set; } public virtual Order Order { get; set; } } public class ItemMap : ClassMap&lt;Item&gt; { public ItemMap() { Id(x =&gt; x.Id).GeneratedBy.Assigned(); References(x =&gt; x.Order); } } [TestClass] public class ItemMapTest : BaseDatabaseTest { [TestMethod] public void ShouldMapItem() { var Order = new Order() { OrderId = 1 }; var Item = new Item() { Id = 1 }; Order.AddItem(Item); new PersistenceSpecification&lt;Item&gt;(_session, new ItemComparer()) .CheckProperty(x =&gt; x.Id, Item.Id) .CheckReference(x =&gt; x.Order, Order) .VerifyTheMappings(); //Test Name: ShouldMapItem //Test Outcome: Failed //Result Message: //Test method Jobs.Test.DatabaseTests.Entity.Base.ItemMapTest.ShouldMapItem threw exception: //NHibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 1, of entity: Jobs.Test.DatabaseTests.Entity.Base.Item //Result StandardOutput: //NHibernate: // SELECT // item_.Id, // item_.Order_id as Order2_0_ // FROM // "Item" item_ // WHERE // item_.Id=@p0; // @p0 = 1 [Type: Int32 (0)] //NHibernate: // INSERT // INTO // "Order" // (OrderId) // VALUES // (@p0); // @p0 = 1 [Type: Int32 (0)] //NHibernate: // INSERT // INTO // "Item" // (Order_id, Id) // VALUES // (@p0, @p1); // @p0 = 1 [Type: Int32 (0)], @p1 = 1 [Type: Int32 (0)] } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload