tags:

views:

21

answers:

1

Hi,

Using NHibernate 2.1 on mssql with C#.

I have 2 diffrent classes witch use an "group" object.

<class name="OrderCode" table="OrderCode" polymorphism="explicit">

<id name="Id" column="ID">
  <generator class="native" />
</id>
...
<many-to-one class="Group" name="Group" column="GRUPPE" cascade="all" />
...

and

<class name="Alotment" table="Alotment">

<id name="Id" column="ID">
  <generator class="native" />
</id>
...
<many-to-one class="Group" name="Group" column="GRUPPE" cascade="all" />
...

An import script now creates first the OrderCode and select the group, if the group not exist it makes an new one. Later in the same script I create the alotment. Same way, select if there is the right group, if not the script create a group.

Sometimes OrderCode and Alotment need the same group, but if the group dosen't exist, the script creates two groups cause the secend select don't get the group out off the session.

I know its the same session. Is there a way to let IQuery, ICriteria or Session.Linq select not commit groups out of the session?

A: 

As far as I know if you save or update an entity using a session you should flush or commit your changes before getting it back from the same session. Can't you use group object reference like this?

using(var session=SessionFactory.OpenSession())
{
var group=(Group)session.CreateQuery(group_query_string).UniqueResult;
if(group==null)
{
group=new Group();
session.SaveOrUpdate(group);
}
...
var orderCode=new OrderCode{Group=group};
session.SaveOrUpdate(orderCode);
...
// and later in your code

var alotment=new Alotment{Group=group};
session.SaveOrUpdate(alotment);
....

session.Flush();
}
Beatles1692
I will reference the group object, thought it would be nicer if i select from the session.
Zhok