I'm trying to make an association with an entity from 3rd party dll. For some reasons, I have to use StatelessSession
. With StatefullSession
it works but not with Stateless. The idea is to allow 3rd party to add a table and have its data fetched when I query the main table.
I have three projects: my main project, model project with the IExtender
interface, and 3rd party project with Extender
class (not referenced by the main project).
My data schema:
Table Data
Id INT identity,
more fields...
Table Extender
Id INT PK, FK from Data
Name NVARCHAR
more fields...
Model project code:
public interface IExtender
{
int Id { get; set; }
}
Main project code:
public class Data
{
public virtual int Id { get; set; }
public IExtender Extender { get; set; }
//public IList<IExtender> Extenders { get; set; }
}
Main project hbm:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="..." assembly="..." auto-import="true">
<class name="Data" table="Data" lazy="false">
<id name="Id" column="Id" type="int" >
<generator class="identity" />
</id>
<one-to-one name="Extender" foreign-key="Id"
class="Model.IExtender, Model" lazy="false"/>
<!--<bag name="Extenders" cascade="all" inverse="true" lazy="false">
<key column="Id" />
<one-to-many class="Model.IExtender, Model"/>
</bag>-->
</class>
</hibernate-mapping>
3rd party project:
public class Extender : IExtender
{
public virtual int Id { get; set; }
public string Name { get; set; }
}
3rd party hbm:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Model" assembly="Model" auto-import="true">
<class name="IExtender" table="IExtender" lazy="false" abstract="true">
<id name="Id" column="Id" type="int" >
<generator class="assigned" />
</id>
<union-subclass table="Extender" name="Extension.Extender, Extension" lazy="false">
<property name="Name" column="Name" not-null="true" type="String" />
</union-subclass>
</class>
</hibernate-mapping>
3rd party project puts its dll in the main project bin folder.
Now my code to get the data looks like:
var sessionFactory = new Configuration().Configure()
.AddAssembly(Assembly.LoadFile(System.Environment.CurrentDirectory + @"\Extender.dll"))
.AddAssembly(Assembly.GetExecutingAssembly())
.BuildSessionFactory();
var session = sessionFactory.OpenStatelessSession();
var criteria = session.CreateCriteria<Data>();
var data = criteria.List<Data>().ToList();
Console.WriteLine(data.First().Extender.Id); // fails because Extender is null
If I use StatefullSession
, it is working fine with a single entity or a bag. If I use StatelessSession
and a bag instead (and a list as the commented rows above), I get an error:
collections cannot be fetched by a stateless session
If I remove the lazy="false"
from the bag, I get this error (no matter if the collection is virtual or not):
Initializing[MyProject.Data#1]-failed to lazily initialize a collection of role: MyProject.Data.Extenders, no session or session was closed
EDIT: I upgraded my NHibernate version to the latest and now it is working when I use a bag, but when I use a single entity with one-to-one relation it is always null.
By the way, no matter if there is an error or not, I see in the final sql query that Extender
table is being joined properly.
Thanks for your help.