Hi Everyone,
userProfile = (UserProfile) NHibernateManager.GetSession().Get(typeof(UserProfile), 1);
foreach (UserTag ut in userProfile.Tags)
{
UserTagId Id = ut.Id;
}
Does anyone know why the Id in the highlighted line above is Null? However, ut.Data contains the valid data. I tried so many times to trace down the problem and failed.
Thanks alot.
UserProfile.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="Business" assembly="Business">
<class name="UserProfile" table="UserProfile">
<id name="UserProfileId" column="UserProfileId" type="System.Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="FirstName" column="FirstName" not-null="true" />
<property name="LastName" column="LastName" not-null="true" />
<property name="Gender" column="Gender" not-null="true" />
<property name="DateOfBirth" column="DOB" not-null="true" />
<property name="InternetMail" column="InternetMail" not-null="true" />
<property name="Password" column="Password" not-null="true" />
<property name="City" column="City" not-null="true" />
<property name="Country" column="Country" not-null="true" />
<property name="Occupation" column="Occupation" not-null="false" />
<property name="MailingCode" column="MailingCode" not-null="true" />
<bag name="Tags" lazy="false" table="UserTag">
<key column="UserProfileId"/>
<composite-element class="UserTag">
<parent name="UserProfile" />
<many-to-one name="Tag" class="Tag" column="TagId" not-null="true"/>
<property name="Data" column="Data" not-null="true" />
</composite-element>
</bag>
</class>
</hibernate-mapping>
UserProfile.cs
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace Business
{
public class UserProfile:BusinessBase
{
int m_UserProfileId;
string m_FirstName;
string m_LastName;
DateTime m_DateOfBirth;
string m_InternetMail;
string m_Password;
string m_City;
string m_Country;
string m_Occupation;
string m_MailingCode;
char m_Gender;
IList m_Tags = new ArrayList();
public IList Tags
{
get { return m_Tags; }
set { m_Tags = value; }
}
public UserProfile()
{
}
public char Gender
{
get { return m_Gender; }
set { m_Gender = value; }
}
public int UserProfileId
{
get { return m_UserProfileId; }
set { m_UserProfileId = value; }
}
public string Occupation
{
get { return m_Occupation; }
set { m_Occupation = value; }
}
public string MailingCode
{
get { return m_MailingCode; }
set { m_MailingCode = value; }
}
public string Country
{
get { return m_Country; }
set { m_Country = value; }
}
public string City
{
get { return m_City; }
set { m_City = value; }
}
public string Password
{
get { return m_Password; }
set { m_Password = value; }
}
public string LastName
{
get { return m_LastName; }
set { m_LastName = value; }
}
public DateTime DateOfBirth
{
get { return m_DateOfBirth; }
set { m_DateOfBirth = value; }
}
public string InternetMail
{
get { return m_InternetMail; }
set { m_InternetMail = value; }
}
public string FirstName
{
get { return m_FirstName; }
set { m_FirstName = value; }
}
}
}
UserTag.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="Business" assembly="Business" >
<class name="UserTag" table="UserTag" >
<composite-id name="Id" class="UserTagId">
<key-property name="TagId" column="TAGID"/>
<key-property name="UserProfileId" column="USERPROFILEID"/>
</composite-id>
<property name="Data" column="Data" not-null="true" />
<!-- Read-only association property -->
<many-to-one name="UserProfile"
column="UserProfileId"
not-null="true"
insert="false"
update="false" />
<!-- Read-only association property -->
<many-to-one name="Tag"
column="TagId"
not-null="true"
insert="false"
update="false" />
</class>
</hibernate-mapping>
UserTag.cs
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace Business
{
public class UserTag
{
UserProfile m_UserProfile;
string m_Data;
Tag m_Tag;
UserTagId m_Id;
public UserTagId Id
{
get { return m_Id; }
set { m_Id = value; }
}
public UserProfile UserProfile
{
get { return m_UserProfile; }
set { m_UserProfile = value; }
}
public Tag Tag
{
get { return m_Tag; }
set { m_Tag = value; }
}
public string Data
{
get { return m_Data; }
set { m_Data = value; }
}
}
[Serializable]
public class UserTagId
{
private int m_UserProfileId;
public int UserProfileId
{
get { return m_UserProfileId; }
set { m_UserProfileId = value; }
}
private int m_TagId;
public int TagId
{
get { return m_TagId; }
set { m_TagId = value; }
}
public override bool Equals(object obj)
{
if (obj is UserTagId)
{
UserTagId that = (UserTagId)obj;
return this.m_UserProfileId.Equals(that.m_UserProfileId) &&
this.m_TagId.Equals(that.m_TagId);
}
else
return false;
}
public override int GetHashCode()
{
return m_UserProfileId.GetHashCode() + m_TagId.GetHashCode();
}
}
}
Tag.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Business.Tag, Business" table="Tag">
<id name="TagId" column="TagId" type="System.Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="Name" column="Name" not-null="true" />
</class>
</hibernate-mapping>
Tag.cs
Code:
using System;
using System.Collections.Generic;
using System.Text;
namespace Business
{
public class Tag
{
string m_Name;
int m_TagId;
public Tag()
{
}
public int TagId
{
get { return m_TagId; }
set { m_TagId = value; }
}
public string Name
{
get { return m_Name; }
set { m_Name = value; }
}
}
}