目前,我在我的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]的转换器
1条答案
按热度按时间bxgwgixi1#
对于本机查询,您必须使用基于接口的投影:
对于基于界面的投影,将
TransferTest
修改为:并确保在SQL查询中使用投影: