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

Hibernate orm Panache query projection is not working #41657

Closed
ia3andy opened this issue Jul 3, 2024 · 11 comments · Fixed by #41359
Closed

Hibernate orm Panache query projection is not working #41657

ia3andy opened this issue Jul 3, 2024 · 11 comments · Fixed by #41359
Assignees
Labels
area/hibernate-orm Hibernate ORM area/panache area/persistence OBSOLETE, DO NOT USE kind/bug Something isn't working triage/upstream Used for issues which are caused by issues in upstream projects/dependency
Milestone

Comments

@ia3andy
Copy link
Contributor

ia3andy commented Jul 3, 2024

Describe the bug

As described here https://quarkus.io/guides/hibernate-orm-panache#query-projection, it should be possible to project a query in a class.

@Entity
public class Score extends PanacheEntity {
    public String name;
    public int points;

    public Score() {
    }

    public Score(String name, int points) {
        this.name = name;
        this.points = points;
    }

    public static List<Score> total() {
        return Score.find("select s.name, sum(s.points) from Score s group by s.name").project(Score.class).list();
    }
}

Expected behavior

This should project correctly

Actual behavior

It currently fail with this exception:

024-07-03 10:06:09,967 ERROR [io.qua.ver.htt.run.QuarkusErrorHandler] (executor-thread-1) HTTP Request to /score failed, error id: acd0a377-6d5a-4073-b8df-43cddb5726c8-1: org.hibernate.query.QueryTypeMismatchException: Result type is 'Score' but the query returned a 'String'
	at org.hibernate.sql.results.internal.RowTransformerCheckingImpl.transformRow(RowTransformerCheckingImpl.java:33)
	at org.hibernate.sql.results.internal.StandardRowReader.readRow(StandardRowReader.java:102)
	at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:205)
	at org.hibernate.sql.results.spi.ListResultsConsumer.consume(ListResultsConsumer.java:33)

How to Reproduce?

https://github.com/ia3andy/reproducer-hibernate-project

Run the test

Quarkus version or git rev

3.12.0

@ia3andy ia3andy added the kind/bug Something isn't working label Jul 3, 2024
@quarkus-bot quarkus-bot bot added area/hibernate-orm Hibernate ORM area/panache area/persistence OBSOLETE, DO NOT USE labels Jul 3, 2024
@quarkus-bot
Copy link

quarkus-bot bot commented Jul 3, 2024

/cc @FroMage (panache), @gsmet (hibernate-orm), @loicmathieu (panache), @yrodiere (hibernate-orm)

@FroMage
Copy link
Member

FroMage commented Jul 3, 2024

Probably an ORM bug related to group by?

@ia3andy
Copy link
Contributor Author

ia3andy commented Jul 3, 2024

I have found a workaround:

@Entity
public class Score extends PanacheEntity {
    public String name;
    public Long points;

    public Score() {
    }

    public Score(String name, Long points) {
        this.name = name;
        this.points = points;
    }

    public static List<Score> total() {
        return Score.find("select s.name, SUM(s.points) from Score s group by s.name").project(Score.class).list();
    }
}

so it seems using int or Integer won't work

@FroMage
Copy link
Member

FroMage commented Jul 3, 2024

Looks like an ORM bug in int/long conversion? At the very least the error should be better. WDYT @yrodiere should we file it upstream? Probably needs a pure-ORM reproducer…

@yrodiere
Copy link
Member

yrodiere commented Jul 4, 2024

WDYT @yrodiere should we file it upstream? Probably needs a pure-ORM reproducer…

+1 please create a reproducer based on https://github.com/hibernate/hibernate-test-case-templates/blob/main/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/QuarkusLikeORMUnitTestCase.java

@yrodiere yrodiere added the triage/upstream Used for issues which are caused by issues in upstream projects/dependency label Jul 4, 2024
@DrongoX
Copy link

DrongoX commented Jul 15, 2024

Hi everyone,
Could someone please link the corresponding issue on the Hibernate side if it was created ?

On a sidenote, I have this problem reproducing with Quarkus 3.12.0 and it is working fine on 3.11.2. I checked quickly, these versions seem to use the exact same Hibernate version.

@FroMage
Copy link
Member

FroMage commented Jul 15, 2024

I am not sure it was filed upstream yet.

@yrodiere yrodiere self-assigned this Jul 16, 2024
@yrodiere
Copy link
Member

yrodiere commented Jul 16, 2024

Tried to reproduce upstream, there's a problem with sum(point) being of type Long, but nothing like the error in the issue description.

Going to try to debug the reproducer...

@yrodiere
Copy link
Member

yrodiere commented Jul 16, 2024

Tried to reproduce upstream, there's a problem with sum(point) being of type Long, but nothing like the error in the issue description.

Forget that, a bug in the chair-keyboard interface. I do reproduce the problem with just Hibernate ORM.

BUT... it's already been reported and fixed, in 6.6.0.CR1: https://hibernate.atlassian.net/browse/HHH-18214

So, upgrading to Hibernate ORM 6.6 in #41359 will fix the problem.

In the meantime @ia3andy: HHH-18214 just caused the reported error to be nonsense, but there was a problem in your code. You can fix it by using Long as a parameter in your Score constructor instead of int. The reason you need that is that sum(points) has type Long, probably to avoid int overflows. As to why Long and not long, I can't say.

@yrodiere yrodiere linked a pull request Jul 16, 2024 that will close this issue
@lfarkas
Copy link

lfarkas commented Aug 5, 2024

when do you plan to upgrade to Hibernate ORM 6.6?

@aaronz-vipaso
Copy link

when do you plan to upgrade to Hibernate ORM 6.6?

@lfarkas I think the Hibernate ORM 6.6.0 version is not yet released.

@quarkus-bot quarkus-bot bot added this to the 3.14 - main milestone Aug 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/hibernate-orm Hibernate ORM area/panache area/persistence OBSOLETE, DO NOT USE kind/bug Something isn't working triage/upstream Used for issues which are caused by issues in upstream projects/dependency
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants