How can the NamedSQLQueryDefinition class be used dynamically as a sql-query equivalent?

前端 未结 2 2027
别那么骄傲
别那么骄傲 2021-01-25 20:34

I need to dynamically add named queries to the NHibernate configuration object. The search engines return few hits referencing NamedSQLQueryDefinition or NamedQueryDefinition. B

2条回答
  •  梦毁少年i
    2021-01-25 20:51

    I'm pretty new to this but most of the parameters look to be determinable from the attributes you can provide in the HBM file. That said, I'm not too sure what QuerySpaces are. The closest I've got to what I think you are trying to achieve is to use the following (untested):

    ISQLQuery q = session.CreateSQLQuery("exec pr_GETCustomer :p1");
    
    if (q is SqlQueryImpl)
    {
        IDictionary namedParams = new Dictionary();
        namedParams.Add("p1", new TypedValue(NHibernateUtil.Int32, 12345);
    
        IDictionary paramTypes = new Dictionary();
    
        NativeSQLQuerySpecification spec = 
            (q as SqlQueryImpl).GenerateQuerySpecification(namedParams);
    
        NativeSQLQueryDefiniton def = new NativeSQLQueryDefiniton(
            spec.QueryString,
            spec.SqlQueryReturns,
            spec.QuerySpaces,
            false,
            null,
            -1,
            -1,
            FlushMode.Never,
            CacheMode.Normal,
            true,
            "blah",
            paramTypes,
            false
        );
    }
    

    Obviously, I don't like the cast to SqlQueryImpl. I'm hoping that, once one of us has done it once, obscure properties like querySpaces can be understood, so you won't have to.

    Not that I expect it to 100% work, but it may be more achievable from here.

提交回复
热议问题