4

I am trying to save an Enum field to database but I am having a problem mapping the field to database. The code I have is as follows:

public enum InvoiceStatus {
PAID,
UNPAID;
}

and I am using this enum in one of my application classes as follows:

public class Invoice {

Enumerated(EnumType.ORDINAL)
@Column(name="INVOICE_STATUS", nullable = false, unique=false)
private InvoiceStatus invoiceStatus;

}

finally I let the app user select the Invoice Status from the view (JSP) using a drop down menu.

But I am not sure how to map the value received from the drop down menu selection to the Invoice Status field

I tried mapping the value received to short as follows, but it won't compile

invoice.setInvoiceStatus(Short.parseShort(request.getParameter("inbStatus")));

can someone please tell me how to map the data received from the view to the enum field?

1
  • that rather depends on what you used as the value in the input on the web side....! Commented Jan 16, 2014 at 21:15

2 Answers 2

2

Enum ordinal values are zero based indexes. In your case:

PAID = 0
UNPAID = 1

So the following code will return PAID:

int invoiceStatus = 0;
invoice.setInvoiceStatus(InvoiceStatus.values()[invoiceStatus]);

And the following code will return UNPAID:

int invoiceStatus = 1;
invoice.setInvoiceStatus(InvoiceStatus.values()[invoiceStatus]);

That means you should be able to do this way:

short invoiceStatus = Short.parseShort(request.getParameter("inbStatus"));
invoice.setInvoiceStatus(InvoiceStatus.values()[invoiceStatus]);

But only if inbStatus is 0 or 1. You should always validate user input for null and invalid values.

Sign up to request clarification or add additional context in comments.

Comments

2

I see that u are using

Enumerated(EnumType.ORDINAL)

however after a while it could be quite difficult to troubleshoot if your enum will grow. Another issue with the ordinal is that you could refactor your code and change the order of the enum values and after that you could be in trouble. Mainly if it is a shared codebase and someone just decides to cleanup the code and "group the relevant enum constants together". If you'll use:

Enumerated(EnumType.STRING)

Directly the enum "name" will be inserted into the database. (Therefore you need Varchar type). If you want to present more user friendly version of your enum you could probably have:

public enum InvoiceStatus {
    PAID(0, "Paid"), UNPAID(1, "Unpaid"), FAILED(2, "Failed"), PENDING(3, "Pending");

    private int st;
    private in uiLabel;

    private InvoiceStatus(int st, String uiLabel){
        this.st = st;
        this.uiLabel = uiLabel;
    }

    private Map<String, InvoiceStatus> uiLabelMap = new HashMap<String, InvoiceStatus> ();

    static {
      for(InvoiceStatus status : values()) {
        uiLableMap.put(status.getUiLabel(), status);
      }
    }

    /** Returns the appropriate enum based on the String representation used in ui forms */
    public InvoiceStatus fromUiLabel(String uiLabel) {
      return uiLableMap.get(uiLabel); // plus some tweaks (null check or whatever)
    }

       //
       // Same logic for the ORDINAL if you are keen to use it
       //

}

Probably this could be also a solution for your problem, however i would really not use the ORDINAL based mapping. But just personal feeling.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.