NHibernate通过ref_cursor从存储过程获取数据

发布于 2024-11-26 09:10:25 字数 3233 浏览 3 评论 0原文

我还无法通过 ref_cursor 从存储过程获取数据。 也许您可以建议您如何解决该问题 -

我的错误: “ORA-06553:PLS-306:调用“GET_SAMPLE_LIST”时参数数量或类型错误”} 我想调用中的参数类型是问题

所在这是我的代码:

HBM

<?xml version="1.0" encoding="utf-8" ?>
<!-- Generated by MoreGen 28-Apr-2008 11:27:28 -->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Infra"
               namespace="Infra.Entities">

<class name="Sample" table="MY_PACK.Get_Sample_List" lazy="true">
<id name="Id" column="ID">
  <generator class="native" />
</id>
<property name="Makat8" column="makat8" />
<property name="SerialNumber" column="serial_number" />

<loader query-ref="MY_PACK.Get_Sample_List"/>
</class>
<sql-query name="MY_PACK.Get_Sample_List" callable="true" >
<query-param name="p_crs" type="OracleDbType.RefCursor" />
<query-param name="p_1" type="int" />
<query-param name="p_2" type="string" />
<query-param name="p_3" type="int" />
<query-param name="p_4" type="date" />
<query-param name="p_5" type="date" />
<return alias="MY_PACK.Get_Sample_List" class="Sample">
  <return-property name="Makat8" column="makat8" />
  <return-property name="SerialNumber" column="serial_number" />

</return>
call MY_PACK.Get_Sample_List  (:p_crs, :p_1 , :p_2 , :p_3 , :p_4 , :p_5)


</sql-query>
</hibernate-mapping>

SAmple 实体:

public class Sample
{
    public virtual int Id { get; set; }
    public virtual int Makat8 { get; set; }
    public virtual int SerialNumber { get; set; }
}

会话配置 - 它很流畅,但仍然相同:

       m_Factory  = Fluently.Configure()  //cfgRules
              .Database(OracleClientConfiguration.Oracle10.Dialect("NHibernate.Dialect.Oracle10gDialect")
           .ConnectionString(x => x.FromConnectionStringWithKey("MyDBCONNSTRING"))
             .Driver<NHibernate.Driver.OracleDataClientDriver>().ShowSql())
             .ExposeConfiguration(c => c.Properties.Add("hbm2ddl.keywords", "none"))
            .Mappings(m =>
           {
               m.FluentMappings.AddFromAssemblyOf<MyEntityMapping>();
               m.HbmMappings.AddFromAssemblyOf<MyHBMMapping>();
               m.MergeMappings();
           })
           .BuildConfiguration()
           .BuildSessionFactory();

有测试:

using (ISession Session = NHibernateSessionProvider.GetSession("MyDBCONNSTRING"))
        {
            using (NHibernate.ITransaction Tran =    Session.BeginTransaction(System.Data.IsolationLevel.Serializable))
            {
                IQuery query = (IQuery)Session.GetNamedQuery("MY_PACK.Get_Sample_List").SetResultTransformer(Transformers.AliasToBean(typeof(Sample))); ;



                query.SetParameter("p_crs", OracleDbType.RefCursor);
                query.SetParameter("p_1", 1);
                query.SetParameter("p_2", 12345678);
                query.SetParameter("p_3", 1);
                query.SetDateTime("p_4", null);
                query.SetDateTime("p_5", null);




                IList result = query.List();

            }
        }

任何帮助将不胜感激!

I'm also having trouble to get data from Stored Procedure through ref_cursor .
Maybe you could advise how did you resolve that issue -

My error :
"ORA-06553: PLS-306: wrong number or types of arguments in call to 'GET_SAMPLE_LIST'"}
I suppose The types of arguments in call is the problem

Here is my code :

HBM

<?xml version="1.0" encoding="utf-8" ?>
<!-- Generated by MoreGen 28-Apr-2008 11:27:28 -->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Infra"
               namespace="Infra.Entities">

<class name="Sample" table="MY_PACK.Get_Sample_List" lazy="true">
<id name="Id" column="ID">
  <generator class="native" />
</id>
<property name="Makat8" column="makat8" />
<property name="SerialNumber" column="serial_number" />

<loader query-ref="MY_PACK.Get_Sample_List"/>
</class>
<sql-query name="MY_PACK.Get_Sample_List" callable="true" >
<query-param name="p_crs" type="OracleDbType.RefCursor" />
<query-param name="p_1" type="int" />
<query-param name="p_2" type="string" />
<query-param name="p_3" type="int" />
<query-param name="p_4" type="date" />
<query-param name="p_5" type="date" />
<return alias="MY_PACK.Get_Sample_List" class="Sample">
  <return-property name="Makat8" column="makat8" />
  <return-property name="SerialNumber" column="serial_number" />

</return>
call MY_PACK.Get_Sample_List  (:p_crs, :p_1 , :p_2 , :p_3 , :p_4 , :p_5)


</sql-query>
</hibernate-mapping>

The SAmple entity :

public class Sample
{
    public virtual int Id { get; set; }
    public virtual int Makat8 { get; set; }
    public virtual int SerialNumber { get; set; }
}

Session configuration - it's Fluent but still the same :

       m_Factory  = Fluently.Configure()  //cfgRules
              .Database(OracleClientConfiguration.Oracle10.Dialect("NHibernate.Dialect.Oracle10gDialect")
           .ConnectionString(x => x.FromConnectionStringWithKey("MyDBCONNSTRING"))
             .Driver<NHibernate.Driver.OracleDataClientDriver>().ShowSql())
             .ExposeConfiguration(c => c.Properties.Add("hbm2ddl.keywords", "none"))
            .Mappings(m =>
           {
               m.FluentMappings.AddFromAssemblyOf<MyEntityMapping>();
               m.HbmMappings.AddFromAssemblyOf<MyHBMMapping>();
               m.MergeMappings();
           })
           .BuildConfiguration()
           .BuildSessionFactory();

There's the test :

using (ISession Session = NHibernateSessionProvider.GetSession("MyDBCONNSTRING"))
        {
            using (NHibernate.ITransaction Tran =    Session.BeginTransaction(System.Data.IsolationLevel.Serializable))
            {
                IQuery query = (IQuery)Session.GetNamedQuery("MY_PACK.Get_Sample_List").SetResultTransformer(Transformers.AliasToBean(typeof(Sample))); ;



                query.SetParameter("p_crs", OracleDbType.RefCursor);
                query.SetParameter("p_1", 1);
                query.SetParameter("p_2", 12345678);
                query.SetParameter("p_3", 1);
                query.SetDateTime("p_4", null);
                query.SetDateTime("p_5", null);




                IList result = query.List();

            }
        }

Any help will be appreceated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人疚 2024-12-03 09:10:25

自从我在 Oracle 中使用 NHibernate 以来已经有一段时间了,但我似乎记得一些事实,除非 ref_cursor 存储过程参数的名称是一个特定值(尝试沿着这些线进行谷歌搜索),否则它不起作用,是过程的第一个参数(就像你的一样)。

It's been a while since I've used NHibernate with Oracle, but I seem to remember something about the fact that it didn't work unless the name of the ref_cursor stored procedure parameter was a specific value (try to google along those lines), and was the first parameter of the proc (as yours is).

冷了相思 2024-12-03 09:10:25

REF CURSOR 必须是过程中的第一个参数 (pCursor OUT ReferenceCursor):

Package Body

PROCEDURE usp_GetDual
    (
          pCursor OUT ReferenceCursor,
          a IN CHAR,
          b IN CHAR
    )

  IS

    err_code NUMBER;
    err_msg VARCHAR2(200);

  BEGIN

  OPEN pCursor FOR
    SELECT * FROM dual;

   EXCEPTION
    WHEN OTHERS THEN 
        err_code := SQLCODE;
        err_msg := substr(SQLERRM, 1, 200);

END usp_GetDual;

以及您的映射 XML:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyAssembly">
    <sql-query name="GetDaul">
        { call MYPACKAGE.usp_GetDual ( :a, :b ) }
    </sql-query>
</hibernate-mapping>

现在您可以简单地查询使用这个:

var value = Session.GetNamedQuery("GetDaul")
    .SetParameter<string>("a", "AAA")
    .SetParameter<string>("b", "BBB")
    .UniqueResult();

您可以在此处找到更好的解释。

这篇文章可能会帮助您理解一些事情。

REF CURSOR must be the first parameter in your procedure (pCursor OUT ReferenceCursor):

Package Body:

PROCEDURE usp_GetDual
    (
          pCursor OUT ReferenceCursor,
          a IN CHAR,
          b IN CHAR
    )

  IS

    err_code NUMBER;
    err_msg VARCHAR2(200);

  BEGIN

  OPEN pCursor FOR
    SELECT * FROM dual;

   EXCEPTION
    WHEN OTHERS THEN 
        err_code := SQLCODE;
        err_msg := substr(SQLERRM, 1, 200);

END usp_GetDual;

and your mapping XML:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyAssembly">
    <sql-query name="GetDaul">
        { call MYPACKAGE.usp_GetDual ( :a, :b ) }
    </sql-query>
</hibernate-mapping>

Now you can simply query using this:

var value = Session.GetNamedQuery("GetDaul")
    .SetParameter<string>("a", "AAA")
    .SetParameter<string>("b", "BBB")
    .UniqueResult();

You can find a better explanation here.

This article might help you to understand things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文