Apache Derby and PostgreSQL - Criteria selectCase with Enum fails without explicit cast
Description
Activity
Show:
Andrea Boriero May 10, 2016 at 7:56 PM
Fixed
Details
Details
Assignee
Andrea Boriero
Andrea BorieroReporter
Andrea Boriero
Andrea BorieroComponents
Fix versions
Priority
Created September 28, 2015 at 5:04 PM
Updated May 2, 2017 at 9:05 PM
Resolved June 1, 2016 at 5:17 AM
SelectCaseTest
public class SelectCaseTest extends BaseEntityManagerFunctionalTestCase{ .... @Test public void selectCaseWithValuesShouldWork() { EntityManager entityManager = getOrCreateEntityManager(); CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaBuilder.Case<EnumValue> selectCase = cb.selectCase(); Predicate somePredicate = cb.equal( cb.literal( 1 ), 1 ); selectCase.when( somePredicate, EnumValue.VALUE_1 ); selectCase.otherwise( EnumValue.VALUE_2 ); CriteriaQuery<Entity> query = cb.createQuery( Entity.class ); Root<Entity> from = query.from( Entity.class ); query.select( from ).where( cb.equal( from.get( "value" ), selectCase ) ); entityManager.createQuery( query ).getResultList(); } public static class Entity { @Id private Long id; @Enumerated(EnumType.STRING) private EnumValue value; } public enum EnumValue { VALUE_1, VALUE_2; } }
with Derby fails with error
java.sql.SQLSyntaxErrorException: Comparisons between 'VARCHAR (UCS_BASIC)' and 'VARCHAR () FOR BIT DATA' are not supported. Types must be comparable. String types must also have matching collation. If collation does not match, a possible solution is to cast operands to force them to the default collation (e.g. SELECT tablename FROM sys.systables WHERE CAST(tablename AS VARCHAR(128)) = 'T1')
while with PostgreSQL the error is
org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying = bytea Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
the test
selectCaseWithCastedTypeValuesShouldWork
adds an explicit cast
query.select( from ).where( cb.equal( from.get( "value" ), selectCase.as( String.class ) ) );
and it works.