To associate a disconnected entity to a new session you need to call
session.Lock() passing in the object that you want to associate with the
session. Since you a loading it one one session and deleting it one another
this is required.

If you have updated the entity, simply calling session.Update(entity) on the
new session will associate them.

- Robert

On Tue, Mar 23, 2010 at 8:35 AM, Jan Apel <[email protected]> wrote:

> Hello everybody,
>
> I'm trying to get started using NHibernate by doing the Summer of
> NHibernate tutorials.
>
> I have a test:
>
>        [Test]
>        public void CanDeleteCustomer()
>        {
>            Customer customer = GetDeletableCustomer();
>            _provider.DeleteCustomer(customer);
>
>            Customer testCustomer = GetDeletableCustomer();
>
>            Assert.IsNull(testCustomer);
>        }
>
> This test invokes GetDeletableCustomer()
>
>        private Customer GetDeletableCustomer()
>        {
>            return _provider.GetCustomerById(80);
>        }
>
> GetCustomerById() opens a session:
>
>        public Customer GetCustomerById(int customerId)
>        {
>            using (ISession session = GetSession())
>            {
>                return session.Get<Customer>(customerId);
>            }
>        }
>
> Later on my test invokes the method DeleteCustomer() which also opens
> a session:
>
> and DeleteCustomer()
>
>        public void DeleteCustomer(Customer customer)
>        {
>            using (ISession session = GetSession())
>            {
>                using (ITransaction tx = session.BeginTransaction())
>                {
>                    try
>                    {
>                        session.Delete(customer);
>                        session.Flush();
>                        tx.Commit();
>                    }
>                    catch (NHibernate.HibernateException)
>                    {
>                        tx.Rollback();
>                        throw;
>                    }
>                }
>            }
>        }
>
>
> Now my question is, are these both sessions the problem?
> If so, what would be an elegant way of solving this?
>
> Many thanks in advance,
>
> Jan Apel
>
> --
> 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]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>

-- 
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