NHibernate:无状态会话错误消息无法获取代理

时间:2011-11-15 21:47:32

标签: c# nhibernate

我正在使用nHibernate无状态会话来获取对象,更新一个属性并将对象保存回数据库。

我不断收到错误消息:

  无状态会话无法获取

代理

我有类似的代码在其他地方工作,所以我无法弄清楚为什么这不起作用。有谁知道问题可能是什么?

我正在尝试更新ScreenLockVersion属性。

映射:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                  assembly="ApplicationUtilities"
                  namespace="ApplicationUtilities.Concurrency">
  <class name="Screen" table="concurrency_screen" dynamic-update="true" optimistic-lock="all">
    <id name="ID" column="screenID">
      <generator class="identity" />
    </id>
    <property name="ScreenName" column="screenName" />
    <property name="SubScreenName" column="subScreenName" />
    <property name="ScreenLockID" column="screenLockID" />
    <property name="ScreenLockVersion" column="screenLockVersion" />
    <property name="ScreenLockRequired" column="screenLockRequired" />
    <many-to-one name="Parent" class="Screen" column="parentScreenID" />
    <many-to-one name="Identity" class="ApplicationUtilities.SystemIdentity.Identity" column="identityID" />
    <many-to-one name="Application" class="ApplicationName" column="applicationID" />
    <one-to-one name="LastModified" class="LastModified" property-ref="Screen" cascade="all" />
    <bag name="AffectedScreens" table="concurrency_affectedScreen" cascade="all">
      <key column="updatedScreenID" />
      <many-to-many column="affectedScreenID" class="Screen" />
    </bag>
    <bag name="ChildScreens" table="concurrency_screen">
      <key column="parentScreenID" />
      <many-to-many column="screenID" class="Screen" />
    </bag>
  </class>
</hibernate-mapping>

代码:

public class Screen : BaseDomain
{
    private string screenName;
    private string subScreenName;
    private Screen parent;
    private IList<Screen> childScreens = new List<Screen>();
    private IList<Screen> affectedScreens = new List<Screen>();
    private Guid screenLockID;
    private int screenLockVersion;
    private bool screenLockRequired;
    private LastModified lastModified;
    private Identity identity;
    private ApplicationName application;

    protected Screen()
    {
        this.parent = null;
        this.screenLockRequired = false;
    }

    /// <summary>
    /// Create new parent screen
    /// </summary>
    /// <param name="screenName">parent screenname</param>
    public Screen(string screenName, ApplicationName application)
    {
        this.screenName = screenName;
        this.subScreenName = null;
        this.parent = null;
        this.application = application; 
    }

    /// <summary>
    /// Create new subscreen
    /// </summary>
    /// <param name="screenName">parent screen name</param>
    /// <param name="subScreenName">subscreen name</param>
    /// <param name="parent">reference to parent screen</param>
    public Screen(string screenName, string subScreenName, Screen parent, ApplicationName application)
    {
        this.screenName = screenName;
        this.subScreenName = subScreenName;
        this.application = application; 
        this.parent = parent;
    }

    #region Properties
    public virtual IList<Screen> AffectedScreens
    {
        get { return affectedScreens; }
        set { affectedScreens = value; }
    }

    public virtual Screen Parent
    {
        get { return parent; }
        set { parent = value; }
    }

    public virtual IList<Screen> ChildScreens
    {
        get { return childScreens; }
        set { childScreens = value; }
    }

    public virtual string ScreenName
    {
        get { return screenName; }
        set { screenName = value; }
    }

    public virtual string SubScreenName
    {
        get { return subScreenName; }
        set { subScreenName = value; }
    }

    public virtual Guid ScreenLockID
    {
        get { return screenLockID; }
        set { screenLockID = value; }
    }

    public virtual int ScreenLockVersion
    {
        get { return screenLockVersion; }
        set { screenLockVersion = value; }
    }

    public virtual bool ScreenLockRequired
    {
        get
        {
            if (parent != null)
                return parent.ScreenLockRequired;
            else
                return screenLockRequired; 
        }
        set { screenLockRequired = value; }
    }

    public virtual LastModified LastModified
    {
        get { return lastModified; }
        set { lastModified = value; }
    }

    protected internal virtual Identity Identity
    {
        get { return identity; }
        set { identity = value; }
    }

    public virtual ApplicationName Application
    {
        get { return application; }
        set { application = value; }
    }
    #endregion

    #region Methods
    public override string ToString()
    {
        return ScreenName + SubScreenName;
    }

    public override bool Equals(object obj)
    {
        Screen other = obj as Screen;

        if (other == null)
            return false;

        if (string.IsNullOrEmpty(SubScreenName))
        {
            if(string.IsNullOrEmpty(other.SubScreenName))
                return ScreenName.Equals(other.ScreenName) && Application.Equals(other.Application);
            else
                return false;
        }
        else if (string.IsNullOrEmpty(other.SubScreenName))
            return false;
        else
            return ScreenName.Equals(other.ScreenName) && SubScreenName.Equals(other.SubScreenName) && Application.Equals(other.Application);
    }

    public override int GetHashCode()
    {
        if(SubScreenName == null)
            return ScreenName.GetHashCode() ^ Application.GetHashCode();
        else
            return ScreenName.GetHashCode() ^ SubScreenName.GetHashCode() ^ Application.GetHashCode();
    }
    #endregion
}

1 个答案:

答案 0 :(得分:6)

无状态会话不支持延迟加载。您需要尽早加载Screen个实体(在当前仅加载代理的同一查询中)。