Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LocalDate or Enum parameters are wrong on JPASQLQuery #2147

Closed
Clandestino opened this issue May 12, 2017 · 2 comments
Closed

LocalDate or Enum parameters are wrong on JPASQLQuery #2147

Clandestino opened this issue May 12, 2017 · 2 comments
Labels

Comments

@Clandestino
Copy link

Clandestino commented May 12, 2017

Hello,
I have a complex JPASQLQuery where I have to pass some localdate (joda) and enum for comparison.
There is the interesting parts (dateLimit is org.joda.time.LocalDate):

.where(qTable1.date.loe(dateLimit))

when I look in the mysql general log this part of the query is transformed as follow :

where table1.Date <= '��\0sr\0org.joda.time.LocalDate�������\0J\0 iLocalMillisL\0 iChronologyt\0\ZLorg/joda/time/Chronology;xp\0\0[��x\0sr\0\'org.joda.time.chrono.ISOChronology$Stub��fq7P\'\0\0xpsr\0org.joda.time.DateTimeZone$Stub�/�|2\Z�\0\0xpw\0UTCxx'

Same issue with enum :

.and(qTable1.type.ne(Type.R))

in the mysql general log :

and table1.BType != '��\0~r\0#enums.Type\0\0\0\0\0\0\0\0\0\0xr\0java.lang.Enum\0\0\0\0\0\0\0\0\0\0xpt\0R'

How can I solve that ?

I'am using queryDSL 3.7.4 and hibernate : 5.1.4.Final

Thanks for your answers.

Mario


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@nickcaballero
Copy link

nickcaballero commented Oct 5, 2020

Seeing this issue as well in version 4.3.1 when passing an enum. The column in the JPA entity is annotated using @Enumerated(EnumType.STRING). From my initial debugging, it seems like com.querydsl.jpa.sql.AbstractJPASQLQuery#createQuery(boolean) delegates to com.querydsl.jpa.impl.JPAUtil#setConstants to set the parameters for the query.

No conversion takes place, instead the values are directly passed to javax.persistence.Query#setParameter(int, java.lang.Object). Since the query is a native SQL query, Hibernate doesn't know what to use as the bind type and chooses org.hibernate.type.SerializableType, which results in the serialized representation of the enum instance, as opposed to the name or ordinal.

The workaround is to use the Enum#name() instead. I'm using the following to work with the generics:

    public static <T extends Enum<T>> Expression<? super T> toNameConstant(T value) {
        return constant(value.name());
    }

...

   entity.status.eq(toNameConstant(Status.SUCCESS));

An alternative might be to register the exact type that the enum should be serialized as in Hibernate.

Ideally, QueryDsl would leverage Hibernate and the JPA annotations to convert the parameter to the correct type based on the entity type - this is already what is done in com.querydsl.jpa.NativeSQLSerializer to serialize the correct table/columns names.

Related issues:

@stale
Copy link

stale bot commented Jul 23, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jul 23, 2022
@stale stale bot closed this as completed Aug 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants