January 29, 2010 Errors, NHibernate No comments
January 29, 2010 Errors, NHibernate No comments
Error:
I had something like this in the SaveCustomer method:
And this don’t work correctly. Simple change to
resolved my issue.
December 23, 2009 NHibernate No comments
Next SQL, which verifies if customer row exists in database:
Will be generated with next NHibernate Query:
return result.HasValue;
}
Please note, that you could use not only primary key, but any other property in your where condition.
December 9, 2009 NHibernate 4 comments
Sometime ago I faced with NHibernate issue and spent much time on figuring out how to resolve it. Issue was with saving complicated entity.
Error:
With NHibernate Profiler I found that this fails on the next SQL:
With better look you will see that there two times RESOURCE_ID mentioned so this looks like wrong mapping. But I was sure that everything is ok there.
This two References are wrong.
At first glance do you see why? At that moment I was not able also.
References(x => x.ResourceRole)
.Access.AsCamelCaseField(Prefix.Underscore)
.WithColumns(“RESOURCE_ROLE_ID”, “RESOURCE_ID”)
.FetchType.Join();
Here Resource has key (RESOURCE_ID).
ResourceRole has composite key (ROLE_ID and RESOURCE_ID).
After long searching and many tries I found why mapping is not correct. Even have my explanation for it.
When we reference ResourceRole we already use RESOURCE_ID field, so when we setup Resource and say “Hey, there is one more RESOURCE_ID“, then NHibernate cannot find out how to update all this correctly.
Solution:
insert=”false” update=”false” attributes for one of references solves the issue.
In Fluent NHibernate it looks like:
December 7, 2009 NHibernate 2 comments
Image that you want to fetch friends from database by the First Name, Last Name and Age, which are properties of your class Friend. With NHibernate you could write query which will look like:
return friends;
}
But it is wrong. Since Age is the Nullable type (int?), your call GetFriends(“Andriy”,”Buday”,null) will not get my record even if there is such in database and the column AGE is NULL there. So to request you need this: ageExpression = Expression.IsNull(“Age”); but it will not work for not null age.
For our luck there is AbstractCriterion so we can generalize our Expressions like here:
IList<Friend> friends = Session.CreateCriteria(typeof(Friend))
.Add(Expression.Eq(“FirstName”, firstName))
.Add(Expression.Eq(“LastName”, lastName))
.Add(ageExpression)
.List<Friend>();
return friends;
}