I have a problem with this piece of code
If I don’t use the transaction (I commented here in that code), the update won’t occur.
using (var session = Repository.TSession())
{
var utilisateurDal = new UtilisateurDal(session);
var utilisateur = utilisateurDal.GetUtilisateur(login);
if (utilisateur != null)
{
//var transaction = Session.BeginTransaction();
utilisateur.MotDePasse = "test";
session.Update(utilisateur);
//transaction.Commit();
}
}
The code which retrieve the object to update :
(Please note that login isn’t an unique identifier)
public Utilisateur GetUtilisateur(ISession session, string login)
{
return session.Query<Utilisateur>().SingleOrDefault(u => u.Identifiant == login);
}
When I enable the debug mode I have that message :
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener: 2011-11-16 14:14:57,832 [35] DEBUG NHibernate.Event.Default.DefaultSaveOrUpdateEventListener [(null)] - ignoring persistent instance
NHibernate.Event.Default.DefaultSaveOrUpdateEventListener: 2011-11-16 14:14:57,833 [35] DEBUG NHibernate.Event.Default.DefaultSaveOrUpdateEventListener [(null)] - object already associated with session: [BusinessObjets.Utilisateur#3]
Do you the meaning of this behaviour ?
Regards
NHibernate doesnt send the Update-statement immediatly and will cache it. On the next
session.Flush()all Updates are sent together to save roundtrips.transaction.Commit()will Flush all changes to the db, otherwise a commit in the db wont know the updates.you can even omit
session.Update(utilisateur);because the session tracks the entities loaded through it and will send all changes to db on the nextFlush().Either use a transaction or call session.Flush(); at the end of your updates.
this could look like this