开发者

nhibernate custom collection mapping

开发者 https://www.devze.com 2023-04-12 07:59 出处:网络
How can I map a collection property of a concrete class, not an interface? It\'s got to be concrete class! I have no control over the class I want to map, so I can\'t change to interface.

How can I map a collection property of a concrete class, not an interface? It's got to be concrete class! I have no control over the class I want to map, so I can't change to interface.

Right now I'm trying to solve this by writing a custom IUserCollectionType implementation and a custom IPersistentCollection implementation.

But... The following exception has stopped mu progress:

Test method ShouldSaveEntityWithSections threw exception: 
NHibernate.StaleStateException: Batch update returned unexpected row count fro开发者_Python百科m update;  actual row count: 0; expected: 1

Profiler shows that NHibernate doesn't try to insert related entity into the database, but tries to update it's foreign key to parent object.

The mapping is like this:

<set name="Rows" table="Rows" lazy="false" cascade="all"
     collection-type="My.PersistentListType`1[Blabla.Row, Blabla], My">
  <key column="ParentID" not-null="true" />
  <one-to-many class="Blabla.Row, Blabla" />
</set>

What's going on? Why doesn't NHibernate insert child entities into db?


Answering my own question. Hope it will help someone, who experience same problem.

That person should check that he\she doesn't create a related object in a state, which results in NHibernate thinking that it's already persisted.

In my case I mistakenly set a property mapped as primary key to Guid.NewGuid() instead of Guid.Empty.

0

精彩评论

暂无评论...
验证码 换一张
取 消