Using JPA AttributeConverter for Y / N Boolean: "Unable to display the literal value of letters"

I am implementing a solution here to convert the column 'Y' / 'N' to a boolean:

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

The problem is that I cannot use booleans in JPQL. The following code gives the error below:

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

Mistake:

 java.lang.IllegalArgumentException: Validation failed for query for method public abstract xyzMyThing xyzMyThingRepository.findOneActive(xyzThingIdEnum)! ... 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 
+3
java jpa jpql
Sep 19 '16 at 20:06
source share
1 answer

It is turned off, because before the conversion this field is varchar / char, JPQL should be considered as a string. I'm not sure if there is a better way to do this, but the following worked:

 @Query("select thing from MyThing thing where thing.id = :id and thing.active = 'Y'") public MyThing findOneActive(@Param("id") ThingIdEnum id); 
+3
Sep 20 '16 at 15:29
source share



All Articles