How to write an Enum to the database with JPA and Postman

Asked

Viewed 261 times

1

I am creating a product register, and when I pass through Postman the JSON of my product to be recorded, its type is being saved as NULL.

The product types are an Enum, I will put down the code I tried to use to record the guys. Am I doing something wrong? Should I pass JSON differently? I appreciate the help

Enum code :

public enum TipoProduto {

    INFORMATICA(1, "informatica"), ELETRONICOS(2, "eletronicos"), CAMA(3, "cama"), MESA(4, "mesa"), BANHO(5, "banho"), CALCADOS(6, "calcados");


    private String descricao;
    private Integer codigo;

    private TipoProduto(Integer codigo, String descricao) {
        this.codigo = codigo;
        this.descricao = descricao;
    }

    public String getDescricao() {
        return descricao;
    }

    public Integer getCodigo() {
        return codigo;
    }
}

How I am trying to pass JSON on POSTMAN:

{
    "nome": "Notebook",
    "TipoProduto": 1,
    "valor": 1300.00,
    "cor": "Cinza Fosco",
    "especificacoes": "8GB RAM, i7, 256GB"
}
  • How is JSON parsed? How the object is persisted in the database?

  • So, to persist I simply call Entity manager and give a persist on the product. In Product Resource I do the following: @POST @Produces(Mediatype.APPLICATION_JSON) public Response adds(Product) { productDao.adds(product); URI Uri = URI.create("/product/" + product.getId()); Return Response.created(Uri). build(); }

  • Have you debugged the program in Resource before persisting? Enum is filled in correctly?

  • So I debug it now, and it’s being filled as null :/

  • put the mapping of the entity that has the Enum Typoproduct

1 answer

0

Hi

Try passing JSON like this

{
    "nome": "Notebook",
    "TipoProduto": "INFORMATICA",
    "valor": 1300.00,
    "cor": "Cinza Fosco",
    "especificacoes": "8GB RAM, i7, 256GB"
}
  • Thanks for the reply, but it still didn’t work :/

  • Face the Enum does not pass filled nor by prayer

  • How is the mapping in the JPA entity that uses Enum?

  • @Column(nullable = false) @Enumerated(Enumtype.STRING) private Typeproduct type;

  • Since only this null was breaking me I mount the table allowing null, but it is not right due to the rule I try to apply

  • To despaired already, is just the One that passes empty, everything passes correctly

  • Using "INFORMATICA" you tried to debug the service? is coming null even when you arrive at the add method? Because for me if you do Typoproduct.valueOf("INFORMATICA") it works

  • André managed to figure out the problem, at least I echo, my attribute calls like, how much I changed him and everything else to typeProduct everything worked perfectly! Thanks for helping me out here!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.