Thursday, July 24, 2014

null id in entry (don't flush the Session after an exception occurs)

Sample Exception :  ( This example is using postgresql 9.3)


null id in YourModelNamespace.Model entry (don't flush the Session after an exception occurs)
NHibernate.AssertionFailure was caught
HResult=-2146232832
Message=null id in YourModelNamespace.Model  entry (don't flush the Session after an exception occurs)
Source=NHibernate
StackTrace:
at NHibernate.Event.Default.DefaultFlushEntityEventListener.CheckId(Object obj, IEntityPersister persister, Object id, EntityMode entityMode)
at NHibernate.Event.Default.DefaultFlushEntityEventListener.GetValues(Object entity, EntityEntry entry, EntityMode entityMode, Boolean mightBeDirty, ISessionImplementor session)
at NHibernate.Event.Default.DefaultFlushEntityEventListener.OnFlushEntity(FlushEntityEvent event)
at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEntities(FlushEvent event)
at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEverythingToExecutions(FlushEvent event)
at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
at NHibernate.Impl.SessionImpl.Flush()
at NHibernate.Transaction.AdoTransaction.Commit()
at YourNamespace.UnitOfWork.NHibernateUnitOfWork.Dispose(Boolean disposing) in c:\Users\..........\UnitOfWork\NHibernateUnitOfWork.cs:line 78
at YourNamespace.UnitOfWork.NHibernateUnitOfWork.Dispose() in c:\Users\..............\UnitOfWork\NHibernateUnitOfWork.cs:line 36
at sourcenamespace.class.method(input parameters ) in c:\Users\hemant\.......\mycalss.cs:line 82
at wrapper.cs in c:\Users\hemant\.....\api\someController.cs:line 134
InnerException:

 

Possible Reasons / Solutions : 

1.  uninstall  FluentNHibernate  ,  NHibernate ,  Iesi.Collections . install FluentNHibernate   , it will automatically install correct version of NHibernate and Iesi.Collections required . Check if your app.config / web.config have wrong versions of dll's referred , may be under runtime .

2. Generally  in nhiberante mappings you define id field for the table like :

Id(x => x.somecolumn).GeneratedBy.Assigned().Not.Nullable();   // reflection to determine column name so its ok to skip .column here

//or

Id(x => x.SomeColumn).GeneratedBy.Assigned().Column("\"SomeColumn\"").Not.Nullable();

//In the above two cases , your own code is responsible to maintain unique id . So used //Assigned

//or

Id(x => x.SomeColumn).GeneratedBy.Assigned().Column("some_column").Not.Nullable();

//snake_case  so ok to skip  \"

//or

Id(x => x.Id).GeneratedBy.Sequence("\"My_Id_seq\"").Column("\"Id\"");

// in the above case you have a Sequence to generate unique ids / primary key  in db

or else

The idea is after GeneratedBy  make sure you have chosen right option as per your table  e.g.

  • Assigned :   lets the application to assign an identifier to the object before Save()  is called. ( with or without Params configuration )

  • Foreign  :  uses the identifier of another associated object. Usually used in conjunction  with a one-to-one primary key association.

  • HiLo :   uses a hi/lo algorithm to efficiently generate identifiers of any integral type, given a table and column (by default hibernate_unique_key and next_hi respectively) as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database. Do not use this generator with a user-supplied connection. requires a "special" database table to hold the next available "hi" value

  • Identity  :   supports identity columns in DB2, MySQL, MS SQL Server and Sybase. The identifier returned by the database is converted to the property type using Convert.ChangeType. Any integral property type is thus supported.

  • Increment :   generates identifiers of any integral type that are unique only when no other process is inserting data into the same table. Do not use in a cluster.

  • Sequence :  uses a sequence in DB2, PostgreSQL, Oracle or a generator in Firebird. The identifier returned by the database is converted to the property type using Convert.ChangeType. Any integral property type is thus supported.


See FluentNHibernate.Mapping.IdentityGenerationStrategyBuilder  for more possible options.

 

 

 

 

 

 

No comments:

Post a Comment