Okay, here is the code in question. I've tried to cobble together what
is scattered across different classes so hopefully its still
understandable what's going on here.

NHibernate.dll Version is 2.1.2.4000 by the way and the whole thing
runs on .Net 3.5 SP1

The mapped Object
public class Folder
{
        public virtual string Name { get; set; }
        public virtual int Id {get; protected set; }
        public override int GetHashCode()
        {
                return (GetType().FullName
                        + "|" + Name
                        ).GetHashCode();
        }
}

The unit test that is essentially wrapped in a transaction being
started and rolled back at the end
[SetUp]
public override void SetUp()
{
        base.SetUp();

        _folderDAO = _daoFactory.GetFolderDAO();

        _folder1 = new de.dmi.TrackingFolder.Core.Domain.Folder() { Name = "c:
\\folder\\1" };
        _folder2 = new de.dmi.TrackingFolder.Core.Domain.Folder() { Name = "c:
\\folder\\2" };

        _folderDAO.BeginTransaction();
        _folderDAO.Save(_folder1);
        _folderDAO.Save(_folder2);
}
[Test]
public void GetUniqueByName_Folder1_Folder1Returned()
{
        Core.Domain.Folder result =
_folderDAO.GetUniqueByName(_folder1.Name);
        Core.Domain.Folder result2 = _folderDAO.GetById(_folder1.Id, false);
        Assert.That(result.Id, Is.EqualTo(_folder1.Id));
        Assert.That(result.Name, Is.EqualTo(_folder1.Name));
}
[TearDown]
public virtual void TearDown()
{
        _folderDAO.RollbackTransaction();
}

This is _folderDAO.GetUniqueBy(...)
public TrackingFolder.Core.Domain.Folder GetUniqueByName(string name)
{
        if (String.IsNullOrEmpty(name)) throw new
ArgumentNullException("name");
        NHibernate.ICriteria criteria =
NHibernateSession.CreateCriteria<Core.Domain.Folder>();
        criteria.Add(NHibernate.Criterion.Expression.Eq("Name", name));

        return criteria.UniqueResult<Core.Domain.Folder>();
}

Now stepping through the code this is what happens:

_folderDAO.BeginTransaction results in
        SessionFactory.OpenSession()
        Session.FlushMode = FlushMode.Commit;
        Session.BeginTransaction()

_folderDAO.Save(_folder...) invokes Session.Save(_folder...) The
Entitycount of the ISession is two after the two .Save(...) statements
The entities DO get an Id Assigned at that point. At the first call
to .Save there is one select max(id) from folder visible in the SQL
Profiler. The INSERT SQL statement is not executed yet.

_folderDAO.GetUniqueBy results in
exec sp_executesql N'SELECT this_.id as id1_0_, this_.name as name1_0_
FROM folder this_ WHERE this_.name = @p0', N'@p0 nvarchar(11)', @p0 =
N'c:\folder\1' and returns null

_folderDAO.RollbackTransaction(); invokes
        Transaction.RollBack()
        Session.Flush()
        Session.Close()


After the call to Session.Flush the Profiler shows the two
exec sp_executesql N'INSERT INTO folder (name, id) VALUES (@p0, @p1)',
N'@p0 nvarchar(11),@p1 int', @p0 = N'c:\folder\1', @p1 = 1
exec sp_executesql N'INSERT INTO folder (name, id) VALUES (@p0, @p1)',
N'@p0 nvarchar(11),@p1 int', @p0 = N'c:\folder\2', @p1 = 2

That's it.

The whole SQL profiler log for this test run reads as follows:
Audit Login     -- network protocol: TCP/IP
set quoted_identifier on
set implicit_transactions off
set cursor_close_on_commit off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set language Deutsch
set dateformat dmy
set datefirst 1
        .Net SqlClient Data Provider    51      2010-03-09 11:03:01.693
RPC:Completed   exec sp_reset_connection        .Net SqlClient Data Provider    
51
2010-03-09 11:03:02.320
SQL:BatchStarting       SET TRANSACTION ISOLATION LEVEL READ COMMITTED;BEGIN
TRANSACTION     .Net SqlClient Data Provider    51      2010-03-09 11:03:02.320
SQL:BatchCompleted      SET TRANSACTION ISOLATION LEVEL READ
COMMITTED;BEGIN TRANSACTION     .Net SqlClient Data Provider    51      
2010-03-09
11:03:02.320
Audit Login     -- network protocol: TCP/IP
set quoted_identifier on
set implicit_transactions off
set cursor_close_on_commit off
set ansi_warnings on
set ansi_padding on
set ansi_nulls on
set concat_null_yields_null on
set language Deutsch
set dateformat dmy
set datefirst 1
        .Net SqlClient Data Provider    55      2010-03-09 11:03:02.333
SQL:BatchStarting       select max(id) from folder .Net SqlClient Data
Provider        55      2010-03-09 11:03:02.350
SQL:BatchCompleted      select max(id) from folder .Net SqlClient Data
Provider        55      2010-03-09 11:03:02.350
RPC:Completed   exec sp_executesql N'SELECT this_.id as id1_0_,
this_.name as name1_0_ FROM verzeichnis this_ WHERE this_.name = @p0',
N'@p0 nvarchar(11)', @p0 = N'c:\folder\1'       .Net SqlClient Data Provider
51      2010-03-09 11:03:02.473
SQL:BatchStarting       IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION .Net
SqlClient Data Provider 51      2010-03-09 11:03:06.177
SQL:BatchCompleted      IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION .Net
SqlClient Data Provider 51      2010-03-09 11:03:06.177
RPC:Completed   exec sp_reset_connection        .Net SqlClient Data Provider    
51
2010-03-09 11:03:06.210
RPC:Completed   exec sp_executesql N'INSERT INTO folder (name, id)
VALUES (@p0, @p1)', N'@p0 nvarchar(11),@p1 int', @p0 = N'c:\folder\1',
@p1 = 1 .Net SqlClient Data Provider    51      2010-03-09 11:03:06.210
RPC:Completed   exec sp_executesql N'INSERT INTO folder (name, id)
VALUES (@p0, @p1)', N'@p0 nvarchar(11),@p1 int', @p0 = N'c:\folder\2',
@p1 = 2 .Net SqlClient Data Provider    51      2010-03-09 11:03:06.210

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to