我有课
public class HobbyHome:Establishment
{
public virtual ICollection<HobbyHomeAddress> HobbyHomeAddressList { set; get; }
public virtual ICollection<HobbyDeveloper> HobbyDeveloperList { set; get; }
public virtual ICollection<HobbyDetail> HobbyDetailList { set; get; }
}
里面的爱好详情:
[Key]
public virtual int HobbyDetailID { get; set; }
// public virtual int HobbyId { get; set; }
public virtual HobbyMasters Hobbymaster { get; set; }
public virtual Course course { get; set; }
public virtual StudyMedium StudyMedium { get; set; }
public virtual decimal Fees { get; set; }
}
我可以将我的数据保存在两个表中...但是在通过 HobbyHomeId 获取 hobbydetail 时...我收到错误,因为找不到字段
Hobbyhome 的映射文件:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly ="HobbyHomes" namespace="HobbyHomes.Model" >
<class name ="HobbyHome" table="HobbyHome">
<id name="EstablishmentId" column="HobbyHomeId">
<generator class="native"/>
</id>
<property name="Name"/>
<set name="HobbyHomeAddressList">
<key column="HobbyHomeId" foreign-key="fk_HobbyHomeAddress_HobbyHomeId"/>
<one-to-many class ="HobbyHomes.Model.HobbyHomeAddress"/>
</set>
<set name="HobbyDeveloperList">
<key column="HobbyHomeId" foreign-key="fk_HobbyDeveloper_HobbyHomeId"/>
<one-to-many class ="HobbyHomes.Model.HobbyDeveloper"/>
</set>
<set name="HobbyDetailList">
<key column="HobbyHomeId" foreign-key="fk_HobbyDetail_HobbyHomeId"/>
<one-to-many class ="HobbyHomes.Model.HobbyDetail"/>
</set>
</class>
</hibernate-mapping>
这是我获取数据的函数:
public HobbyDetail FetchbyHobbyId(int id)
{
log.Debug("Start");
ISession session = DataAccessLayerHelper.OpenWriterSession();
ITransaction transaction = session.BeginTransaction();
HobbyDetail hobbydetail = null;
try
{
ICriteria criteria = session.CreateCriteria(typeof(HobbyDetail))
.Add(Restrictions.Eq("HobbyHomeId", id));
hobbydetail = criteria.UniqueResult<HobbyDetail>();
transaction.Commit();
}
catch (SessionException ex)
{
if (transaction != null && transaction.IsActive)
transaction.Rollback();
log.Error(ex);
hobbydetail = null;
}
finally
{
if (transaction != null)
transaction.Dispose();
if (session != null && session.IsConnected)
session.Close();
log.Debug("End");
}
return hobbydetail;
}
sumbody 可以帮我解决这个问题....