I am getting a weird error in NHibernate (v3.3), when trying to persist an entity with a manually generated ID:
Unable to determine if {Entity} with assigned identifier {Id} is transient or detached; querying the database.
Use explicit Save() or Update() in session to prevent this.
But the problem is, I am using Save instead of SaveOrUpdate. What could be the problem?
It turned out that my problem was actually happening while saving the parent entity, containing child entities in a one-to-many relation:
In other words, a call to
Saveon the parent entity caused aSaveOrUpdateon child entities, which NHibernate was complaining about.When I realized that, I quickly found this StackOverflow thread: How to save a child with assigned id in nhibernate, which has two great suggestions:
Create and map a Version or Timestamp column – if it’s null, NHibernate will know it needs to persist the entity, or
Attach a custom Interceptor to a session (or session factory) and use a custom private field to keep track of whether the entity needs to be persisted.