I have a springboot project with mysql and jpa and in there I have a model/domain class
class MyClass {
private long id;
private String name;
private LocalDate date;
private String data1;
private Integer data2;
}
and I also have repository interface that extends JpaRepository where I want to add a custom query to pick rows with latest date by name - the code is as follows:
interface MyClassRepository extends JpaRepository<MyClass, Long> {
@Query(value = "select m.name, max(date) from MyClass m group by m.name")
List<String> selectLatest();
}
When the call of that query is executed I get the error message as in the title
No converter found capable of converting from type [java.time.LocalDate] to type [@org.springframework.data.jpa.repository.Query java.lang.String]
I have found similar questions but each was more specific to a class and more complex than mine, I think it's the java.util.LocalDate that's the problem here?
CodePudding user response:
You select two columns which is not a string. Your message returns an Object[]
:
interface MyClassRepository extends JpaRepository<MyClass, Long> {
@Query(value = "select m.name, max(date) from MyClass m group by m.name")
List<Object[]> selectLatest();
}
Where the index zero contains the name and the index 1 contains the Date.
But for me a better solution is to create an Object with two attributes and select this:
public class MyLatestClass {
private String name;
private LocalDate date;
public MyLatestClass(String name, LocalDate date){
this.name=name;
this.date=date;
}
//getter
....
}
interface MyClassRepository extends JpaRepository<MyClass, Long> {
@Query(value = "select new MyLatestClass(name, max(date)) from MyClass m group by m.name")
List<MyLatestClass> selectLatest();
}