I have a class in java that one of its attributes is an enum and has the following values:
HIPERTROFIA(1, "Hipertrofia"),
DEFINICAO(2, "Definição"),
RESISTENCIA(3, "Resistência"),
OUTROS(4, "Outros");
When I send the value 0, it saves the value in the bank 1. When I send the value 1, it saves the value 2 in the bank and so on.
package br.com.academia.domain;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import org.hibernate.validator.constraints.Length;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import br.com.academia.domain.enums.StatusSerie;
import br.com.academia.domain.enums.TipoSerie;
@Entity
public class Solicitacao implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator =
"seqSolicitacao")
@SequenceGenerator(name = "seqSolicitacao", sequenceName =
"seq_id_solicitacao")
private Integer id;
private Integer tipoSerie;
//definição de todos os atributos
public Solicitacao() {
super();
}
public Solicitacao((...), TipoSerie tipoSerie, (...)) {
super();
(...)
this.tipoSerie = (tipoSerie == null) ? null : tipoSerie.getCodigo();
(...)
}
//get e set
public TipoSerie getTipoSerie() {
return TipoSerie.toEnum(tipoSerie);
}
public void setTipoSerie(TipoSerie tipoSerie) {
this.tipoSerie = tipoSerie.getCodigo();
}
//get e set
}
The enum class:
package br.com.academia.domain.enums;
public enum TipoSerie {
HIPERTROFIA(1, "Hipertrofia"),
DEFINICAO(2, "Definição"),
RESISTENCIA(3, "Resistência"),
OUTROS(4, "Outros");
private int codigo;
private String descricao;
private TipoSerie(int codigo, String descricao) {
this.codigo = codigo;
this.descricao = descricao;
}
public int getCodigo() {
return codigo;
}
public String getDescricao() {
return descricao;
}
public static TipoSerie toEnum(Integer codigo)
{
if(codigo == null)
{
return null;
}
for (TipoSerie serie: TipoSerie.values())
{
if(codigo.equals(serie.getCodigo()))
{
return serie;
}
}
throw new IllegalArgumentException("Código inválido" + codigo);
}
}
Images that demonstrate the problem:
NoticethatinpostmanIsend0andthebanksaved1.
Myresource,whereIgettherequest
@RequestMapping(method=RequestMethod.POST)publicResponseEntity<Solicitacao>create(@Valid@RequestBodySolicitacaosolicitacao){solicitacao=solicitacaoService.save(solicitacao);URIuri=ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(solicitacao.getId()).toUri();
return ResponseEntity.created(uri).build();
}
In the service I only do a few sets in some values, but I do not make any changes to the type, which in turn, arrives in the wrong value service.