如何对 hql 中的链接列表进行排序?

发布于 2024-09-11 17:20:53 字数 341 浏览 0 评论 0原文

这个相同的问题,只是我想在 Hibernate 中执行此操作(如果重要的话可以使用 grails)。

因此,域类看起来像这样

class LinkedElement {
  LinkedElement precedingElement
  String someData
}

,我想按链接顺序查询所有元素(其中第一个 LinkedElement 的前置元素为 null)。这可以以有效的方式实现吗?

Same question as this, only I'd like to do it in Hibernate (using grails if that matters).

So the domain class looks like this

class LinkedElement {
  LinkedElement precedingElement
  String someData
}

and I'd like to query all elements in their linked order (where the first LinkedElement has null as the precedingElement). Is this possible in an efficient way?

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

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

发布评论

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

评论(1

尴尬癌患者 2024-09-18 17:20:53

您只能轻松找出谁在行的前面(即前面的元素为空)。不幸的是,这意味着您需要在 N+1 查询区域中才能获取整个列表。

Query 1 - who's in front 
Query 2 - who's behind 1
Query 3 - who's behind 2
....
Query n - who's behind n-1
Query n+1 - who's behind n -> no one is behind n, I must be at the end

参考您提到的问题,不要将高效误认为语法简洁。仅仅因为某些 DBMS 将为您提供方便的语法,它们仍然通过以下方式解决相同的问题:a.)执行相同的低效算法但使用简化的语法或 b.)提前索引事物,以便 DBMS 可以有效地访问您的数据,即使你没有那样建模。因此,如果您绝对必须使用 hibernate 使用指定的数据结构来解决这个问题,那么您应该考虑使用 本机 SQL 查询,在数据库级别进行调整,利用 DBMS 在该领域可能提供的功能。

如果您考虑一下您正在使用的数据结构,它非常适合表示堆栈。您只需进行几次操作即可推送、弹出和置顶。一般来说,这就是单向链表的优点。对于队列之类的东西,您需要考虑使用双向链表,因为您只需进行几个操作即可出队、前置和入队。对于将元素动态添加到列表中,LinkedList 非常有用。为了按顺序获取整个事物列表,LinkedList 本身效率相当低 - 您正在根据单向或双向查看 n+1 或 n 操作。相反,ArrayList 是正确的选择。想知道第三个元素是什么吗?酷,使用它的索引。与需要使用first.getNext.getNext的链表相比,这效率更高!但是,如果您需要向列表添加内容或将其用于排队或堆栈类型应用程序,那么它肯定有其缺点 - 与在链接列表中添加新链接相比,调整数组大小的成本昂贵。

我希望我能为您提供更好的答案,但希望这至少有些用处。

You can only easily find out who is at the front of the line (i.e. preceding element is null). Unfortunately, that means you're in N+1 query territory to get the whole list.

Query 1 - who's in front 
Query 2 - who's behind 1
Query 3 - who's behind 2
....
Query n - who's behind n-1
Query n+1 - who's behind n -> no one is behind n, I must be at the end

Referring to the question you mentioned, don't mistake efficient for syntactically concise. Just because some DBMS will give you convenient syntax they are still solving the same problem by either a.) performing the same inefficient algorithm but with simplified syntax or b.) indexing things ahead of time so the DBMS has access to your data efficiently even though you didn't model it that way. So, if you absolutely have to solve this problem with the specified data structure using hibernate, then you should look at using a Native SQL Query, tuning at the database level, taking advantage of the features your DBMS may offer you in this area.

If you think about the data structure you're using, it's great for representing a Stack. You can push, pop, and top with just a couple operations each. In general, that's what unidirectional linked lists are good for. For something like a queue, you'd want to think about using a bidirectional linked list since you can dequeue, front, and enqueue with just a couple operations each. For dynamically adding elements into a list, LinkedLists are great. For getting a whole list of things in order then LinkedLists are pretty inefficient by themselves - you're looking at n+1 or n operations depending on single or bidirectional. Instead, an ArrayList is the way to go. Want to know what the third element is? Cool, use its index. Compared to linked list where you need to use first.getNext.getNext this is way more efficient! But if you need to add stuff to the list or use it for a queuing or stack type application then it's definitely got its drawbacks - resizing arrays is expensive compared to adding a new link in a linked list.

I wish I had a better answer for you, but hopefully this is at least somewhat useful.

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