查询id返回类型

时间:2017-10-20 09:15:22

标签: java sql spring-data

我使用弹簧数据。我想从DB获得一些合同。所以我创建了两个查询。首先,我得到我需要的合同ID,第二个我得到这个id的合同。

Repository.class中的第一个查询

@Query(nativeQuery = true, value =
        "select id from (" +
            "select contract.id, max(invoice.period_to) " +
            "from public.invoice " +
            "join public.contract on contract.id = invoice.contract_id " +
            "where invoice.period_to <= '2017-10-20' " +
            "AND contract.close_type IS NULL " +
            "AND contract.payment_type != 'TRIAL' " +
            "group by contract.id" +
            ") foo  ")
    List<Long> findContractsIdForInvoicesCreation();

ServiceJPA.class

List<Long> contractsId = ContractRepository.findContractsIdForInvoicesCreation();
List<Contract> contracts = contractRepository.findAll(contractsId);

但是在上面的最后一行我有一个错误。

java.lang.IllegalArgumentException: Parameter value element [2] did not match expected type [java.lang.Long (n/a)]

如果我只是创建

    List<Long> contractsIdL = new ArrayList<>();
        contractsIdL.add(2L);
        contractsIdL.add(3L);
        contractsIdL.add(4L);
    List<Contract> contracts = contractRepository.findAll(contractsId);

一切正常。不知道出了什么问题。在wich类型中首先查询返回id?

P.S。 DB中的id类型是bigint

p.p.s我用System.out.println查询了第一个查询 - 它似乎返回了核心编号。

1 个答案:

答案 0 :(得分:1)

我认为问题出在ContractRepository.findContractsIdForInvoicesCreation();

我认为它不会像您预期的那样返回List<Long>

相关问题