spring 未找到能够从类型[query.AbstractJpaQuery$TupleConverter$TupleBackedMap]转换为类型[com.name.dto.TransferTest]的转换器

klh5stk1  于 2023-03-07  发布在  Spring
关注(0)|答案(1)|浏览(169)

目前,我在我的Repository中有一个查询,从数据库返回2列。

@Repository
public interface HomeTransferPageRepository extends JpaRepository<TransferEntity, Integer> {
    
    @Query(value="select DISTINCT transfer_to, username from (select transfer_to, transaction_date, username from transfers_table left join registration_entity on transfers_table.transfer_to = registration_entity.id where transfer_from= :transfer_from ORDER BY transaction_date DESC LIMIT 10 )  as x", nativeQuery=true)
    public ArrayList<TransferTest> getTransferRequests(Integer transfer_from);

}

现在TransferTest这里是我的dto:

@Data
public class TransferTest {

    private Integer transfer_to;
    private String username;
}

这是我的DAO

@Autowired
    private HomeTransferPageRepository homeTransferPageRepository;
    
    public ArrayList<TransferTest> getSentTransfers(TransferEntity entity) {
        return homeTransferPageRepository.getTransferRequests(entity.getId());
    }

这是我的服务类:

@Autowired
    private SendMoneyDAOImpl daoImpl;
    private ModelMapper modelMapper = new ModelMapper();
    
    @Override
    public ArrayList<TransferTest> getSentTransfers(TransferRequest request) {
        TransferEntity entity = modelMapper.map(request, TransferEntity.class);
        ArrayList<TransferTest> getTransfers = daoImpl.getSentTransfers(entity);
        return getTransfers;
    }

这是我的transferEntity:

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "transfers_table")
public class TransferEntity {
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer Id;
    private Integer transfer_from;
    private Integer transfer_to;
    private Double transaction_amount;
    private Date transaction_date;
    
}

当我运行它的时候,它会给我以下的错误:
未找到能够从类型[org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap]转换为类型[com.name.dto.TransferTest]的转换器

bxgwgixi

bxgwgixi1#

对于本机查询,您必须使用基于接口的投影:
对于基于界面的投影,将TransferTest修改为:

public interface TransferTest {

    Integer getTransfer_to();
    String getUsername();
}

并确保在SQL查询中使用投影:

@Repository
public interface HomeTransferPageRepository extends JpaRepository<TransferEntity, Integer> {
    
        @Query(value="select DISTINCT transfer_to as transfer_to, username as username from (select transfer_to, transaction_date, username from transfers_table left join registration_entity on transfers_table.transfer_to = registration_entity.id where transfer_from= :transfer_from ORDER BY transaction_date DESC LIMIT 10 )  as x", nativeQuery=true)
        public ArrayList<TransferTest> getTransferRequests(Integer transfer_from);
    
    }

相关问题