I am trying to communicate with an entity from a third party dll. For some reason I have to use StatelessSession . It works with StatefullSession , but not with Stateless. The idea is to let a third party add a table and get its data when I query the main table.
I have three projects: my main project, the model project with the IExtender interface and the third-party project with the Extender class (which the main project does not refer to).
My data schema:
Table Data Id INT identity, more fields... Table Extender Id INT PK, FK from Data Name NVARCHAR more fields...
Project Project Code:
public interface IExtender { int Id { get; set; } }
The main project code:
public class Data { public virtual int Id { get; set; } public IExtender Extender { get; set; }
The main hbm project:
<?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"/> </class> </hibernate-mapping>
side project:
public class Extender : IExtender { public virtual int Id { get; set; } public string Name { get; set; } }
Third 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>
A third-party project places its DLL in the main bin folder of the project.
Now my code to get the data is as follows:
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);
If I use StatefullSession , it works fine with a single entity or bag. If I use StatelessSession and instead have a bag (and list as commented lines above), I get an error:
collections cannot be retrieved by session without saving
If I remove lazy="false" from the bag, I get this error (whether the collection is virtual or not):
Initialization [MyProject.Data # 1] - Initially unsuccessful role collection: MyProject.Data.Extenders, session or session was closed
EDIT: I updated my version of NHibernate to the latest version, and now it works when I use a bag, but when I use a single entity with a one-to-one relationship, it is always zero.
By the way, regardless of whether there is an error or not, I see in the last sql query that the Extender table is connecting properly.
Thank you for your help.