Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对布尔Y/N字段使用JPA AttributeConverter:";无法呈现布尔文字值";_Java_Jpa_Jpql - Fatal编程技术网

Java 对布尔Y/N字段使用JPA AttributeConverter:";无法呈现布尔文字值";

Java 对布尔Y/N字段使用JPA AttributeConverter:";无法呈现布尔文字值";,java,jpa,jpql,Java,Jpa,Jpql,我正在实现将“Y”/“N”列转换为布尔值的解决方案: @Basic(optional = false) @Column(name = "ACTIVE_YN") @Convert(converter = BooleanToStringConverter.class) private Boolean active; 。。以及: @Converter public class BooleanToStringConverter implements AttributeConverter<Bool

我正在实现将“Y”/“N”列转换为布尔值的解决方案:

@Basic(optional = false)
@Column(name = "ACTIVE_YN")
@Convert(converter = BooleanToStringConverter.class)
private Boolean active;
。。以及:

@Converter
public class BooleanToStringConverter implements AttributeConverter<Boolean, String> {

    @Override
    public String convertToDatabaseColumn(Boolean value) {
        return (value != null && value) ? "Y" : "N";
    }

    @Override
    public Boolean convertToEntityAttribute(String value) {
        return "Y".equals(value);
    }
}
错误:

java.lang.IllegalArgumentException: Validation failed for query for method public abstract x.y.z.MyThing x.y.z.MyThingRepository.findOneActive(x.y.z.ThingIdEnum)!
...
Unable to render boolean literal value [select thing from MyThing thing where thing.id = :id and thing.active = true]
...
org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter cannot be cast to org.hibernate.type.LiteralType

事实证明,因为该字段在转换之前是varchar/char,所以JPQL需要将其视为字符串。我不确定是否有更好的方法来做到这一点,但以下方法奏效了:

@Query("select thing from MyThing thing where thing.id = :id and thing.active = 'Y'")
public MyThing findOneActive(@Param("id") ThingIdEnum id);
@Query("select thing from MyThing thing where thing.id = :id and thing.active = 'Y'")
public MyThing findOneActive(@Param("id") ThingIdEnum id);