流利的对象列表异常



i使用fluent nhibernate映射查询(存储在db作为字符串)及其参数(存储在单独的表中)。

当我尝试使用服务层中的参数列表时,我会遇到问题。

List<QueryParameter> lqp = (List<QueryParameter>)qry.parameters;

投掷

无法施放类型Hibernate.Collection.generic.persistentGenericBag 1[nha.cs.utility.mole.QueryParameter]' to type 'System.Collections.Generic.List 1 [nha.cs.utility.mole.mole.mole.squeryparameter]'。


List<QueryParameter> lqp = qry.parameters.ToList<QueryParameter>();

投掷

初始化[nha.cs.utility.mole.mole.query#24] - 懒惰地初始化角色集合:nha.cs.utility.mole.mole.query.parameters,否不会关闭会话或会话


List<QueryParameter> lqp = new List<QueryParameter>(qry.parameters);

投掷

测试方法molesvstest.molesvctester.molesvctestmethod抛出了例外: nhibernate.LazyInitializationException:
初始化[nha.cs.utility.mole.query#24] - 懒惰地初始化一个角色集合:nha.cs.utility.mole.mole.query.query.parameters,没有会话或会话或会话已关闭


IList<QueryParameter> lqp = (IList<QueryParameter>)qry.parameters;

投掷

测试方法molesvstest.molesvctester.molesvctestmethod抛出了例外: nhibernate.LazyInitializationException:初始化[nha.cs.utility.mole.mole.query#24] - 懒惰地初始化角色集合:nha.cs.utility.mole.mole.query.query.query.parameters,no session或session,no session或session session

public class Query
{
    public virtual int id { get; protected set; }
    public virtual string name { get; set; }
    public virtual string query { get; set; }
    public virtual IList<QueryParameter> parameters { get; set; }
    public virtual IList<Application> applicationsUsedIn { get; set; }
    public Query()
    {
        this.parameters = new List<QueryParameter>();
        this.applicationsUsedIn = new List<Application>();
    }
    public virtual void AddParameter(QueryParameter qp)
    {
        qp.query = this;
        this.parameters.Add(qp);
    }
}


public class QueryMap : ClassMap<Query>
{
    public QueryMap()
    {
        Table("dbo.Queries");
        Id(x => x.id);
        Map(x => x.name);
        Map(x => x.query);
        HasMany(x => x.parameters)
            .Cascade.All()
            .KeyColumn("qryid")
            .LazyLoad()
            ;
        HasManyToMany(x => x.applicationsUsedIn)
            .Table("dbo.ApplicationsQueries")
            .ParentKeyColumn("qryid")
            .ChildKeyColumn("appid")
            .Inverse()
            .LazyLoad()
            ;
    }
}


    public XmlNode runQuery(string appnname, string qryname, List<String> parms)
    {
        XmlNode xn = null;
        if ((null != appnname) && (appnname.Length > 0))
        {
            if ((null != qryname) && (qryname.Length > 0))
            {
                Query qry = md.getQuery(appnname, qryname);
                if (null != qry)
                {
                    if ((null != parms) && (parms.Count > 0)) //Passed parameters as List<String>
                    {
                        //These are the three lines I have tried
                        IList<QueryParameter> lqp = (IList<QueryParameter>)qry.parameters;
                        List<QueryParameter> lqp = qry.parameters.ToList<QueryParameter>();
                        List<QueryParameter> lqp = new List<QueryParameter>(qry.parameters);
    ...
    ...
    ...


使用Queryparameter类和地图更新。

public class QueryParameter
{
    public virtual int id { get; set; }
    public virtual Query query { get; set; }
    public virtual string name { get; set; }
    public virtual MOLEDataTypes type { get; set; }
    public virtual int order { get; set; }
    public QueryParameter()
    {
    }
    public override bool Equals(object obj)
    {
        if (obj == null)
            return false;
        var t = obj as QueryParameter;
        if (t == null)
            return false;
        if (query == t.query && name == t.name)
            return true;
        return false;
    }
    public override int GetHashCode()
    {
        return (query.id + "|" + name).GetHashCode();
    }
}


public class QueryParametersMap : ClassMap<QueryParameter>
{
    public QueryParametersMap()
    {
        Table("dbo.QueryParameters");
        Id(x => x.id);
        References(x => x.query).Column("qryid").Not.Insert();
        Map(x => x.name);
        Map(x => x.type).CustomType<MOLEDataTypes>();
        Map(x => x.order).Column("ordr");
    }
}

更多代码

    public Query getQuery(string appname, string qryname)
    {
        Query retq = null;
        using (ISessionFactory isf = getSessionFactory())
        {
            using (var sess = isf.OpenSession())
            {
                using (var tran = sess.Transaction)
                {
                    try
                    {
                        tran.Begin();
                        ...
                        USING session
                        ...
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        sess.Close();
                        lws.logMessage(AppName, "getQuery", ex.ToString(), MessageType.Error, MessageLevel.Error);
                    }
                }
            }
        }
        return (retq);
    }

您可能有任何提示或建议将不胜感激。

谢谢,

bruce。

问题是您在getQuery中关闭了会话,然后要求您的Query对象的参数列表,而Nhibernate试图加载并且由于没有会话而失败。为了解决您的问题,您需要确保在整个请求期间开放会议,或者至少在所有业务交易完成之前保持开放。

我建议您采用每次课程的方法。每当有Web请求时,打开会话,并在会话结束时将其关闭。可以使用global.asax文件可以轻松实现这一点,您可以在此处阅读。

public class Global : System.Web.HttpApplication
{    
    public static ISessionFactory SessionFactory { get; private set; }  
    protected void Application_Start(object sender, EventArgs e)
    {
        // Create a session factory when the application starts.
        // Session factories are expensive to create, and therefore you should create one and use it throughout your application.
        SessionFactory = Fluently.Configure()
                                 .Database(
                                    SQLiteConfiguration.Standard
                                        .UsingFile("firstProject.db")
                                 )
                                 .BuildSessionFactory();
    }
    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        // Create a session per request.
        var session = SessionFactory.OpenSession();
        CurrentSessionContext.Bind(session);
    }
    protected void Application_EndRequest(object sender, EventArgs e)
    {
        // Close the session at the end of every request
        var session = CurrentSessionContext.Unbind(SessionFactory);
        session.Dispose();
    }
    protected void Application_End(object sender, EventArgs e)
    {
        // Close the session factory at the end of the application.
        if (SessionFactory != null)
            SessionFactory.Dispose();
    }
}

在上面的示例中,当应用程序启动时会创建会话工厂,这是建议的,因为创建会话工厂是昂贵的。然后,创建每个请求,并在最后处置。通过使用这种方法,您可以为自己节省很多工作,因为一切都是自动完成的。

然后,要在代码中使用会话,您只需要致电GetCurrentSession(),例如:

var session = Global.SessionFactory.GetCurrentSession();

我希望这会有所帮助,好运的是摆弄Nhibernate。我建议您考虑阅读NHIBERNATE 3.0食谱,以更好地了解Nhibernate。

最新更新