|
|
|
SelectCaseTest {code:java} 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; }
} {code}
with Derby fails with error
{code:java} Caused by: 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') {code}
while with PostgreSQL the error is
{code:java} 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. {code}
the test {code:java} selectCaseWithCastedTypeValuesShouldWork {code}
adds an explicit cast {code:java} query.select( from ).where( cb.equal( from.get( "value" ), selectCase.as( String.class ) ) ); {code}
and it works.
|
|
|
|