RicL
RicL

Reputation: 553

NHibernate.ObjectNotFoundException: No row with the given identifier exists

Here's my error:

NHibernate.ObjectNotFoundException: No row with the given identifier exists[Project.Core.Entities.User#(GUID)]
at Hibernate.Impl.SessionFactoryImpl.DefaultEntityNotFoundDelegate.HandleEntityNotFound(String entityName, Object id) 
at NHibernate.Event.Default.DefaultLoadEventListener.Load(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)

One of the users of the system chanced upon getting this error, which doesn't make sense to me entirely. What I do know:

  1. A Teacher table REFERENCES a User table.
  2. Somehow the Teacher table has a row that references to a row in the User's table, but that row doesn't exist in the User's table.

Any idea why is this happening? Please advise!

Rephrase my Qn:

I know what does the error meant, however I don't seem to figure out what caused the missing user row? I have placed it all on Cascade.ALL. I don't think it's related to deletion. What are the possible scenarios the above may happen?

Edit 2:

Please refer to the mapping via FNH: Any issues with this?

public void Override(AutoMapping<Teacher> mapping)
    {
        mapping.References(x => x.User).Cascade.All().Not.LazyLoad();
    }

Thanks!

Upvotes: 5

Views: 11216

Answers (5)

Mark
Mark

Reputation: 599

If you are using a .hbm.xml mapping file then the "not found" setting can be applied in there to the mapping, e.g.

<many-to-one name="User" column="User" not-found="ignore" />

Upvotes: 0

Volodymyr Kotylo
Volodymyr Kotylo

Reputation: 493

In Fluent NHibernate for C#, I've used

HasManyToMany(x => x.Bars).Table("foobars")
                        .ParentKeyColumn("FooId")
                        .ChildKeyColumn("Id")
                        .NotFound.Ignore();

Upvotes: 3

Rodolpho Brock
Rodolpho Brock

Reputation: 8145

Try that...

public void Override(ModelMapper modelMapper) {
    modelMapper.Class<T>(c => { 
        c.ManyToOne(m => m.FKObj, r => {
            r.Column("FKColumn");
            r.NotFound(NotFoundMode.Ignore); // THIS IS IMPORTANT!!!
        });
    });
}

Or this... if you have an "Conventions" file...

First handles the BeforeMapManyToOne event:

mapper.BeforeMapManyToOne += Mapper_OnBeforeMapManyToOne;

And then set the NotFound to Ignore by default...

private static void Mapper_OnBeforeMapManyToOne(IModelInspector modelInspector, PropertyPath propertyPath, IManyToOneMapper manyToOneMapper) {
    manyToOneMapper.NotFound(NotFoundMode.Ignore);
}

Upvotes: 0

MoMo
MoMo

Reputation: 8200

I set the 'Not Found' property to 'Ignore'

Upvotes: 4

Peter
Peter

Reputation: 27944

I guess you are missching a FK constrain in your database, otherwise you will not have any missing records on a FK relation. Add the FK and Cascade rules in your database, then you cann't have any 'missing row exceptions'. A error will be raised when you try to do something which is not in line with your database model.

Upvotes: 1

Related Questions