Skip to content

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

Closed as not planned
@sergey-morenets

Description

@sergey-morenets

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions