-2
import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;

import org.hibernate.annotations.GenericGenerator;

import com.lue.billingsystem.enums.Status;
import com.lue.billingsystem.enums.Types;

@Entity
@Table(name="product_tab")
public class Product implements Serializable{

    private static final long serialVersionUID = 8919320309645697466L;

    @Id
    @Column(name="prod_id",updatable=false)
    @GenericGenerator(name="product_tab_genetator",strategy="increment")
    @GeneratedValue(generator="product_tab_genetator")
    private Long id;

    private String name;

    @Enumerated(EnumType.STRING)
    @Column(name = "type")
    private Types type;

    @Column(name = "status")
    @Enumerated(EnumType.STRING)
    private Status status;

    @Column(name = "description", length = 200)
    private String description;

    @OneToMany(mappedBy="product")
    private List<Charge> charges;


    @Column(name = "create_date", columnDefinition = "DATETIME")
    private Date createDate;

    @Column(name = "update_date", columnDefinition = "DATETIME")
    private Date updateDate;

    //@Version
    private Integer version;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Types getType() {
        return type;
    }
    public void setType(Types type) {
        this.type = type;
    }
    public Status getStatus() {
        return status;
    }
    public void setStatus(Status status) {
        this.status = status;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public List<Charge> getCharges() {
        return charges;
    }
    public void setCharges(List<Charge> charges) {
        this.charges = charges;
    }
    public Date getCreateDate() {
        return createDate;
    }
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
    public Date getUpdateDate() {
        return updateDate;
    }
    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }
    public Integer getVersion() {
        return version;
    }
    public void setVersion(Integer version) {
        this.version = version;
    }


}





import org.springframework.http.HttpStatus;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.lue.billingsystem.enums.utils.StatusDeserializer;
import com.lue.billingsystem.exception.BillingException;

@JsonDeserialize(using = StatusDeserializer.class)
public enum Status {
    ACTIVE("Active"), INACTIVE("Inactive");

    private final String text;

    Status(final String text) {
        this.text = text;
    }

    @Override
    public String toString() {
        return text;
    }

    public String getText() {
        return this.text;
    }

    public static Status fromText(String text) {
        for (Status r : Status.values()) {
            if (r.getText().equals(text)) {
                System.out.println(r);
                return r;
            }
        }
        throw new BillingException("Your Status not valied: "+text +" ", HttpStatus.BAD_REQUEST, 400);
    }

}


import java.io.IOException;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.lue.billingsystem.enums.Status;

public class StatusDeserializer extends JsonDeserializer<Status> {

    @Override
    public Status deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
            throws IOException, JsonProcessingException {

        ObjectCodec oc = jsonParser.getCodec();
        JsonNode node = oc.readTree(jsonParser);

        if (node == null) {
            return null;
        }

        String text = node.textValue(); // gives "A" from the request

        if (text == null) {
            return null;
        }
        //System.out.println(Status.fromText(text) + "---------------");
        return Status.fromText(text);
    }

}

How to enum mapping with jpa Spring boot why not save enum value in DB in DB saving enum key when i saving product in databse not save status like Active it always save ACTIVE

Tim Diekmann
  • 7,755
  • 11
  • 41
  • 69
kaushal kumar
  • 11
  • 1
  • 2
  • 2
    Repeating your question four times to get around the 'too little text' pop up, isn't making your question any clearer. Please instead elaborate on your problem and give a description. –  Jan 03 '19 at 11:03
  • Google for "JPA AttributeConverter". – JB Nizet Jan 03 '19 at 11:07

1 Answers1

-1

You just need to pay attention to the enum values when the table is being created. What are the enum values in the table e.g. in status column, are the values defined as 'Active', 'Inactive' or 'ACTIVE', 'INACTIVE'. That's what will determine the value saved.

If the enum values are defined as 'ACTIVE', 'INACTIVE', if you insert 'active' as the value for status, it will change to 'ACTIVE' inside the database because it inserts based on the pre defined enum values.

Olantobi
  • 869
  • 1
  • 8
  • 16
  • thanks for answer , but in i have some different enum like public enum AccountType { RESIDENTIAL_ACCOUNT("Residential account"), BUSINESS_ACCOUNT("Business account"), SUNDRY_ACCOUNT("Sundry account"); } How to write this type enum please help me – kaushal kumar Jan 04 '19 at 05:12
  • I mean the enum definition in your database tables, not in your code. – Olantobi Jan 04 '19 at 06:04
  • https://stackoverflow.com/questions/7766791/serializing-enums-with-jackson – kaushal kumar Jan 08 '19 at 10:28