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 7955757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:47:21+00:00 2026-06-04T03:47:21+00:00

I’d like to fetch data from a table ordered by priority column that is

  • 0

I’d like to fetch data from a table ordered by priority column that is enum. We use javaee 6, GF 3.1.

Our entity looks like this

@Entity
@Table(name="ITEMS")
@NamedQueries({
    @NamedQuery(
        name = "Item.findbyPriority",
        query = "select i from Item i where ORDER BY i.priority DESC" )
})
public class Item implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Integer id;



    String data inputFile;


    @Enumerated(EnumType.ORDINAL)
    Priority priority;


...
}

Priority is defined

public enum Priority {

    HIGH,
    NORMAL;
}

However Item.findbyPriority query sorts by ordinal, and returns result in wrong order. If I change Priority class to

public enum Priority {

        NORMAL,
        HIGH;
    }

It works OK. But that is not really robust. What if I add new Priority use or reorder it?

I tried to override compareTo method but it is final in Enum class.

What can I do in this case? How can it be done in nice way?

I know I could store priority as number etc, but than I have to sync entities to Enum by hand, which is not super nice.
Maybe @PrePersist, @PreUpldate and @PostLoad methods are the way to go? How would then jpql query look like?

thanks,

milan

  • 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-06-04T03:47:23+00:00Added an answer on June 4, 2026 at 3:47 am

    Only way how you can affect order of priority outside of your named query is choosing type of enum attribute (ORDINAL, STRING).

    Even if overriding compareTo would be aloud, it does not change a thing, because it is not called when query is executed. Reason for this is that JPQL query is converted to the SQL query, and there is no concept of calling Java methods from SQL query.

    Also specifying callback methods does not help, because they to not have anything to do with generating query or affecting order of result list.

    What you can do is to have post processing method that defines order for your list and just call that after you have results from executing named query.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I would like to run a str_replace or preg_replace which looks for certain words

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.