如何在nhibernate中使用存储过程

时间:2011-01-24 10:02:07

标签: nhibernate named-query

您好 我尝试在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]

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

此致

1 个答案:

答案 0 :(得分:9)

试试这个,您的映射: -

<?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>();

和YOURCLASS是: -

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

HTH