Lazy loading for NHibernate using Ignore.NotFound

I have a mapping similar to the following for the Candidate object:

References(x => x.Country).Column("CountryId").NotFound().Ignore() 

the problem is that if I select * Candidates, I get an additional choice for each of them, this is not very good, so I pull out the NotNound () bit. Ignore (), but now the following code fails with an ObjectNotFoundException exception

 if (entity.Country != null) { bos.CountryName = entity.Country.Name; } 

Is there a way to get Hhibernate to make a choice when I compare County! = Null?

Thanks,

+4
source share
1 answer

When you specify .NotFound (). Ignore (), this forces the object to load and cannot be overridden with .LazyLoad (). NHibernate does this because it needs to be sure that the relationship exists or does not exist, since you do not rely on the database to enforce it.

My suggestion was to either catch an ObjectNotFoundException or fix your data so that you don't have these inconsistencies.

Here is an article about it: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2753

+5
source

Source: https://habr.com/ru/post/1348091/


All Articles