spring-projects/spring-data-redis

Failed to use Java records as projections in Spring Data Redis repository

sergey-morenets opened this issue · 2 comments

Hi

I have a domain class Book:

@Data
@RedisHash("books")
public class Book {
	
	private List<Translation> translations;
	
	@Id
	private int id;
	
	private String name;
	
	@Indexed
	private int year;
	
	@Indexed
	private int pages;
	
	@Indexed
	private double price;
	
	private Person author;
	
	@Version
	private Integer version;
}

and BookDTO as Java record:

public record BookDTO(String name) {}

Here's my repository:

public interface BookRepository 
		extends KeyValueRepository<Book, Integer> {
	
	List<BookDTO> findBy();
}

According to the latest Spring Data Redis documentation Java records are allowed as projections:

Java Records are ideal to define DTO types since they adhere to value semantics: All fields are private final and equals(…)/hashCode()/toString() methods are created automatically. Alternatively, you can use any class that defines the properties you want to project.

However when I execute findBy() method I get an error:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [model.Book] to type [projection.BookDTO]
	at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:294)
	at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:185)
	at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:165)

Spring Boot: 3.2.3
Java: 21

You have to use the latest spring boot milestone. DTO projections are in spring-data-redis:3.3.0-M2 and have not yet been released in a GA version.

Hi @christophstrobl

Thank you for the quick response. Why do I need to use latest Spring Boot milestone if DTO projects are declared as implemented in the latest stable version (3.2.4) ?