https://kotlinlang.org logo
#spring
Title
# spring
o

Okan Yıldırım

05/17/2022, 10:50 AM
Hello friends, may anybody help me about custom Query using JpaRepository ? I do use this query:
Copy code
@Query("select id, email from User")
fun getAllIdsAndEmails(): List<Pair<Long, String>>
But sadly I get an error of
No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [kotlin.Pair<?, ?>]
Also tried
List<Tuple>
, still did not work
But the return type is like Pair this is example return [ [ 0, “x@gmail.com” ], [ 1, “a@mail.com” ] ]
g

Gabriel Pomin

05/17/2022, 11:32 AM
Have you tried
List<User>
?
o

Okan Yıldırım

05/17/2022, 3:29 PM
let me do it
but I am not selecting all required fields of user
Yeah it gave error
g

Gabriel Pomin

05/17/2022, 3:42 PM
Have you tried something like this, using some list implementation:
fun getAllIdsAndEmails(): List<Long, String>
? Or, you could use something like they show here: https://faun.pub/select-specific-columns-from-a-database-table-using-spring-data-jpa-d4eb0a24a2c4 That it is similar to what I see in a Stack overflow thread Thats looks like the right path I think
o

Okan Yıldırım

05/17/2022, 5:32 PM
Yes this did not work
p

Pavel

05/17/2022, 7:07 PM
I am not sure how to convert to a
Pair
but you can try this interface:
Copy code
interface UserData {
    val id: Long 
    val email: String?        
}
and then you will have
fun getAllIdsAndEmails(): List<UserData>
2
o

Okan Yıldırım

05/17/2022, 9:37 PM
Ah yeah, that worked
Thanks
2520 Views