eclipse-ee4j / eclipselink

Eclipselink project
https://eclipse.dev/eclipselink/
Other
196 stars 167 forks source link

CriteriaQuery API javax.persistence.criteria.Expression#as doest not work #829

Open rieonke opened 4 years ago

rieonke commented 4 years ago

When I try to cast a Long column to String in CriteriaQuery,

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Long> cq = cb.createQuery(Long.class);
Root<UserEntity> from = cq.from(UserEntity.class);

cq.select(cb.count(from)).where(cb.like(from.get("id").as(String.class), "1%")); //id starts with 1

TypedQuery<Long> pageQuery = em.createQuery(cq);
Long count = pageQuery.getSingleResult();

The UserEntity's id is an Long column. SQL generated by Eclipselink 2.7.7 will cause an exception:

SELECT COUNT(ID) FROM fpm_sys_user WHERE ID LIKE ?

Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint ~~ character varying,
No operator matches the given name and argument types. You might need to add explicit type casts.
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2533)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2268)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:313)
    at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:448)
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:369)
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:159)
    at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:109)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeSelect(DatabaseAccessor.java:1015)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:651)
    ... 23 more

But hibernate works fine, SQL generated by Hibernate:

select count(userentity0_.id) as col_0_0_ from fpm_sys_user userentity0_ where cast(userentity0_.id as varchar(255)) like ?

Is it a bug of eclipselink?

rieonke commented 4 years ago

I found a similar bug of hibernate HHH-5755: javax.persistence.criteria.Expression.as() is broken