Enum save in sleep mode

I am trying to save an Enum field in a database, but I had a problem displaying the field in the database. The code I have is as follows:

public enum InvoiceStatus {
PAID,
UNPAID;
}

and I use this enumeration 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 user of the application select the account status from the presentation (JSP) using the drop-down menu.

But I'm not sure how to match the value obtained from the drop-down menu in the Invoice Status field

I tried to match the resulting short value as follows, but it will not compile

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

can someone tell me how to match the data received from the view to the enumeration field?

+4
source share
2

Enum , . :

PAID = 0
UNPAID = 1

, PAID:

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

UNPAID:

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

, :

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

, inbStatus - 0 1. .

+2

, u

Enumerated(EnumType.ORDINAL)

, . , , . , , - " ". :

Enumerated(EnumType.STRING)

"enum" . ( Varchar). , , :

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
       //

}

, , ORDINAL. .

+2
source

All Articles