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

Specification using enum values does not follow @Enumerated(EnumType.STRING) and tries to convert it to smallint #3486

Closed
jluiz20 opened this issue May 22, 2024 · 2 comments
Labels
status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged

Comments

@jluiz20
Copy link

jluiz20 commented May 22, 2024

I have been migrating some services from Spring Boot 2.7.18 to Spring Boot 3.2.5 and after it, I am facing a specific issue with an enum column only using the Specification and I think it could be a bug (or I am doing something wrong).

I have an entity with the following field

    @Enumerated(EnumType.STRING)
    @Builder.Default
    @Column(nullable = false)
    var acceptanceStatus: PaymentAcceptanceStatus = PaymentAcceptanceStatus.RECEIVED,

As the annotation shows, I am storing it as a String in the DB (PostgreSQL) and that works perfectly.

image

It also works well when fetching the entity by id using normal repository methods.

But when I create a Specification filtering by the enum, like below.

image

I get an error ERROR: invalid input syntax for type smallint: "SENT"

In debugging further, it seems that it is trying to convert the value passed SENT to a smallint. This is the query generated (some other fields were truncated)

org.hibernate.exception.DataException: JDBC exception executing SQL [select prw1_0.id,prw1_0.acceptance_status,prw1_0.client_id from my_table prw1_0 where cast(prw1_0.acceptance_status as smallint)=? and cast(prw1_0.acceptance_status as smallint)=? order by prw1_0.received_at desc offset ? rows fetch first ? rows only] [ERROR: invalid input syntax for type smallint: "SENT"] [n/a]

going deeper it seems that in the SimpleJpaRepository when building the parameterBindingMap, it is defining the jdbcType as TyneIntJdbcType instead of some kind of varchar or string that I would expect based on the @Enumerated(EnumType.STRING) definition.

image

Now, I am not sure if I need to add some other annotation of metadata to the field, or if the problem is the Hibernate or in the data jpa. I came here as saving and fetching are working so I think the mapping of the field is correct in Hibernate and I only have this problem when trying to use the Specification.

I also forced the latest version 3.3.0 of the library and I still see the problem. Also, tried to force a column definition @Column(nullable = false, columnDefinition = "varchar(255)") but it didn't help.

Anything I can do? Any help is really appreciated! Thank you!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 22, 2024
@jluiz20 jluiz20 changed the title Specification using enum values does not follow @Enumerated(EnumType.STRING) and tries to convert it to smallint Specification using enum values does not follow @Enumerated(EnumType.STRING) and tries to convert it to smallint May 22, 2024
@quaff
Copy link
Contributor

quaff commented May 23, 2024

Could you provide your specifications code? It's better to provide a minimal reproducer project.

@mp911de mp911de added the status: waiting-for-feedback We need additional information before we can continue label May 23, 2024
@jluiz20
Copy link
Author

jluiz20 commented May 23, 2024

Thank you for your attention and I really sorry as I found out that the problem is on my side.

Turns out that we have a custom code that builds Specifications based on generic parameters and due to the Hibernate change it started breaking.

The previous code was creating an Expression using

 pathExpression = path.as(path.getJavaType());

The implementation was SingularAttributePath and it was mapped correctly,

On hibernate 6, the same logic was creating a SelfRenderingSqmFunction with the incorrect types.

image

Turns out that I only need the .as(path.getJavaType()) anymore and the new logic is

 pathExpression = path;

which maps the correct type

image

I honestly don't know how this magic works, but it is not working again.

Sorry for creating an issue that was a problem on my side.

@jluiz20 jluiz20 closed this as completed May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

4 participants