如何使用JPA Spring Boot枚举映射,为什么不在数据库保存枚举键中将枚举值保存在DB中

时间:2019-01-03 10:58:40

标签: java spring-boot spring-data-jpa

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);
    }

}

如何使用jpa Spring Boot枚举映射,为什么在我将产品保存到数据库中时未在数据库保存枚举键中将枚举值保存在数据库中,所以未将其保存为活动状态

1 个答案:

答案 0 :(得分:-1)

创建表时,您只需要注意枚举值即可。 表格中的枚举值是多少?在状态列中,是定义为'Active', 'Inactive''ACTIVE', 'INACTIVE'的值。这将决定保存的值。

如果将枚举值定义为'ACTIVE', 'INACTIVE',则如果您插入“ active”作为状态值,则它将在数据库内部更改为“ ACTIVE”,因为它是基于预定义的枚举值进行插入的。 / p>