我正在实施解决方案here https://stackoverflow.com/a/22368268/26535将“Y”/“N”列转换为布尔值:
@Basic(optional = false)
@Column(name = "ACTIVE_YN")
@Convert(converter = BooleanToStringConverter.class)
private Boolean active;
.. and:
@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);
}
}
问题是我似乎无法在 JPQL 中使用布尔值。下面的代码给出了以下错误:
@Query("select thing from MyThing thing where thing.id = :id and thing.active = true")
public MyThing findOneActive(@Param("id") ThingIdEnum id);
Error:
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)