December 9, 2009 NHibernate
December 9, 2009 NHibernate
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:
Markdown | Result |
---|---|
*text* | text |
**text** | text |
***text*** | text |
`code` | code |
~~~ more code ~~~~ |
more code |
[Link](https://www.example.com) | Link |
* Listitem |
|
> Quote | Quote |
Great post, saved me a bunch of time. In Fluent NH it ended up looking like this:
References(x => x.TableName).Column("ColumnName").Not.Insert().Not.Update();
Thank you… great to hear that things I write help others..
Thank you so much for this post! It helped a lot :)
I'm glad it helped!