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 ?