PostgreSQL和JPA 2.1:枚举映射不起作用

时间:2016-07-08 09:16:56

标签: java spring hibernate postgresql jpa

我有一个带有PostgreSQL数据库的REST webservices的SpringBoot项目。

我的一个实体有一个枚举,它引用数据库中的枚举

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

为了进行映射,我遵循了使用JPA 2.1类型转换器的Hibernate文档here

我的枚举java方面:

@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;
    }
}

这是我进行映射的方式:我在我的实体UniteOperationnelle.java中有以下语句:

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

我的转换器StatutUniteOperationnelleConverter.java看起来像这样:

@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);
    }

}

我有一个显示UniteOperationnelle的服务,具体取决于状态,该状态必须为“活动”。

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

但在执行时,我收到以下错误:

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.

这是如此奇怪的原因,如果我在我的数据库中使用字符串作为参数执行给定的请求,它就像一个魅力......

关于这个问题的任何想法?

0 个答案:

没有答案