Does NHibernate have in-memory entities for updates

I am new to NHibernate. We are using Dapper for retrieval and planning to use NHibernate for CRUD.

I am trying to remove the child object through parent list object in one to many relationship.

This works when I retrieve the object using NHibernate session and remove the item.

var mercedes = Read("chevrolet"); //queries from nhibernate session
var model = mercedes.Models.Where(c => c.Id == 181).Single(); 
mercedes.Models.Remove(model);

When I manually create the object and attach thecars models, it is unable to remove it.

var mercedes = new Make() { Id = 77, Name = "chevrolet" };//manually created the object
mercedes.Models = GetAllModels(77);//I have it in-memory            
var model = mercedes.Models.Where(c => c.Id == 173).Single();
mercedes.Models.Remove(model);

I think I am doing something weird. But I am able to add/update the models using second approach, so why can't i remove it. Any insights please.

Answers


When you create a new domain object it isn't attached to the NHibernate session (unless you are creating a new object and call Save, for instance). As such, deleting from the models collection doesn't actually do anything.

Your second example doesn't seem like a particularly good practice but you can recreate an object and attach it to your NHibernate session using Session.Lock (Merge or Update are fairly normal depending on your preferred behavior).


Need Your Help

Extending ObjectGraph with overriding module

java android dependency-injection dagger

I'm playing with Dagger right now and apparently some of the features don't work for me.

I deleted my git project from Eclipse, how can I open it again and sync?

android eclipse git sync egit

I just used Eclipse and Egit in order to make my first open source project.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.