Query Spring DATA with specific condition

Momo :

I'm trying to get a the account having a list of contracts with a status 'CA' or 'CCR', I tried the query below and it works.

The problem is that i'd like to add a condition to my query, to get the accounts having the opposing contracts with different types.

Two opposing contracts are two contrats with different type. ( One contracts H and an other contract A)

The supported types are : Home Insurance & Auto Insurance ( H and A ).

@Query("select a from Account a full join a.contracts c join c.refStatusContract r where a.id=:id AND r.cdStatutContrat in ('CA','CCR')")
    Account getAccountHavingContractStatusActifOrInProcessOfTermination(@Param("id") long id);

My need :

I want to check if exists two contracts attached to the account on my database, where the two contracts are opposed, and contracts authorised status are 'CA' or 'CCR'.

MiguelDuque :

Not sure if I understood your problem correctly

So you want Accounts that:

  1. Have Contracts with status 'CA' or 'CCR'
  2. Have at least 2 Contracts, one with Type H and another with type A

Is this what you need?

@Query("select a 
        from Account a 
        full join a.contracts c 
        join c.refStatusContract r 
        where a.id=:id 
            AND r.cdStatutContrat in ('CA','CCR') 
            AND c.contractType in ('H', 'A')
        GROUP BY a
        having count(distinct c.contractType) > 1")
Account getAccountHavingContractStatusActifOrInProcessOfTermination(@Param("id") long id);

Is this what you need?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=19081&siteId=1