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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:13:13+00:00 2026-06-16T19:13:13+00:00

I have an entity that has an enum property: // MyFile.java public class MyFile

  • 0

I have an entity that has an enum property:

// MyFile.java
public class MyFile {   
    private DownloadStatus downloadStatus;
    // other properties, setters and getters
}

// DownloadStatus.java
public enum DownloadStatus {
    NOT_DOWNLOADED(1),
    DOWNLOAD_IN_PROGRESS(2),
    DOWNLOADED(3);

    private int value;
    private DownloadStatus(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
} 

I want to save this entity in database and retrieve it. The problem is that I save the int value in database and I get int value! I can not use switch like below:

MyFile file = new MyFile();
int downloadStatus = ...
switch(downloadStatus) {
    case NOT_DOWNLOADED:
    file.setDownloadStatus(NOT_DOWNLOADED);
    break;
    // ...
}    

What should I do?

  • 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-16T19:13:14+00:00Added an answer on June 16, 2026 at 7:13 pm

    You could provide a static method in your enum:

    public static DownloadStatus getStatusFromInt(int status) {
        //here return the appropriate enum constant
    }
    

    Then in your main code:

    int downloadStatus = ...;
    DowloadStatus status = DowloadStatus.getStatusFromInt(downloadStatus);
    switch (status) {
        case DowloadStatus.NOT_DOWNLOADED:
           //etc.
    }
    

    The advantage of this vs. the ordinal approach, is that it will still work if your enum changes to something like:

    public enum DownloadStatus {
        NOT_DOWNLOADED(1),
        DOWNLOAD_IN_PROGRESS(2),
        DOWNLOADED(4);           /// Ooops, database changed, it is not 3 any more
    }
    

    Note that the initial implementation of the getStatusFromInt might use the ordinal property, but that implementation detail is now enclosed in the enum class.

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

Sidebar

Related Questions

I have an entity that has a ManyToMany relationship with itself: @Entity public class
I have an entity class in my Enterprise Java application that has an entity
I have an entity class that has a property with an underlying db column
Imagine you have an entity that has some relations with other entities and you
I have an entity class that has 10 fields. I am using MS Validation
In my business model I have Entity class (IPoint interface ) that has a
Lets say we have an Entity class that has many fields and methods but
I have a entity that has a scalar string property Clarified. Clarified can have
I have an entity that has so many properties and the customer wants to
I have an entity that has a reference to another one like these: class

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.