如何在nhibernate中使用存储过程

发布于 2024-10-14 02:41:41 字数 3900 浏览 3 评论 0原文

你好 我尝试在nhibernate中使用存储过程,我发现了一些方法:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <sql-query name="CO_Visites_Treeview_Sel">
    exec CO_Visites_Treeview_Sel :Idclient, :Idmagasin, :Autre, :Tous
  </sql-query>
</hibernate-mapping>

如果我想使用我将使用的数据:

var query = session.GetNamedQuery("CO_Visites_Treeview_Sel");
query.SetString("Idclient", lstClients.SelectedValue.ToString());
query.SetInt32("Idmagasin", 36);
query.SetBoolean("Autre", false);
query.SetBoolean("Tous", true);
var results = query.List();

在这种情况下,我不会有智能,结果['colName']

我使用另一种方法:

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
      <class name="NHibernate.DataAccess.Models.Visites_Treeview,NHibernate.DataAccess" lazy="true">
        <id name="Idvisite" column="IDVISITE" type="string">
          <generator class="assigned" />
        </id>
        <property column="NOMMAGASIN" name="Nommagasin" type="string" />
        <property column="DATEVIS" name="Datevis" type="DateTime" />
        <property column="INTERVENTION" name="Intervention" type="Boolean" />
        <property column="IDFACTURE" name="Idfacture" type="string" />
        <property column="STATUT" name="Statut" type="byte" />
        <property column="NOFACTURE" name="Nofacture" type="int" />
        <property column="NODEVIS" name="Nodevis" type="int" />
        <property column="LIVRE" name="Livre" type="Boolean" />
        <property column="NOCOMMANDE" name="Nocommande" type="int" />
        <property column="COMPTABILISEE" name="Comptabilisee" type="Boolean" />
        <property column="RECUP" name="Recup" type="Boolean" />
        <property column="MASQUE" name="Masque" type="Boolean" />
      </class>
      <sql-query name="CO_Visites_Treeview_Sel">
        <return alias="Visites_Treeview" class="NHibernate.DataAccess.Models.Visites_Treeview,NHibernate.DataAccess">
          <return-property column="NOMMAGASIN" name="Nommagasin" />
          <return-property column="IDVISITE" name="Idvisite" />
          <return-property column="DATEVIS" name="Datevis" />
          <return-property column="INTERVENTION" name="Intervention" />
          <return-property column="IDFACTURE" name="Idfacture" />
          <return-property column="STATUT" name="Statut" />
          <return-property column="NOFACTURE" name="Nofacture" />
          <return-property column="NODEVIS" name="Nodevis" />
          <return-property column="LIVRE" name="Livre" />
          <return-property column="NOCOMMANDE" name="Nocommande" />
          <return-property column="COMPTABILISEE" name="Comptabilisee" />
          <return-property column="RECUP" name="Recup" />
          <return-property column="MASQUE" name="Masque" />
        </return>
        exec CO_Visites_Treeview_Sel :Idclient, :Idmagasin, :Autre, :Tous
      </sql-query>
    </hibernate-mapping>

我创建了一个bean班级。 在这种情况下,结果将具有强类型对象,但我有一些一致性问题。 但我没有真正的标识符,所以如果有两个 Idvisite,第二个将被忽略。 我尝试创建一个复合键,但它不起作用。

<composite-id>
  <key-property name="Idvisite" column="IDVISITE" type="string" />
  <key-property name="Idfacture" column="IDFACTURE" type="string" />
</composite-id>

我有一个错误:

could not execute query
[ exec CO_Visites_Treeview_Sel @p0, @p1, @p2, @p3 ]
  Name:Idclient - Value:036000004130  Name:Idmagasin - Value:36  Name:Autre - Value:False  Name:Tous - Value:True
[SQL: exec CO_Visites_Treeview_Sel @p0, @p1, @p2, @p3]

有人可以告诉我该怎么做吗?

问候

Hello
I try to use stored procedure in nhibernate and I found some methods :

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <sql-query name="CO_Visites_Treeview_Sel">
    exec CO_Visites_Treeview_Sel :Idclient, :Idmagasin, :Autre, :Tous
  </sql-query>
</hibernate-mapping>

If I want to use the data I will use :

var query = session.GetNamedQuery("CO_Visites_Treeview_Sel");
query.SetString("Idclient", lstClients.SelectedValue.ToString());
query.SetInt32("Idmagasin", 36);
query.SetBoolean("Autre", false);
query.SetBoolean("Tous", true);
var results = query.List();

In that case I won't have intellisence neither result['colName']

I use another method :

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
      <class name="NHibernate.DataAccess.Models.Visites_Treeview,NHibernate.DataAccess" lazy="true">
        <id name="Idvisite" column="IDVISITE" type="string">
          <generator class="assigned" />
        </id>
        <property column="NOMMAGASIN" name="Nommagasin" type="string" />
        <property column="DATEVIS" name="Datevis" type="DateTime" />
        <property column="INTERVENTION" name="Intervention" type="Boolean" />
        <property column="IDFACTURE" name="Idfacture" type="string" />
        <property column="STATUT" name="Statut" type="byte" />
        <property column="NOFACTURE" name="Nofacture" type="int" />
        <property column="NODEVIS" name="Nodevis" type="int" />
        <property column="LIVRE" name="Livre" type="Boolean" />
        <property column="NOCOMMANDE" name="Nocommande" type="int" />
        <property column="COMPTABILISEE" name="Comptabilisee" type="Boolean" />
        <property column="RECUP" name="Recup" type="Boolean" />
        <property column="MASQUE" name="Masque" type="Boolean" />
      </class>
      <sql-query name="CO_Visites_Treeview_Sel">
        <return alias="Visites_Treeview" class="NHibernate.DataAccess.Models.Visites_Treeview,NHibernate.DataAccess">
          <return-property column="NOMMAGASIN" name="Nommagasin" />
          <return-property column="IDVISITE" name="Idvisite" />
          <return-property column="DATEVIS" name="Datevis" />
          <return-property column="INTERVENTION" name="Intervention" />
          <return-property column="IDFACTURE" name="Idfacture" />
          <return-property column="STATUT" name="Statut" />
          <return-property column="NOFACTURE" name="Nofacture" />
          <return-property column="NODEVIS" name="Nodevis" />
          <return-property column="LIVRE" name="Livre" />
          <return-property column="NOCOMMANDE" name="Nocommande" />
          <return-property column="COMPTABILISEE" name="Comptabilisee" />
          <return-property column="RECUP" name="Recup" />
          <return-property column="MASQUE" name="Masque" />
        </return>
        exec CO_Visites_Treeview_Sel :Idclient, :Idmagasin, :Autre, :Tous
      </sql-query>
    </hibernate-mapping>

I created a bean class.
In that case results will have strongly typed object but I have some coherency problem.
But I have no real identifier so if there is two Idvisite, the second will be ignored.
I tryed to create a composite key but it doesn't work.

<composite-id>
  <key-property name="Idvisite" column="IDVISITE" type="string" />
  <key-property name="Idfacture" column="IDFACTURE" type="string" />
</composite-id>

And I've an error :

could not execute query
[ exec CO_Visites_Treeview_Sel @p0, @p1, @p2, @p3 ]
  Name:Idclient - Value:036000004130  Name:Idmagasin - Value:36  Name:Autre - Value:False  Name:Tous - Value:True
[SQL: exec CO_Visites_Treeview_Sel @p0, @p1, @p2, @p3]

Can someone tell me how to do ?

Regards

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

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

发布评论

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

评论(1

彼岸花似海 2024-10-21 02:41:42

试试这个,你的映射:-

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <sql-query name="CO_Visites_Treeview_Sel">
    exec CO_Visites_Treeview_Sel :Idclient, :Idmagasin, :Autre, :Tous
  </sql-query>
</hibernate-mapping>

使用 SetResultTransformer...

var results = Session
    .GetNamedQuery("CO_Visites_Treeview_Sel")
    .SetString("Idclient", lstClients.SelectedValue.ToString())
    .SetInt32("Idmagasin", 36)
    .SetBoolean("Autre", false)
    .SetBoolean("Tous", true)
    .SetResultTransformer(new AliasToBeanResultTransformer(typeof(YOURCLASS)));

return results.List<YOURCLASS>();

而你的类是:-

public class YOURCLASS 
{
    public virtual int ColA { get; set; }
    public virtual string COLB { get; set; }
    public virtual int COLC { get; set; }
    // etc..
}

HTH

Try this, your mapping:-

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <sql-query name="CO_Visites_Treeview_Sel">
    exec CO_Visites_Treeview_Sel :Idclient, :Idmagasin, :Autre, :Tous
  </sql-query>
</hibernate-mapping>

use SetResultTransformer...

var results = Session
    .GetNamedQuery("CO_Visites_Treeview_Sel")
    .SetString("Idclient", lstClients.SelectedValue.ToString())
    .SetInt32("Idmagasin", 36)
    .SetBoolean("Autre", false)
    .SetBoolean("Tous", true)
    .SetResultTransformer(new AliasToBeanResultTransformer(typeof(YOURCLASS)));

return results.List<YOURCLASS>();

and YOURCLASS is:-

public class YOURCLASS 
{
    public virtual int ColA { get; set; }
    public virtual string COLB { get; set; }
    public virtual int COLC { get; set; }
    // etc..
}

HTH

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