Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 1.0.2
Mapping documents: <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Competition.Business.Entity, Competition.Business" table="t_entities" lazy="true">
<cache usage="nonstrict-read-write"/>
<id name="ID" column="EntityID" type="int" unsaved-value="0">
<generator class="native" />
</id>
<property name="Name" column="Name" type="String" length="200" not-null="false" />
<property name="IsPrivate" not-null="false" />
<component name="Roles" class="Competition.Business.RoleSet, Competition.Business">
<parent name="Parent"/>
<set name="RolesActor" where="EndDate is null" inverse="true" lazy="true">
<key column="ActorId"/>
<one-to-many class="Competition.Business.Role, Competition.Business"/>
</set>
<set name="RolesTarget" where="EndDate is null" inverse="true" lazy="true" >
<key column="TargetId"/>
<one-to-many class="Competition.Business.Role, Competition.Business"/>
</set>
</component>
<component name="RolesHistory" class="Competition.Business.RoleSet, Competition.Business">
<parent name="Parent"/>
<set name="RolesActor" where="EndDate is not null" lazy="true" inverse="true" >
<key column="ActorId"/>
<one-to-many class="Competition.Business.Role, Competition.Business" />
</set>
<set name="RolesTarget" where="EndDate is not null" lazy="true" inverse="true" >
<key column="TargetId"/>
<one-to-many class="Competition.Business.Role, Competition.Business" />
</set>
</component>
<!-- Users -->
<joined-subclass name="Competition.Business.User, Competition.Business" table="t_users" lazy="true">
<key column="UserID"/>
<property name="LoginName" column="LoginName" type="String" length="20" not-null="false" />
<property name="Password" type="String" length="20" not-null="false"/>
<property name="EmailAddress" column="EmailAddress" type="String" length="40" not-null="false"/>
<property name="LastLogon" type="DateTime" not-null="true"/>
<property name="CreationDate" type="DateTime" not-null="true"/>
<property name="IsVerified" type="Boolean" not-null="true"/>
<property name="ConfirmationCode" type="String" length="50" not-null="false"/>
<property name="ForumUserID" not-null="true"/>
<property name="BirthDate" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" not-null="false"/>
<property name="Sex" not-null="false"/>
<property name="FirstName" type="String" length="100" not-null="true"/>
<property name="MiddleName" type="String" length="100" not-null="true"/>
<property name="LastName" type="String" length="100" not-null="true"/>
<property name="SecurityQuestion" type="String" length="200" not-null="false"/>
<property name="SecurityAnswer" type="String" length="200" not-null="false"/>
<property name="Comment" type="String" length="1000" not-null="true"/>
<property name="LastPasswordChangeDate" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" not-null="false"/>
<property name="LastLockoutDate" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate" not-null="false"/>
<property name="IsApproved" type="Boolean" not-null="true"/>
<property name="IsLockedOut" type="Boolean" not-null="true"/>
<bag name="CompetitionInscriptions" inverse="true" lazy="true" >
<key column="UserID"/>
<one-to-many class="Competition.Business.CompetitionInscription, Competition.Business"/>
</bag>
</joined-subclass>
<!-- Teams -->
<joined-subclass name="Competition.Business.Team, Competition.Business" table="t_teams" lazy="true">
<key column="TeamID"/>
<property name="AcceptRequests" />
</joined-subclass>
<!-- Competitions-->
<joined-subclass name="Competition.Business.Competition, Competition.Business" table="t_competitions" lazy="true">
<key column="CompetitionID"/>
<property name="Description" type="String" length="1000" not-null="true" />
<property name="ForumID" not-null="true"/>
<property name="DateInscriptionStarts" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate"/>
<property name="DateInscriptionEnds" type="Nullables.NHibernate.NullableDateTimeType, Nullables.NHibernate"/>
<set name="Inscriptions" inverse="true" lazy="true">
<key column="CompetitionID"/>
<one-to-many class="Competition.Business.CompetitionInscription, Competition.Business"/>
</set>
<bag name="Events" inverse="true" lazy="true">
<key column="CompetitionID"/>
<one-to-many class="Competition.Business.CompetitionEvent, Competition.Business"/>
</bag>
</joined-subclass>
Code between sessionFactory.openSession() and session.close():
Name and version of the database you are using: SQL Express 05
Good day folks,
I've got the following problem with NHibernate and I have no clue on how to fix it within the NH framework. My mappings are working really well except for the issue I'm highlighting here.
I have an object called Entity that is inherited by multiple classes like User, Teams, Events... I'm using a table-per-subclass mapping. In my code I'm loading objects from their IDs like this: m_session.Load(type, id).
To show the problem I'm going to give an example. I have and object of type User with ID 1 and an object of type Team with ID 2. Using m_session.Load(typeof(User),1) works as normal. However: m_session.Load(typeof(Team),1) causes problem because it is not returning null (or more accurately throwing an exception for missing object). It returns an object, of the correct type Team, but of course, the properties are not initialized and don't contain any meaningful data.
I was expecting an exception while using m_session.Load(typeof(Team),1) when 1 is the ID of a User and it is causing me major headaches. So my question is: how can I ensure that when I load an object of a specific tpye it will not load a different object from the same base object? (the same problem happens between any of the types derived from Entity).
I hope I explained it clearly...
Thanks,
Alain-Daniel