1

hbm ファイル内のコンテナーに割り当てられたアイテムの数を取得しようとしています。私は少し掘り下げて、hbmコードをここまで取得することができました(以下!)。コンテナ オブジェクトがクエリされるたびにカウントを取得する必要があります。インターセプターを使用できますが、もっと良い方法があると思います。私は正しい軌道に乗っていますか、それとも別の戦略を使用してカウントをロードする必要がありますか?

ありがとう。

PS NH v2.2 を使用しています

   <?xml version="1.0" encoding="utf-8"?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false" assembly="MyEntities" namespace="Entities.Containers">   <class name="Container" table="[Container]" xmlns="urn:nhibernate-mapping-2.2">
    <id name="Id" column="Id" type="Int32" unsaved-value="0">
      <generator class="native" />
    </id>
    <property name="Capacity" column="Capacity">
      <column name="Capacity" />
    </property>
    <property name="Description" column="Description" length="50" type="String">
      <column name="Description" />
    </property>
    <loader query-ref="sqCurrentContainerAllocation"/>   </class>

  <sql-query name="sqCurrentContainerAllocation">
    <return-scalar column="AllocatedItemsCount" type="int"></return-scalar>
      SELECT COUNT(*) FROM [ContainerTracking]
      WHERE [ContainerId] = :Id   </sql-query> 
   </hibernate-mapping>
4

1 に答える 1

0

計算されたプロパティを取得する必要がある場合は、 でマッピングを使用できますformula。C# クラスを拡張しましょう。

public class Container
{
  ... // ID, Capacity, Description
  public virtual int MyCount { get; set; }

マッピングを拡張する

<class name="Container" table="[Container]"
  ...
  <property name="MyCount" insert="false" update="false" >
      <formula>
      (
         SELECT count(*) 
         FROM [ContainerTracking] as ct
         WHERE ct.[ContainerId] = Id
      )
      </formula>
  </property> 

これIdは、「_this.Id」、列 ID の名前、およびそのエイリアスのようなものに置き換えられます

もちろん、これは常にカウントをロードします(プロジェクションを除く)ので、使用する前によく考えてください

于 2013-06-04T15:54:55.627 に答える