devnet wrote:
Thank you very much for your reply.
However i think this is extremely bad for performance.
2 TRIPS TO MAKE A DELETE ???.
Again, the entities are managed by NHibernate through the cash, so they must first be loaded then deleted. Enlisting a command for the delete wil bypass this.
devnet wrote:
Also and this is because I am new to Nhibernate.That sometimes if you are not careful how you code it will make an insert instead of an update.
I believe that you have to use the same instantiation of your class otherwise it beleives its a new entity and perform an insert instead of an update.
Have you had it before?
Right, if you create a new instance it is a different entity. NHibernate serves as your repository and you use it to manage persitable entities. If you have a new instance and the ID is not set yet, it's new and saving it will add it to the DB. You can put unique constraints in your mapping if you have properties that are unique given a particular class (without having to add constraint in database itself). I can't remember when I've accidentally inserted something last, but I've been using NHibernate fro about 3+ years now. Just remember, an entity is identified by it's reference, not it's attributes. If there's no ID, it's new...if you pulled it from the repository, it's a pre-existing object.
devnet wrote:
Is it possible to retrieve things from the cache without having a network trip to the database?If so where can I find an example?
Most definitely...in fact this is one of the great strengths of NHibernate. Once you insert an item or retrieve an item from the repository it is in the cash by default (unless you evict the object or clear the session). You should read about the difference of Get and Load. Also, you can set the session's FlushMode so that data is only flushed to the database on Commits if you like. Above all I'd recommend handling all saves, deletes, etc. in transactions and deal with well defined units of work.
Once you get the hang of it, you'll see that you can often reduce round trips thanks to the cache. I'd recommend setting up a log for NHibernate using log4net and setting it to debug. You can see exactly what's going on on the back end.