在查询

时间:2016-11-10 13:30:10

标签: java hibernate spring-boot enums

我有一个带有一组枚举的Entity类:

@Entity
public class Something {

    public enum Type{
        FIRST_ENUM((short)1),
        SECOND_ENUM((short)2);

        private short id;

        private Type(short id) {
            this.id = id;
        }

        public short getId() {
            return id;
        }
    }

    @CollectionTable(name="table_name", joinColumns=@JoinColumn(name="something_id"))
    @Column(name="type")
    private Set<Type> types;

    ... //other props + getters and setters
}

对于enum,我制作了一个转换器(整个转换器包由@EntityScan注释加载):

@Converter(autoApply=true)
public class TypeConverter implements AttributeConverter<Type, Integer> {

    @Override
    public Integer convertToDatabaseColumn(Type st) {
        //implementation
    }

    @Override
    public Type convertToEntityAttribute(Integer i) {
        //implementation
    }
}

现在我尝试在查询中使用枚举

... AND {packagename}.Something$Type.FIRST_ENUM MEMBER OF {someobject}.something.types ...

我偶然发现了以下错误:

org.hibernate.QueryException: Unrecognized Hibernate Type for handling query constant ({package}.Something$Type.FIRST_ENUM); expecting LiteralType implementation or AttributeConverter

有没有人知道为什么我不能在查询中使用枚举?它似乎不知道Hibernate不知道枚举。我不明白为什么,因为在我开始申请时课程已加载。

0 个答案:

没有答案