JPA将本机查询结果映射到非实体DTO



我有一个复杂的本机查询,我试图将其结果映射到非实体DTO类。我正在尝试将JPASqlResultSetMappingConstructorResult

一起使用

我的DTO类

@Data
public class Dto {
    private Long id;
    private String serial;
    private Long entry;
    private int numOfTasks;
}

我的实体类,它具有我称之为此本机查询结果的存储库接口。

@SqlResultSetMapping(
        name = "itemDetailsMapping",
        classes = {
                @ConstructorResult(
                        targetClass = Dto.class,
                        columns = {
                                @ColumnResult(name = "ID"),
                                @ColumnResult(name = "SERIAL"),
                                @ColumnResult(name = "ENTRY"),
                                @ColumnResult(name = "TASKS")
                        }
                )
        }
)
@NamedNativeQuery(name = "getItemDetails", query = "complex query is here", resultSetMapping = "itemDetailsMapping")
@Entity
@Data
public class Item {}

存储库

@Repository
public interface ItemRepository extends JpaRepository<Item, Long> {
    ...     
    List<Dto> getItemDetails();
}

当我从ItemRepository调用getItemDetails()时,我有以下错误:

org.springframework.data.mapping.propertyreferenceException:否 为类型项目找到的属性ItemDetails

使用SqlResultSetMappingConstructorResult的正确方法是什么并解决这个问题。

任何帮助将不胜感激。

要使用名为Queries命名查询的名称必须具有实体名称为前缀:

@NamedNativeQuery(name = "Item.getItemDetails", 
                 query = "complex query is here", resultSetMapping = "itemDetailsMapping")

然后,接口方法必须具有与没有前缀的命名查询相同的名称:

List<Dto> getItemDetails();

-

阅读有关春季数据JPA的更多信息,并在参考文献中命名查询https://docs.spring.io/spring-data/jpa/jpa/docs/current/current/referent/referent/reference/html/#jpa.query-methods.nemed-methods.named-quernods.nemed-quermesies

最新更新