Hi,
I am currently using NHibernate in a little test application and I have some difficulties with subclasses.
Here is a little description of my test application:
* List of classes:
- Patient
- IVisit (interface)
- VisitA (VisitA : IVisit)
- VisitB (VisitB : IVisit)
So, both VisitA and VisitB inherit from Visit.
In my Patient class, I have 2 ISet, one for VisitA and one for VisitB.
Content of "patients" table:
Code:
id;name
---------
1;John
Content of "visits" table:
Code:
id;patients_id;visit_type;data
---------------------------------
1;1;1;A1
2;1;1;A2
3;1;2;B1
visit_type = 1 -> VisitA
visit_type = 2 -> VisitB
If I write
Code:
IList va = _session.CreateCriteria(typeof(VisitA)).List();
, "va" only contains rows 1 and 2 from "visits" table (which is correct).
But now, if I write
Code:
Patient p = (Patient)_session.Get(typeof(Patient), 1);
System.Diagnostics.Debug.WriteLine("VisitsA: " + p.VisitsA.Count);
, I will have the exception written below.
It seems that NHibernate doesn't take only VisitA entities. It takes everything, so it throws an exception when parsing the 3rd row of the table.
It should create the following SQL query:
Code:
SELECT * FROM visits WHERE patients_id=1 AND visit_type=1
but instead of that, it seems it creates this one:
Code:
SELECT * FROM visits WHERE patients_id=1
Any suggestion?
Thanks,
Anthony
Hibernate version: 2.0.1.GA
Mapping documents:Visit.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="test8.Entities.IVisit, test8" table="visits" abstract="true">
<id name="Id" column="id" type="Int32">
<generator class="native" />
</id>
<discriminator column="visit_type"/>
<many-to-one name="Patient" class="test8.Entities.Patient, test8" column="patients_id"/>
<property name="Data" column="data" type="String"/>
<subclass name="test8.Entities.VisitA, test8" discriminator-value="1"/>
<subclass name="test8.Entities.VisitB, test8" discriminator-value="2"/>
</class>
</hibernate-mapping>
Patient.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="test8.Entities.Patient, test8" table="patients">
<id name="Id" column="id" type="Int32">
<generator class="native" />
</id>
<property name="Name" column="name" type="String"/>
<set name="VisitsA">
<key column="patients_id"/>
<one-to-many class="test8.Entities.VisitA, test8"/>
</set>
<set name="VisitsB">
<key column="patients_id"/>
<one-to-many class="test8.Entities.VisitB, test8"/>
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Patient p = (Patient)_session.Get(typeof(Patient), 1);
System.Diagnostics.Debug.WriteLine("VisitsA: " + p.VisitsA.Count);
System.Diagnostics.Debug.WriteLine("VisitsB: " + p.VisitsB.Count);
Full stack trace of any exception that occurs:Code:
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Exception non gérée</Description><AppDomain>test8.exe</AppDomain><Exception><ExceptionType>NHibernate.WrongClassException, NHibernate, Version=2.0.1.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4</ExceptionType><Message>Object with id: 3 was not of the specified subclass: test8.Entities.VisitA (loading object was of wrong class [test8.Entities.VisitB])</Message><StackTrace> à NHibernate.Loader.Loader.InstanceAlreadyLoaded(IDataReader rs, Int32 i, ILoadable persister, EntityKey key, Object obj, LockMode lockMode, ISessionImplementor session)
à NHibernate.Loader.Loader.GetRow(IDataReader rs, ILoadable[] persisters, EntityKey[] keys, Object optionalObject, EntityKey optionalObjectKey, LockMode[] lockModes, IList hydratedObjects, ISessionImplementor session)
à NHibernate.Loader.Loader.GetRowFromResultSet(IDataReader resultSet, ISessionImplementor session, QueryParameters queryParameters, LockMode[] lockModeArray, EntityKey optionalObjectKey, IList hydratedObjects, EntityKey[] keys, Boolean returnProxies)
à NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
à NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
à NHibernate.Loader.Loader.LoadCollection(ISessionImplementor session, Object id, IType type)
à NHibernate.Loader.Collection.CollectionLoader.Initialize(Object id, ISessionImplementor session)
à NHibernate.Persister.Collection.AbstractCollectionPersister.Initialize(Object key, ISessionImplementor session)
à NHibernate.Event.Default.DefaultInitializeCollectionEventListener.OnInitializeCollection(InitializeCollectionEvent event)
à NHibernate.Impl.SessionImpl.InitializeCollection(IPersistentCollection collection, Boolean writing)
à NHibernate.Collection.AbstractPersistentCollection.Initialize(Boolean writing)
à NHibernate.Collection.PersistentSet.get_Count()
à test8.MainForm..ctor() dans d:\sharp test\test8\MainForm.cs:ligne 74
à test8.Program.Main(String[] args) dans d:\sharp test\test8\Program.cs:ligne 28</StackTrace><ExceptionString>NHibernate.WrongClassException: Object with id: 3 was not of the specified subclass: test8.Entities.VisitA (loading object was of wrong class [test8.Entities.VisitB])
à NHibernate.Loader.Loader.InstanceAlreadyLoaded(IDataReader rs, Int32 i, ILoadable persister, EntityKey key, Object obj, LockMode lockMode, ISessionImplementor session)
à NHibernate.Loader.Loader.GetRow(IDataReader rs, ILoadable[] persisters, EntityKey[] keys, Object optionalObject, EntityKey optionalObjectKey, LockMode[] lockModes, IList hydratedObjects, ISessionImplementor session)
à NHibernate.Loader.Loader.GetRowFromResultSet(IDataReader resultSet, ISessionImplementor session, QueryParameters queryParameters, LockMode[] lockModeArray, EntityKey optionalObjectKey, IList hydratedObjects, EntityKey[] keys, Boolean returnProxies)
à NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
à NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
à NHibernate.Loader.Loader.LoadCollection(ISessionImplementor session, Object id, IType type)
à NHibernate.Loader.Collection.CollectionLoader.Initialize(Object id, ISessionImplementor session)
à NHibernate.Persister.Collection.AbstractCollectionPersister.Initialize(Object key, ISessionImplementor session)
à NHibernate.Event.Default.DefaultInitializeCollectionEventListener.OnInitializeCollection(InitializeCollectionEvent event)
à NHibernate.Impl.SessionImpl.InitializeCollection(IPersistentCollection collection, Boolean writing)
à NHibernate.Collection.AbstractPersistentCollection.Initialize(Boolean writing)
à NHibernate.Collection.PersistentSet.get_Count()
à test8.MainForm..ctor() dans d:\sharp test\test8\MainForm.cs:ligne 74
à test8.Program.Main(String[] args) dans d:\sharp test\test8\Program.cs:ligne 28</ExceptionString></Exception></TraceRecord>
Name and version of the database you are using: SQLite3 (using System.Data.SQLite.dll)
The generated SQL (show_sql=true):Code:
SELECT visitsa0_.patients_id as patients3_1_, visitsa0_.id as id1_, visitsa0_.id as id2_0_, visitsa0_.patients_id as patients3_2_0_, visitsa0_.data as data2_0_ FROM visits visitsa0_ WHERE visitsa0_.patients_id=@p0; @p0 = '1'