1

I've got an issue about inserting an enum into a VARCHAR field. That's my enum:

public enum StatusPesquisaEnum {

    ATENDIDO("A"),
    ESPERA("E"),
    EMATENDIMENTO("EA"),
    NAOATENDIDO("NA");


    private StatusPesquisaEnum(String codigo){
        this.codigo = codigo;
    }

    private final String codigo;

    public String getCodigo() {
        return codigo;
    }

}

I want to insert A, E, EA and NA into the database. Furthermore I've tried a converter:

@Converter(autoApply = true)
public class StatusPesquisaEnumConverter 
        implements AttributeConverter<StatusPesquisaEnum, String> {

    @Override
    public String convertToDatabaseColumn(StatusPesquisaEnum status) {
        return status.getCodigo();
    }

    @Override
    public StatusPesquisaEnum convertToEntityAttribute(String codigoStatus) {
        return StatusPesquisaEnum.valueOf(codigoStatus);
    }
}

And it's mapped as:

@Column(name = "str_status")
@Enumerated(EnumType.STRING)
@Convert(converter=StatusPesquisaEnumConverter.class)
private StatusPesquisaEnum statusPesquisa;

But it is inserting ATENDIDO, ESPERA, EMATENDIMENTO and NAOATENDIDO into the database.

4
  • 1
    Remove the @Enumerated as this conflicts with a custom converter. Commented Nov 24, 2015 at 13:45
  • Thank you, It partially worked! I've made some changes and I'll edit the post it! (y) Commented Nov 24, 2015 at 15:52
  • Please don't add the solution in the question. Either create a new answer or just leave my comment as answer... Commented Nov 24, 2015 at 18:57
  • This is not one of those forums you find on the net with the silly [SOLVED] text. Instead, if there's an answer that solves your problem, mark is as the answer. Commented Nov 24, 2015 at 20:50

1 Answer 1

3

Remove the @Enumerated as this conflicts with a custom converter:

@Column(name = "str_status")
@Convert(converter = StatusPesquisaEnumConverter.class)
private StatusPesquisaEnum statusPesquisa;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.