You probably need to do an id check yourself first, before the IsDirty. John Davidson
On Thu, Oct 7, 2010 at 7:32 AM, cremor <cre...@gmx.net> wrote: > Sadly, a different ID generator is no option for me. My application is > not the only one accessing the DB and the other systems rely on these > sequences. > > What is the reason for getting an ID and flushing the Child when > checking IsDirty()? Shouldn't it be enough for NHibernate that the ID > of the Child has an unsaved-value? > > On 7 Okt., 12:56, John Davidson <jwdavid...@gmail.com> wrote: > > The use of sequence is similar to identity in that the db generates the > id > > stored rather than the other id generators which create the id within > > NHibernate. For your problem the issue identified in jira NH-2136 is the > > same. > > > > The solution is to use an NHibernate created id from hi-lo or guid based > > generators. > > > > John Davidson > > > > On Thu, Oct 7, 2010 at 4:09 AM, cremor <cre...@gmx.net> wrote: > > > I currently have the problem that NHibernate creates an insert > > > statement for new items in a collection of a persistent entity when > > > calling session.IsDirty(). > > > > > My configuration: > > > * NHibernate 3.0.0.Alpha3 > > > * An entity "Parent" with a collection of "Child" entities. One-to- > > > many side is mapped as set, inverse, lazy and cascade all-delete- > > > orphan. Many-to-one side is mapped as lazy. > > > * All IDs are using the sequence generator. > > > * Optimistic lock is by version column. > > > * Session.FlushMode is Never. > > > > > My use-case: > > > 1. Persistent Parent (already containing some Child entities) is > > > loaded. > > > 2. A new Child is added to the collection. > > > 3. session.IsDirty() is called (UI checks, if it should enable the > > > save button). > > > 4. NHibernate executes a select statement for the ID of the new Child > > > and queues an insert statement for the Child using its current > > > (default) values. (Problem starts here: Why is it needed to get the ID > > > of the new Child and queue an insert statement?) > > > 4a. (Optional step) Some more new Childs are added. No more select > > > statements are executed or insert statements are queued when calling > > > session.IsDirty() because IsDirty() returns early because it knows it > > > already has queued statements (very inconsistent behaviour in my > > > opinion!) > > > 5. User has to change a property of the Child to get rid of UI > > > validation errors (a property is not allowed to be empty, but is by > > > default to force the user to input something meaningful). > > > 6. User saves the changes, session.Flush() is called. > > > > > Now NHibernate does the following: > > > 1. Select statements for the new IDs of additional Childs from step 4a > > > are executed. > > > 2. The queued insert statement for the first new Child is executed. > > > This statement uses the default values of the class and fails with an > > > not null constraint error. > > > 3. (If I disable the not null check in the DB) Parent version column > > > is updated. > > > 4. Additional childs from step 4a are inserted (with the already > > > changed properties, so these statements are ok). > > > 5. The first new Child is updated with the changed property. > > > > > I found the following Jira issue which already describes the problem, > > > but it was rejected because it's "Expected behavior using identity": > > >http://216.121.112.228/browse/NH-2136 > > > But I'm using sequence, not identity, and still have the same problem. > > > > > Does anyone know if this is a bug at my side, a bug in NHibernate or > > > just "by design" like for identiy IDs? > > > > > -- > > > You received this message because you are subscribed to the Google > Groups > > > "nhusers" group. > > > To post to this group, send email to nhus...@googlegroups.com. > > > To unsubscribe from this group, send email to > > > nhusers+unsubscr...@googlegroups.com<nhusers%2bunsubscr...@googlegroups.com> > <nhusers%2bunsubscr...@googlegroups.com<nhusers%252bunsubscr...@googlegroups.com> > > > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/nhusers?hl=en. > > > > > > -- > You received this message because you are subscribed to the Google Groups > "nhusers" group. > To post to this group, send email to nhus...@googlegroups.com. > To unsubscribe from this group, send email to > nhusers+unsubscr...@googlegroups.com<nhusers%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/nhusers?hl=en. > > -- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to nhus...@googlegroups.com. To unsubscribe from this group, send email to nhusers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.