{"action":"created","issue":{"url":"https://api.github.com/repos/nhibernate/nhibernate-core/issues/1738","repository_url":"https://api.github.com/repos/nhibernate/nhibernate-core","labels_url":"https://api.github.com/repos/nhibernate/nhibernate-core/issues/1738/labels{/name}","comments_url":"https://api.github.com/repos/nhibernate/nhibernate-core/issues/1738/comments","events_url":"https://api.github.com/repos/nhibernate/nhibernate-core/issues/1738/events","html_url":"https://github.com/nhibernate/nhibernate-core/issues/1738","id":330631587,"node_id":"MDU6SXNzdWUzMzA2MzE1ODc=","number":1738,"title":"Refresh of locally removed collection item crashes with \"instance was not in a valid state\"","user":{"login":"RobertSimonsSMA","id":40061016,"node_id":"MDQ6VXNlcjQwMDYxMDE2","avatar_url":"https://avatars2.githubusercontent.com/u/40061016?v=4","gravatar_id":"","url":"https://api.github.com/users/RobertSimonsSMA","html_url":"https://github.com/RobertSimonsSMA","followers_url":"https://api.github.com/users/RobertSimonsSMA/followers","following_url":"https://api.github.com/users/RobertSimonsSMA/following{/other_user}","gists_url":"https://api.github.com/users/RobertSimonsSMA/gists{/gist_id}","starred_url":"https://api.github.com/users/RobertSimonsSMA/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RobertSimonsSMA/subscriptions","organizations_url":"https://api.github.com/users/RobertSimonsSMA/orgs","repos_url":"https://api.github.com/users/RobertSimonsSMA/repos","events_url":"https://api.github.com/users/RobertSimonsSMA/events{/privacy}","received_events_url":"https://api.github.com/users/RobertSimonsSMA/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-06-08T12:03:55Z","updated_at":"2018-12-30T21:46:34Z","closed_at":null,"author_association":"NONE","body":"When removing an item from a mapped collection in one ISession, and refreshing the collection state in another session NHibernate crashes with \"instance was not in a valid state\".\r\n\r\nCrash was reproduced in NHibernate 4.0.x\r\nCode snippet to reproduce the crash:\r\n```\r\n\tDataItemWithSubItems item;\r\n\tusing (ISession session = sessionFactory.OpenSession()) {\r\n\t\t item = session.Get<DataItemWithSubItems>(1);\r\n\r\n\t\tAssert.That(item.SubItems.Count, Is.EqualTo(3));\r\n\t\tAssert.That(item.SubItems[0].Name, Is.EqualTo(\"Name 101\"));\r\n\t\tAssert.That(item.SubItems[1].Name, Is.EqualTo(\"Name 102\"));\r\n\t\tAssert.That(item.SubItems[2].Name, Is.EqualTo(\"Name 103\"));\r\n\r\n\t\titem.SubItems.RemoveAt(1);\r\n\t\tAssert.That(item.SubItems.Count, Is.EqualTo(2));\r\n\t}\r\n\r\n\tusing (ISession session = sessionFactory.OpenSession()) {\r\n\t\tsession.Update(item);\r\n\r\n\t\tsession.Refresh(item);\r\n\t\tforeach (var rawItem in item.SubItems) {\r\n\t\t\tsession.Refresh(rawItem); // <-- it crashes here.\r\n\t\t}\r\n\r\n\t\tAssert.That(item.SubItems.Count, Is.EqualTo(3));\r\n\t\tAssert.That(item.SubItems[0].Name, Is.EqualTo(\"Name 101\"));\r\n\t\tAssert.That(item.SubItems[1].Name, Is.EqualTo(\"Name 102\"));\r\n\t\tAssert.That(item.SubItems[2].Name, Is.EqualTo(\"Name 103\"));\r\n\t}\r\n```\r\n\r\nStacktrace:\r\n```\r\nNHibernate.HibernateException : instance was not in a valid state\r\n   bei NHibernate.Engine.EntityEntry.get_IsReadOnly() in d:\\Projects\\NHibernate\\nhibernate-core\\src\\NHibernate\\Engine\\EntityEntry.cs:Zeile 293.\r\n   bei NHibernate.Event.Default.DefaultRefreshEventListener.OnRefresh(RefreshEvent event, IDictionary refreshedAlready) in d:\\Projects\\NHibernate\\nhibernate-core\\src\\NHibernate\\Event\\Default\\DefaultRefreshEventListener.cs:Zeile 115.\r\n   bei NHibernate.Impl.SessionImpl.FireRefresh(RefreshEvent refreshEvent) in d:\\Projects\\NHibernate\\nhibernate-core\\src\\NHibernate\\Impl\\SessionImpl.cs:Zeile 2492.\r\n   bei NHibernate.Impl.SessionImpl.Refresh(Object obj) in d:\\Projects\\NHibernate\\nhibernate-core\\src\\NHibernate\\Impl\\SessionImpl.cs:Zeile 1379.\r\n   [User Code]\r\n```\r\n\r\nWorkaround: use own IRefreshEventListener that does not evaluate EntityEntry.IsReadOnly for the locally deleted item.\r\n\r\nmapped classes:\r\n```\r\n\tpublic class DataItemWithSubItems {\r\n\t\tpublic int SID { get; private set; }\r\n\t\tpublic string Name { get; set; }\r\n\t\tpublic IList<SubItem> SubItems { get; private set; }\r\n\t}\r\n\tpublic class SubItem {\r\n\t\tpublic int SID { get; private set; }\r\n\t\tpublic string Name { get; set; }\r\n\r\n\t\tpublic DataItemWithSubItems Parent { get; private set; }\r\n\t}\r\n```\r\nMapping files:\r\n```\r\n  <class name=\"NHibernateRefreshEventListener.DataItemWithSubItems, NHibernateRefreshEventListener\" table=\"MAIN_TABLE\" dynamic-update=\"true\" lazy=\"false\">\r\n    <id name=\"SID\" type=\"Int32\" column=\"SID\" unsaved-value=\"-1\">\r\n      <generator class=\"hilo\" />\r\n    </id>\r\n    <property name=\"Name\" column=\"NAME\" length=\"50\" not-null=\"false\"/>\r\n\r\n    <bag\r\n      name=\"SubItems\" inverse=\"true\" lazy=\"false\" cascade=\"all-delete-orphan\">\r\n      <key column=\"PARENT_SID\"/>\r\n      <one-to-many class=\"NHibernateRefreshEventListener.SubItem, NHibernateRefreshEventListener\" />\r\n    </bag>\r\n  </class>\r\n  \r\n    <class name=\"NHibernateRefreshEventListener.SubItem, NHibernateRefreshEventListener\" table=\"SUB_TABLE\" dynamic-update=\"true\" lazy=\"false\">\r\n    <id name=\"SID\" type=\"Int32\" column=\"SID\" unsaved-value=\"-1\">\r\n      <generator class=\"hilo\" />\r\n    </id>\r\n    <property name=\"Name\" column=\"NAME\" length=\"50\" not-null=\"false\"/>\r\n    <many-to-one name=\"Parent\" class=\"NHibernateRefreshEventListener.DataItemWithSubItems, NHibernateRefreshEventListener\" column=\"PARENT_SID\" />\r\n  </class>\r\n\r\n```\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/nhibernate/nhibernate-core/issues/comments/450588824","html_url":"https://github.com/nhibernate/nhibernate-core/issues/1738#issuecomment-450588824","issue_url":"https://api.github.com/repos/nhibernate/nhibernate-core/issues/1738","id":450588824,"node_id":"MDEyOklzc3VlQ29tbWVudDQ1MDU4ODgyNA==","user":{"login":"MrGroovy","id":7824288,"node_id":"MDQ6VXNlcjc4MjQyODg=","avatar_url":"https://avatars1.githubusercontent.com/u/7824288?v=4","gravatar_id":"","url":"https://api.github.com/users/MrGroovy","html_url":"https://github.com/MrGroovy","followers_url":"https://api.github.com/users/MrGroovy/followers","following_url":"https://api.github.com/users/MrGroovy/following{/other_user}","gists_url":"https://api.github.com/users/MrGroovy/gists{/gist_id}","starred_url":"https://api.github.com/users/MrGroovy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MrGroovy/subscriptions","organizations_url":"https://api.github.com/users/MrGroovy/orgs","repos_url":"https://api.github.com/users/MrGroovy/repos","events_url":"https://api.github.com/users/MrGroovy/events{/privacy}","received_events_url":"https://api.github.com/users/MrGroovy/received_events","type":"User","site_admin":false},"created_at":"2018-12-30T21:46:34Z","updated_at":"2018-12-30T21:46:34Z","author_association":"NONE","body":"I was mistaken i think, when i read the asserts better the item should not be removed. But with the `Flush` the item does get removed. Sorry."}}
{"actor":{"display_login":"MrGroovy"}}