Skip to content
Advertisement

Query id return type

I use spring data. I want to get some contracts from DB. So I create two queries. In first I get contracts id that I need, in second I get contract by this id.

First query in 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);

But in last line above I have an error.

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

If I simply create

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

All works fine. Cant figure out what is wrong. In which type do first query return id?

p.s. id type in DB is bigint

p.p.s. I checked first query with System.out.println – it seems to return correct numbers.

Advertisement

Answer

I think the issue is with ContractRepository.findContractsIdForInvoicesCreation();

I think it doesn’t return List<Long> as you expected

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement