Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHIbernate refreshing a collection

I know this has been asked, but I can not solve this problem.

Lets say you have an nhibernate object that has a collection.

Problem is, if some object in the collection is updated in the database by a different user (i manually modifiy the database fot testing purposes), I can't seem to find a way to make nhibernate refresh the collection value.

I hace tried refresh, evict, Loading againg... Only closing the session and creating a new one works. But I find this solution problematic and, how hard can it be to tell nhibernate "dude, refresh the items in the collection"?

But anyhow I can't get it working.

Thanks a lot

like image 320
Daniel Dolz Avatar asked Nov 12 '12 19:11

Daniel Dolz


1 Answers

This isn't actually supposed to work, that's why it doesn't.

NHibernate uses the a session level cache by default, to optimise reads and track modifications.

It sounds like you're not using a correct unit of work around your data access as your sessions should be scoped down and used only when you need them.

If this is part of a web app, it's recommended you follow a "session-per-request" approach.

If you really wanted this to work, you could probably evict entity and it's children from the session, forcing a re-fetch, but this is nasty and I'd really not advise it.

like image 74
DavidWhitney Avatar answered Sep 30 '22 16:09

DavidWhitney