使用 NHibernate 的 HQL 进行具有多个内连接的查询

发布于 2024-08-28 23:21:51 字数 2749 浏览 2 评论 0原文

这里的问题是将用 LINQ to SQL 语法编写的语句转换为 NHibernate 的等效语句。 LINQ to SQL 代码如下所示:

var whatevervar = from threads in context.THREADs
                          join threadposts in context.THREADPOSTs
                            on threads.thread_id equals threadposts.thread_id
                          join posts1 in context.POSTs
                            on threadposts.post_id equals posts1.post_id
                          join users in context.USERs
                            on posts1.user_id equals users.user_id
                          orderby posts1.post_time
                          where threads.thread_id == int.Parse(id)
                          select new
                          {
                              threads.thread_topic,
                              posts1.post_time,
                              users.user_display_name,
                              users.user_signature,
                              users.user_avatar,
                              posts1.post_body,
                              posts1.post_topic
                          };

它本质上是试图获取给定论坛线程中的帖子列表。我能够为 NHibernate 提出的最好的方案(在本网站有用的用户的帮助下)是:

var whatevervar = session.CreateQuery("select t.Thread_topic, p.Post_time, " +
                                              "u.User_display_name, u.User_signature, " +
                                              "u.User_avatar, p.Post_body, p.Post_topic " +
                                              "from THREADPOST tp " +
                                              "inner join tp.Thread_ as t " +
                                              "inner join tp.Post_ as p " +
                                              "inner join p.User_ as u " +
                                              "where tp.Thread_ = :what")
                                              .SetParameter<THREAD>("what", threadid)
                                              .SetResultTransformer(Transformers.AliasToBean(typeof(MyDTO)))
                                              .List<MyDTO>();

但这不能很好地解析,抱怨连接表的别名是空引用。 MyDTO 是输出的自定义类型:

public class MyDTO
{
    public string thread_topic { get; set; }
    public DateTime post_time { get; set; }
    public string user_display_name { get; set; }
    public string user_signature { get; set; }
    public string user_avatar { get; set; }
    public string post_topic { get; set; }
    public string post_body { get; set; }
}

我没有主意,虽然可以通过直接 SQL 查询来执行此操作,但我希望正确执行此操作,而不会违背使用 ORM 的目的。

提前致谢!

编辑:

数据库如下所示: http://i41.tinypic.com/5agciu.jpg(还不能发布图片。)

The problem here consists of translating a statement written in LINQ to SQL syntax into the equivalent for NHibernate. The LINQ to SQL code looks like so:

var whatevervar = from threads in context.THREADs
                          join threadposts in context.THREADPOSTs
                            on threads.thread_id equals threadposts.thread_id
                          join posts1 in context.POSTs
                            on threadposts.post_id equals posts1.post_id
                          join users in context.USERs
                            on posts1.user_id equals users.user_id
                          orderby posts1.post_time
                          where threads.thread_id == int.Parse(id)
                          select new
                          {
                              threads.thread_topic,
                              posts1.post_time,
                              users.user_display_name,
                              users.user_signature,
                              users.user_avatar,
                              posts1.post_body,
                              posts1.post_topic
                          };

It's essentially trying to grab a list of posts within a given forum thread. The best I've been able to come up with (with the help of the helpful users of this site) for NHibernate is:

var whatevervar = session.CreateQuery("select t.Thread_topic, p.Post_time, " +
                                              "u.User_display_name, u.User_signature, " +
                                              "u.User_avatar, p.Post_body, p.Post_topic " +
                                              "from THREADPOST tp " +
                                              "inner join tp.Thread_ as t " +
                                              "inner join tp.Post_ as p " +
                                              "inner join p.User_ as u " +
                                              "where tp.Thread_ = :what")
                                              .SetParameter<THREAD>("what", threadid)
                                              .SetResultTransformer(Transformers.AliasToBean(typeof(MyDTO)))
                                              .List<MyDTO>();

But that doesn't parse well, complaining that the aliases for the joined tables are null references. MyDTO is a custom type for the output:

public class MyDTO
{
    public string thread_topic { get; set; }
    public DateTime post_time { get; set; }
    public string user_display_name { get; set; }
    public string user_signature { get; set; }
    public string user_avatar { get; set; }
    public string post_topic { get; set; }
    public string post_body { get; set; }
}

I'm out of ideas, and while doing this by direct SQL query is possible, I'd like to do it properly, without defeating the purpose of using an ORM.

Thanks in advance!

EDIT:

The database looks like this: http://i41.tinypic.com/5agciu.jpg (Can't post images yet.)

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

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

发布评论

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

评论(2

谈情不如逗狗 2024-09-04 23:21:51

当我希望 HQL 查询返回自定义类型(就像您一样)时,我总是这样做:

select new MyDTO (t.Thread_Topic, p.Post_time, u.User_Display_Name, .... ) 
from ...

我必须检查我的一些代码,但我认为在这种情况下我什至不使用 AliasToBeenTransformer 。
我心里不太清楚,因为我主要使用 NHibernate 的 ICriteria API(当使用这个 API 时,在执行此类操作时确实需要指定 resulttransformer)。

注意:我发现在属性名称中看到下划线很奇怪(或者相当尴尬)......

When I want a HQL query to return a custom type, like you do, I always do it like this:

select new MyDTO (t.Thread_Topic, p.Post_time, u.User_Display_Name, .... ) 
from ...

I would have to check some code of mine, but I think that I don't even use the AliasToBeenTransformer in such cases.
I don't know by heart, since I mostly use NHibernate's ICriteria API (and when using this one, you indeed need to specify the resulttransformer when executing such kind of operation).

NB: I find it weird (or rather awkward) to see underscores in property-names ...

吲‖鸣 2024-09-04 23:21:51

HQL 是对您的对象而不是表的查询!

在您的 HQL 中,我看到类 tp 和该类的属性 tp.Thread_ 之间的连接。您应该在那里区分 SQL 和 HQL。将 HQL 视为对对象 TP 的查询,而不是对底层表结构的查询。您可以发布您的领域模型(对象之间的关系)以便我们可以帮助您吗?

谢谢你的那张照片。但是:看起来您的对象是表格的副本,我不认为这就是您的想法?例如:我本以为线程和帖子之间的多对多关系将使用 Hibernate 进行映射。如果是这种情况,您可以将线程与帖子连接起来,而不必担心中间对象 threadpost,事实上,它只是保存这些对象之间的关系,对吗?

换句话说;用帖子列表装饰你的线程对象,并用线程列表装饰帖子对象。

[装饰]
也就是说,将线程列表作为帖子类的属性,并将帖子列表作为线程类的属性。您正在寻找的是 nhibernate 映射文件的多对多关系。这意味着您不需要映射类中的多对多表,只需映射帖子类和线程类的关系。

HQL is a query on your objects, not your tables!

In your HQL I see a join between the class tp and a property of that class, tp.Thread_. You should distinguish between SQL and HQL there. Think of HQL as a query on the object TP instead of on the underlying table structure. Can you post your domain model (the relations between your objects) so that we can help you out?

Thanks for that picture. However: it looks like your objects are a copy of your tables and I do not think that is what you had in mind? For example: I would have thought that the many-to-many relation between threads and posts would have been mapped using Hibernate. If that would be the case, you could join threads with posts without having to bother with the intermediate object threadpost, which, in fact, is just holding the relationship between those objects, right?

In other words; decorate your thread object with a list of posts, and decorate the post object with a list of threads.

[decorate]
That is to put a list of threads as a property on your post class, and put a list of posts as a property on your threads class. What you are looking for is the many-to-many relationship for nhibernate mapping files. That means you do not need to map the many-to-many table in a class, only map the relationship of the post and thread classes.

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