通过 HQL 在 Nhibernate 中选择

发布于 2024-10-28 21:13:04 字数 1057 浏览 0 评论 0原文

我有一些映射:

<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 来返回人员的姓氏、名字以及包含 City 、 Street 的地址集合

我如何在地址字段上添加条件?

I have some mapping:

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

It's my HQL query
hbm= "from person p join fetch p.Address WHERE p.id in (:ids)"

but now I get select * from both tables(Address and Person)
I need HQL that retern LastName,FirstName of person and collection of Addresses that contains City , Street

I haw I add conditions on Address fields??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尸血腥色 2024-11-04 21:13:04

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

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

Add Select p to only return Person.
Add an alias to Address so that you can use it in the where clause (conditions).

select p from person p join fetch p.Address ad WHERE p.id in (:ids) and a.City = :city
千寻… 2024-11-04 21:13:04

这个查询怎么样?

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]);
}

What about this query?

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

The result collection contains object arrays then:

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]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文