通过HQL在Nhibernate中选择

时间:2011-04-02 05:40:32

标签: nhibernate hql

我有一些映射:

<class entity-name="Person" table="table1">
<id column="Id" type="long" name="Id"/>
<set name="Address">
<key column="Person_id"/>
<one-to-many class="Address"/>
</set>
<property column="Id" name="Id" type="long"/>
<property column="Last_Name" name="LastName" type="string"/>
<property column="First_Name" name="FirstName" type="string"/>
<property column="Education" name="Education" type="string"/>   
</class>

<class entity-name="Address" table="table2">
<id column="Id" type="long" name="Id"/>
<property column="Id" name="Id" type="long"/>
<property column="City" name="City" type="long"/>
<property column="Street" name="Street" type="string"/>
<property column="number" name="number" type="string"/>
</class>

这是我的HQL查询        hbm =“from person p join fetch p.Address WHERE p.id in(:ids)”

但现在我从两个表中获取select *(地址和人物) 我需要HQL来重新命名LastName,人名FirstName和包含City,Street

的地址集合

我在地址字段中添加了条件吗?

2 个答案:

答案 0 :(得分:0)

添加选择p仅返回Person。 为Address添加别名,以便可以在where子句(条件)中使用它。

select p from person p join fetch p.Address ad WHERE p.id in (:ids) and a.City = :city

答案 1 :(得分:0)

这个查询怎么样?

select p.FirstName, p.LastName, a.City, a.Street from Person p inner join p.Addresses a WHERE p.Id in (:id) and a.City=:city

结果集合包含对象数组:

var resultList = query.List();
foreach (object[] result in resultList)
{
    Console.WriteLine("Person.FirstName = {0}", result[0]);
    Console.WriteLine("Person.LastName = {0}", result[1]);
    Console.WriteLine("Address.City = {0}", result[2]);
    Console.WriteLine("Address.Street = {0}", result[3]);
}