Hi ,
I've been looking for quite a while cannot find the solution to for a strange behaviour of my application. I can reproduce it in a small example.
I have two OrganizationalUnit und Type.
OrganizationalUnit contains Type als Foreign Key, which mean each OrganizationalUnit is of a certain type Typ. After retrieving entities I do not close the session but instead leave the session to reuse it
- Mapping File of OrganizationalUnit looks like that :
Code:
<class name="xxx.OrganizationalUnit,xxx" table="OrganizationalUnit" lazy="false">
<id name="OrganizationalUnitId" column="OrganizationalUnitId" type="int">
<generator class="increment" />
</id>
<many-to-one name="TypeId" cascade="none" column="TypeId" not-null="true" />
</class>
- Mapping File of Type looks like that
Code:
<class name="xxx.Type,xxx" table="Type" lazy="false">
<id name="TypeId" column="TypeId" type="string">
<generator class="assigned" />
</id>
<bag name="FkOrganizationalUnitType" inverse="true" lazy="true" cascade="all">
<key column="TypeId" />
<one-to-many class="xxx.OrganizationalUnit,xxx" />
</bag>
</class>
Now, what am I doing?:
Code:
IList types = BusinessObjectFactory<DC.Type>.GetItems; // retrieve all “Type”-Objects
// each Object contains the Attribute FkOrganizationalUnitType being a list of OrganizationalUnits which are of the specific type
DC.OrganizationalUnit orgUnit = new DC.OrganizationalUnit((DC.Type)types[0]); // now I create a new OrganizationalUnit of Type types[0]
orgUnit.Save(orgUnit); //I save the OrganizationalUnit in the database – works fine; entry is persistent
IList typesNew = BusinessObjectFactory<DC.Type>.GetItems; // now I try to retrieve the Objects of the entity “type“ again
Now I would like that the Attribut „FkOrganizationalUnitType“ of typesNew[0] has one more entry then types[0] – the one which we have created and saved. Unfortunately the FkOrganizationalUnitType are identical and only after starting the application again the list in FkOrganizationalUnitType will be complete
Here ist he getItems Routine:
Code:
public static IList GetItems
{
get
{
IList items = null;
ISession session = SessionProvider.Instance.GetSession();
ITransaction trans = null;
try
{
trans = session.BeginTransaction();
items = session.CreateCriteria(typeof(T)).List();
trans.Commit();
foreach (object currentEntry in items) ((T)currentEntry).PersistenceInfo = PersistenceStatus.persistent;
return items;
}
catch (Exception ex)
{
trans.Rollback();
throw new DataAccessException("Error getting items", ex);
}
finally
{
SessionProvider.Instance.CloseSession();
}
}
}
As I said the session retrieved here, is reused because the first call did not close the session. Otherwise I could not work with lazy-loading. How can I do both lazy loading and retrieve entries, . Do you have an Idea what I could do to make it work right???
Thanks for your help in advance
antoschka