现在设置一对多的关系,加载父类没问题,如果没延迟加载的时候,就会抛:
could not initialize a collection 的异常..代码如下:
子表的syskeyword是外键,主表的customid的主键
父实体:
Code
public class Custom
{
public virtual String customid { get; set; }
public virtual String customcode { get; set; }
public virtual String customname { get; set; }
public virtual String customarea { get; set; }
public virtual String customtype { get; set; }
public virtual String linkman1 { get; set; }
public virtual String linkphone1 { get; set; }
public virtual String linkmobile1 { get; set; }
public virtual String linkman2 { get; set; }
public virtual String linkphone2 { get; set; }
public virtual String linkmobile2 { get; set; }
public virtual String linkman3 { get; set; }
public virtual String linkphone3 { get; set; }
public virtual String linkmobile3 { get; set; }
public virtual DateTime customwebsite { get; set; }
public virtual String inputtime { get; set; }
public virtual String inputer { get; set; }
public virtual DateTime updatetime { get; set; }
public virtual String updater { get; set; }
public virtual String remark { get; set; }
public virtual int removed { get; set; }
private IList<SysLog> syslogs;
public virtual IList<SysLog> SysLogs
{
get
{
if (syslogs == null)
syslogs = new List<SysLog>();
return syslogs;
}
set { syslogs = value; }
}
}
配置文件:
Code
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Domain.Entities.SysLog, Domain" table ="syslog">
<id name="sysid" column ="sysid">
<generator class ="native"/>
</id>
<!--<property name ="syskeyword" column="syskeyword"/>-->
<property name ="syscontent" column="syscontent"/>
<property name="systype" column="systype" />
<property name="inputtime" column="inputtime" />
<property name="inputer" column="inputer" />
<property name="removed" column="removed" />
<!--name当前实体类的父类属性,column是子类与父类关联的字段-->
<many-to-one name="customer" column="syskeyword" not-null="true"
class="Domain.Entities.Custom,Domain"/>
</class>
</hibernate-mapping>
子实体:
Code
public class SysLog
{
public virtual String sysid { get; set; }
public virtual String syskeyword { get; set; }
public virtual String syscontent { get; set; }
public virtual String systype { get; set; }
public virtual String inputtime { get; set; }
public virtual String inputer { get; set; }
public virtual int removed { get; set; }
public virtual Custom customer { get; set; }
}
配置文件:
Code
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Domain.Entities.SysLog, Domain" table ="syslog">
<id name="sysid" column ="sysid">
<generator class ="native"/>
</id>
<!--<property name ="syskeyword" column="syskeyword"/>-->
<property name ="syscontent" column="syscontent"/>
<property name="systype" column="systype" />
<property name="inputtime" column="inputtime" />
<property name="inputer" column="inputer" />
<property name="removed" column="removed" />
<!--name当前实体类的父类属性,column是子类与父类关联的字段-->
<many-to-one name="customer" column="syskeyword" not-null="true"
class="Domain.Entities.Custom,Domain"/>
</class>
</hibernate-mapping>
调用:
Code
public Custom GetCustomeById(string customerId)
{
return Session.CreateQuery("from Custom where customid=:customerId")
.SetString("customerId", customerId).List<Custom>().First();//这里抛could not initialize a collection 异常
}
帮忙看看啊。。不知道哪出了问题