Skip to content
Advertisement

Failed to convert from type [java.lang.Object[]] to type [@org.springframework.data.jpa.repository.Query com.data.models.Users]

I am trying to limit my query to only select specific columns from a table which am going to use, but when I write a simple select query I end up with an error

Resolved [org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object[]] to type [@org.springframework.data.jpa.repository.Query com.aims.covidsurvey.models.Users] for value ‘{44, test@gmail.com, John , Smith}’; nested exception is org.springframework.core.convert

When I select all, no errors but it picks some big columns that take the query take long to execute.

This is what I have done so far

my repository

JavaScript

My service

JavaScript

My model class

JavaScript

My controller

JavaScript

What am I doing wrong?

Thank you in advance.

Advertisement

Answer

The problem is in your repository. When you only select some columns, Spring Boot cannot convert them into your Users object.

There are 2 ways to fix this:

1. Change the return type to List<Object[]>.

JavaScript

Then, you have to do the mapping yourself.

2. Change the query

JavaScript

Just make sure that you have the appropriate constructor in your Users model. As I see, you already have it.

In my opinion, this is a more elegant approach.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement