Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 947979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:10:04+00:00 2026-05-15T23:10:04+00:00

Same question as this , only I’d like to do it in Hibernate (using

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:10:04+00:00Added an answer on May 15, 2026 at 11:10 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: This question looks like it might be the same problem, but has no
I have the same question like this: Delay before showing the tooltip I need
I've asked this same question with Python. Now I like to know if this
This is the same question for older version of Scala, but they say that
(Please note that the behavior described in this question only appeared because of something
I'm having the exact same problem as in this question: Gray border when using
Why the same question again This question has been asked around 100 times on
I basically have the same question as this guy .. The example in the
Basically, I have the same question as this one , which unfortunately never got
I thought I would rewrite this question (same iteration). The original was how to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.