2011-06-29 6 views
0

enter image description hereNHibernateでクエリを作成する方法は?

私は、ProductionbyEmployeeテーブルのDate、DataQuantity、FeetQuantityを取得し、EmployeeテーブルにOperatorNumを設定する必要があります。操作の数はパラメータであり、NHibernateを使用しています

hqlクエリ。 Nhibernate 3.2

+1

nhibernateクエリの例をいくつか示します:http://ayende.com/blog/4023/nhibernate-queries-examples – reggie

+0

可能な複製の[NHibernateでクエリを作成する方法?](http://stackoverflow.com/questions/6524392/how-to-make-a-query-with-nhibernate) – Rup

答えて

1

通常、オブジェクトは可能な限り透明な方法でオブジェクトをリレーショナルデータベースにロードして永続化するためにORMを使用します。データの特定部分の予測を作成することは可能ですが、シナリオでは特定のOperatorNumを持つEmployeeエンティティをフェッチし、そのEmployeeの生産メトリックの一部を検査する必要があります。

重要な点として、HQLクエリは、ドメインオブジェクトの名前とプロパティ名に対して宣言され、データベーステーブルと列名に対しては宣言されません。

あなたのエンティティとそのプロパティは、あなたのテーブルと列に同じ名前されている場合、それは次のようになります。

// you need an ISession variable -- here let's assume it is called session 
var operatorNum = 5; 
var query = session.CreateQuery(
    @"from Employee emp where emp.OperatorNum = :operatorNum") 
    .SetProperties(new { operatorNum }); 
var employee = query.UniqueResult<Employee>(); 

// you can now get the collection of production metrics in the 
// employee.ProductionbyEmployee property; note that if you have 
// not mapped this collection to be eagerly fetched, you'll get 
// a second roundtrip to the database to get the collection 

(私はこれがあなたが求めている正確に何ではないかもしれないことを認識し、それが困難ですあなたのエンティティやマッピングがどのようなものかわからずに達成しようとしていることを教えてください。)

関連する問題