开发者

how to make a query with NHibernate?

how to make a query with NHibernate?

I need to get the Date, DataQuantity, FeetQuantity of the ProductionbyEmployee table and based a O开发者_运维知识库peratorNum on the Employee table. the number of operations is a parameter and using NHibernate

hql query. Nhibernate 3.2


Usually, one uses an ORM in order to load and persist objects to relational databases in as transparent a way as possible. While it is possible to create projections of specific parts of the data, it seems like in your scenario you want to fetch an Employee entity with a specific OperatorNum, and then inspect some that Employee's production metrics.

Important to note is that HQL queries are declared against the names and property names of your domain objects, not against the database table and column names.

If your entities and their properties are named identically to your tables and columns, it would look like this:

// 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

(I realize that this may not be exactly what you are asking for; it is difficult for me to tell what you are trying to achieve without knowing what your entities or mappings look like.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜