Hi Luka, one of the first rule of thumb with nhibernate is that you *have* to
keep consistency in memory.
If your objects are not consistent in memory the will never be consistent in
the db or anywhere.

The second thing to note is that you already have a Ticket loaded in memory,
so when you call *Session.Get<Ticket>(ticketId);*
you are not going to the DB again... this operation will give you the same
instance of the object that is in the variable t..
Because the session keep tracks of the entities.

Where to go from here? If you have the collection properly mapped (and have
a cascade with save option), you can do something like this:

using (var transaction = Session.BeginTransaction())
           {
               var t = new Ticket();
               t.Title = "TestTicket";
               var ticketId = (Guid)Session.Save(t);

               var pe = new ProcessExec();
*               t.AddProcess(pe);*

               var ticket = Session.Get<Ticket>(ticketId);
               transaction.Commit();

               Assert.NotNull(ticket);
               Assert.True(ticket.ProcessExecCollection.Count > 0);
           }

Or if you want to keep your former code, you can ensure in-memory
consistence as follows:

using (var transaction = Session.BeginTransaction())
           {
               var t = new Ticket();
               t.Title = "TestTicket";
               var ticketId = (Guid)Session.Save(t);

               var pe = new ProcessExec();
               pe.Ticket = t;
               Session.Save(pe);

               var ticket = Session.Get<Ticket>(ticketId);
               transaction.Commit();

               Assert.NotNull(ticket);
               Assert.True(ticket.ProcessExecCollection.Count > 0);
           }

you might change your Ticket property to something like this:

public class ProcessExec
{
    public Ticket Ticket { get { return ticket} set { ticket = value;
value.AddProcess(this); } }
}

public class Ticket
{
    public ISet<ProcessExec> Process { get; set;}

    public void AddProcess(ProcessExec pe)
    {
        if(pe.Ticket == null || !pe.Ticket.Equals(this)) pe.Ticket = this;
        Process.Add(pe);
    }
}


Memory consistence-always remember that , and you will be safe.


2011/5/20 Luka <[email protected]>

> Hi, I am having this problem..
>
> using (var transaction = Session.BeginTransaction())
>            {
>                var t = new Ticket();
>                t.Title = "TestTicket";
>                var ticketId = (Guid)Session.Save(t);
>
>                var pe = new ProcessExec();
>                pe.Ticket = t;
>                Session.Save(pe);
>
>                var ticket = Session.Get<Ticket>(ticketId);
>                transaction.Commit();
>
>                Assert.NotNull(ticket);
>                Assert.True(ticket.ProcessExecCollection.Count > 0);
>            }
>
>
> Now the problem is this that the assert fails on
> Assert.True(ticket.ProcessExec.Count>0).
> But If I do Session.Refresh(ticket); just after the
> transaction.Commit(), everithing works fine.
> How to tell NHibernate that when I create new ProcessExec and set its
> ticket, to automatically update the ticket?
>
> I need this because I do lots of stuff creating and selecting in a
> transaction.
>
> Please help.
>
> --
> 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.
>
>

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