0

With the reactive version of panache I am unable to select a specific column from table using the project

    @Entity
   class Test: PanacheEntity(){
     @Column(name="amount")
     var amount: Double = 0.0
    
     @Column(name="name")
     lateinit var name: String
   }

 @ApplicationScoped
   class TestRepository: PanacheRepository<Test> {
     fun getSum(name: String) =
        find("select sum(l.amount) as amount from Test l where l.name = :name",Paramater().with("name", name)
        .project(Result::class)
        .singleResult()
   }

  data class Result(val amount: Double)

For sum reason this is generating an incorrect SQL statement i.e.

SELECT new org.package.Result(amount) select sum(l.amount) as amount from org.package.Test l where l.name = $1

It never uses the projection. Is there another way to get the single value from the SQL which is not the entity being used? any workaround for this?

dondragon2
  • 535
  • 5
  • 15

1 Answers1

0

UPDATE: The issue has been fixed and included in Quarkus 2.12.CR1

I've reported the issue.

As a workaround, you can remove the .project(Result.class) and run the following query:

select new org.package.Result(sum(l.amount) as amount) from Test l where l.name = :name

The method will look like this:

   @ApplicationScoped
   class TestRepository: PanacheRepository<Test> {
     fun getSum(name: String) =
        find("select new org.package.Result(sum(l.amount) as amount) from Test l where l.name = :name",Paramater().with("name", name)
        .singleResult()
   }
Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30
  • That did not work. It generates this error -> `parse() - HQL: SELECT new org.package.Result () select new org.package.Result(sum(l.amount)) from org.package.Test l where l.name = :name group by l.name` `line 1:51: unexpected token: )` notice the double select statements – dondragon2 Jun 22 '22 at 02:15
  • You need to remove the ‘.project()’. Check my answer. I’m pretty sure it works because I’ve tested it – Davide D'Alto Jun 22 '22 at 07:33