Spring Data Jpa 无法创建具有两个嵌套关联的视图投影

pkbketx9  于 11个月前  发布在  Spring
关注(0)|答案(1)|浏览(137)

我有一个查询,由于多个关系需要很长时间。所以,我想创建一个视图来只获取必要的数据,并使用JOIN Fetch优化此查询。但是,JPA返回错误No aliases found in result tuple!请确保您的查询定义了aliases!
下面是我创建的视图的定义:

public interface BoatContractView {

  Integer getId();
  String getName();
  Boolean getMaintenance();
  Set<ServiceBoatView> getBoatServices();

 
  interface ServiceBoatView {
        Integer getId();

        List<ContractDocumentDto> getContracts();
  
        interface ContractDocumentDto {
            Integer getId();
            ContractType getContractType();
        }
  }
}

字符串
以及提供程序中的方法:

@Query("FROM Boat b JOIN FETCH b.boatServices bs WHERE b.marina.id = ?1 AND b.active = true ")
List<BoatContractView> findBoatContractViewByMarinaIdAndActiveTrue(Integer marinaId);

hujrc8aj

hujrc8aj1#

您看到的错误消息是因为您没有在查询中使用别名。您可以尝试以下查询:

@Query("SELECT b.id as id, b.name as name, b.maintenance as maintenance, bs.id as serviceId, cd.id as contractId, cd.contractType as contractType FROM Boat b JOIN b.boatServices bs JOIN bs.contracts cd WHERE b.marina.id = ?1 AND b.active = true ")
List<BoatContractView> findBoatContractViewByMarinaIdAndActiveTrue(Integer marinaId);

字符串
此查询使用别名将查询结果Map到视图中的字段。然后可以使用这些别名填充视图。

相关问题