Eugene Strizhok
Eugene Strizhok

Reputation: 1188

nhibernate custom collection mapping

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 from 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?

Upvotes: 1

Views: 511

Answers (1)

Eugene Strizhok
Eugene Strizhok

Reputation: 1188

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.

Upvotes: 1

Related Questions