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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T13:50:26+00:00 2026-05-16T13:50:26+00:00

Currently, my project uses @Enumerated(EnumType.ORDINAL) , so when I sort by this column, it

  • 0

Currently, my project uses @Enumerated(EnumType.ORDINAL), so when I sort by this column, it is ordering based on the order in the enum, which works fine. But I need to add some additional values to the enum, which need to be inserted at different locations in the list of enum values and can’t be just added to the bottom to maintain the correct sort order.

If I do this, my database will be messed up. I’ll have to write some scripts to translate all these ordinal values to the correct new ordinal. There is a possibility that more status will have to be added later. Since I have to fix all the data in the database, I’d like to only have to do it once, as it will be a big task.

So I’m thinking of switching to EnumType.STRING to not have to remap ordinal values in the database again. But if I do this, then how do I sort properly? The alphabetical order of the enum strings is not the order I need.

Using the classes below, when I sort by the property “status”, the results come out in this order:

hql = "from Project order by status"

Development
Planning
Production

I’d like them to come out in this order, without using EnumType.ORDINAL:

Planning
Development
Production

Is this possible without creating a table for the enum, or adding an additional column to the Project class? I’ve tried this, but it throws an exception:

hql = "from Project order by status.sort"

The enum:

public enum Status {

    PLANNING("Planning", 0),
    DEVELOPMENT("Development", 1),
    PRODUCTION("Production", 2);

    private final String name;
    private final int sort;
    private Status(String name, int sort) {
        this.name = name;
        this.sort = sort;
    }
    @Override
    public String toString() {
        return name;
    }
}

The entity:

@Entity
public class Project {
    private Long id;
    private Status status;

    @Id
    @GeneratedValue
    public Long getId() {
        return this.id;
    }
    private void setId(Long id) {
        this.id = id;
    }
    @Enumerated(EnumType.STRING)
    public Status getStatus() {
        return status;
    }
    public void setStatus(Status status) {
        this.status = status;
    }
}
  • 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-16T13:50:27+00:00Added an answer on May 16, 2026 at 1:50 pm

    So I’m thinking of switching to EnumType.STRING to not have to remap ordinal values in the database again. But if I do this, then how do I sort properly? The alphabetical order of the enum strings is not the order I need.

    I personally totally avoid using the evil EnumType.ORDINAL as just changing the order of the constants would brake the persistence logic. Evil. That said, EnumType.STRING is indeed not always appropriate.

    In your case, here is what I would do (with standard JPA): I would persist an int at the entity level and perform the enum conversion in getter/setters. Something like this. First, the enum:

    public enum Status {
        PLANNING("Planning", 100),
        DEVELOPMENT("Development", 200),
        PRODUCTION("Production", 300);
    
        private final String label;
        private final int code;
    
        private Status(String label, int code) {
            this.label = label;
            this.code = code;
        }
    
        public int getCode() { return this.code; }
    
        private static final Map<Integer,Status> map;
        static {
            map = new HashMap<Integer,Status>();
            for (Status v : Status.values()) {
                map.put(v.code, v);
            }
        }
        public static Status parse(int i) {
            return map.get(i);
        }
    }
    

    So basically, the idea is to be able to get a Status by its code. And we keep some room between constants so that adding values is possible (it’s not pretty but, well, it will work and should be safe for some time).

    And then in the entity:

    @Entity
    public class Project {
        private Long id;
        private int statusCode;
    
        @Id @GeneratedValue
        public Long getId() {
            return this.id;
        }
        private void setId(Long id) {
            this.id = id;
        }
    
        @Transient
        public Status getStatus () {
            return Status.parse(this.statusCode);
        }
        public void setStatus(Status status) {
            this.statusCode = status.getCode();
        }
    
        protected int getStatusCode() {
            return statusCode;
        }
        protected void setStatusCode(int statusCode) {
            this.statusCode = statusCode;
        }
    }
    

    The getter/setter for the int representation are protected. The public getter/setter deal with the conversion.

    An alternative solution would be to use a custom type (at the cost of portability).

    Related questions

    • How to use enums with JPA
    • Map enum in JPA with fixed values ?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 537k
  • Answers 537k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You have typed an empty statement: int i=0; // that's… May 17, 2026 at 1:35 am
  • Editorial Team
    Editorial Team added an answer I would say he's about half right. The biggest purpose… May 17, 2026 at 1:35 am
  • Editorial Team
    Editorial Team added an answer PAGE_NOCACHE is murder on perf, it disables the CPU cache.… May 17, 2026 at 1:35 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a VB 20008 Express project which currently uses a DLL. I would
Our project currently uses Crystal Reports for Visual Studio 2008. We need to upgrade
Currently my InstallShield project uses a custom prerequiste to install Adobe Reader on the
Currently an EJB / Web Application project uses a JBoss-specific JNDI configuration file, placed
I currently have a project that uses g++ to compile it's code. I'm in
I'm currently debugging a project that uses an external library ( LibFirm ). When
I am currently running the local version of Caché on my system in order
I am currently working on an app that uses a broadcastreceiver to check for
I am currently working on a project in C# and use MySql as database.
I have a Python script which uses Tkinter for the GUI. My little script

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.