0

I have a SpringBoot project for REST webservices with a PostgreSQL database.

One of my entity has an enumeration which refers to an enumeration in database

CREATE TYPE statut_unite_hierarchie AS ENUM (
    'Archivé',
    'Actif'
);

In order to make the mapping, I followed Hibernate documentation here which uses JPA 2.1 type converter.

My enum java side :

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum StatutUniteOperationnelle
{
    /** Archivé */
    ARCHIVE("Archivé"),

    /** Actif */
    ACTIF("Actif");

    private String type;

    StatutUniteOperationnelle(String type)
    {
        this.setType(type);
    }

    public String getType()
    {
        return type;
    }

    public void setType(String type)
    {
        this.type = type;
    }

    public static StatutUniteOperationnelle fromType(String type)
    {
        switch (type)
        {
            case "Archivé":
                return ARCHIVE;
            case "Actif":
                return ACTIF;
        }

        return null;
    }
}

This is how I make the mapping : I have the following statement in my entity UniteOperationnelle.java :

@Convert(converter = StatutUniteOperationnelleConverter.class)
private StatutUniteOperationnelle statut;

And my converter StatutUniteOperationnelleConverter.java looks like that :

@Converter
public class StatutUniteOperationnelleConverter implements AttributeConverter<StatutUniteOperationnelle, String>
{
    @Override
    public String convertToDatabaseColumn(StatutUniteOperationnelle value)
    {
        if (value == null) { return null; }

        return value.getType();
    }

    @Override
    public StatutUniteOperationnelle convertToEntityAttribute(String value)
    {
        if (value == null) { return null; }

        return StatutUniteOperationnelle.fromType(value);
    }

}

I have a service who displays the UniteOperationnelle depending on the status, which must be "active".

@Override
public List<UniteOperationnelle> getAllUniteOperationnelle()
{
    return uoDao.findByStatut(StatutUniteOperationnelle.ACTIF);
}

But at the execution, I get the following error:

2016-07-08 11:01:38.406 DEBUG 9964 --- [nio-8080-exec-1] org.hibernate.SQL                        : select uniteopera0_.id as id1_5_, uniteopera0_.chemin as chemin2_5_, uniteopera0_.code as code3_5_, uniteopera0_.dirigeant as dirigean7_5_, uniteopera0_.libelle as libelle4_5_, uniteopera0_.parent as parent8_5_, uniteopera0_.statut as statut5_5_, uniteopera0_.type as type6_5_ from unite_hierarchie uniteopera0_ where uniteopera0_.statut=?
2016-07-08 11:01:38.416 DEBUG 9964 --- [nio-8080-exec-1] tributeConverterSqlTypeDescriptorAdapter : Converted value on binding : ACTIF -> Actif
2016-07-08 11:01:38.422 DEBUG 9964 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper   : could not extract ResultSet [n/a]
ERROR: operator does not exist: statut_unite_hierarchie = character varying
  Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.

which is so weird cause if I execute the given request using a string as parameter in my database, it works like a charm...

Any idea on this issue ?

M4veR1K
  • 21
  • 1
  • 9
  • Just as a verification test, does the mapping work correctly if you just use the JPA `Enumerated` annotation rather than a converter? – Gimby Jul 08 '16 at 09:29
  • @Gimby I get the same error using `@Enumerated(EnumType.STRING)`. But that wouldn't be a good solution anyway, cause it uses the `enum.name()` method which gives the value in uppercase (`ACTIF`), whereas I need the value in mixed case (`Actif`). Hence the converter. – M4veR1K Jul 08 '16 at 10:01
  • Could you also post the StatutUniteOperationnelle enum Java code, just for completion. It probably won't help much though - it seems Hibernate wants the database side to be a varchar, not a custom enum type if I understand the cryptic error correctly. – Gimby Jul 08 '16 at 10:10
  • @Gimby I edited the question. To me, it seems that hibernate doesn't want the String given by `return value.getType();` in the converter – M4veR1K Jul 08 '16 at 11:13
  • yes, I agree with that assessment. Seems like you'e not the only one having trouble mapping to a postgresql enum type: http://stackoverflow.com/questions/851758/java-enums-jpa-and-postgres-enums-how-do-i-make-them-work-together – Gimby Jul 08 '16 at 11:26
  • As there are no easy way to do it properly as of now, we forgot the idea of using enum in postgres, we are using tables instead. – M4veR1K Aug 10 '16 at 08:38

0 Answers0