This discussion is archived
5 Replies Latest reply: Aug 15, 2012 1:01 AM by gimbal2 RSS

JPQL -Collection Member Expressions -type mismatch

Amasoni Newbie
Currently Being Moderated
I'm trying to run the query below but I get an error
Caused by: java.lang.IllegalArgumentException: You have attempted to set
a value of type class java.lang.String for parameter contid with expected type
of class mmis.entity.Contract from
query string SELECT c FROM Consultant c WHERE c.contractCollection IS EMPTY
OR :contid NOT MEMBER OF c.contractCollection .

public List<Consultant> getAvailableConsultants( String cntrctId) {

return em.createQuery("SELECT c FROM Consultant c"
+ " WHERE c.contractCollection IS EMPTY OR :contid NOT MEMBER OF c.contractCollection ")
.setParameter("contid", cntrctId)
.getResultList();
}

The collection is a result of a many-to-many relationship which i thought holds the contractIDs
how can i get the query to check whether the parameter contid is not in the collection
Thnx
  • 1. Re: JPQL -Collection Member Expressions -type mismatch
    gimbal2 Guru
    Currently Being Moderated
    contractCollection likely does not contain an integer but some form of Contract object. The JPQL parameter will then be an actual Contract object - the one with the key you are trying to pass now.

    Remember: this is JPQL, not SQL. There is no database as far as your code is concerned.
  • 2. Re: JPQL -Collection Member Expressions -type mismatch
    Amasoni Newbie
    Currently Being Moderated
    Yes, am just getting used to the fact thast this isn't SQL but PSQL. This is what contractCollection looks like .

    @ManyToMany
    private Collection<Contract> contractCollection;

    How can i cnvert/cast my parameter in order to check whether it exists n the collection plz
  • 3. Re: JPQL -Collection Member Expressions -type mismatch
    848602 Newbie
    Currently Being Moderated
    Hi Amasoni, I think you might want to review the JPA persistence basics: http://docs.oracle.com/javaee/6/tutorial/doc/bnbtl.html (or probably Java basics). As pointed out by qimbal2, c.contractCollection refers to a collection of Contract object, hence you won't be able to simply cast it to get its id.

    Instead if you want to find out Consultants with no contracts with exceptions of some contract ids, you can use joins as such:
    select distinct c
    from Consultant c 
    left outer join c.contractCollection o
    where o is null
    or (o is not null and o <> 3)
  • 4. Re: JPQL -Collection Member Expressions -type mismatch
    Amasoni Newbie
    Currently Being Moderated
    I sorted out my problem by adding the find() below. Thnx for the responses.

    public List<Consultant> getAvailableConsultants( String contractId) {
    Contract contract = em.find(Contract.class, contractId);
    return em.createQuery("SELECT c FROM Consultant c"
    + " WHERE c.contractCollection IS EMPTY OR :cntrct NOT MEMBER OF c.contractCollection ")
    .setParameter("cntrct", contract)
    .getResultList();
    }
  • 5. Re: JPQL -Collection Member Expressions -type mismatch
    gimbal2 Guru
    Currently Being Moderated
    Gee, I don't spoon-feed the answer like you requested and you then figure it out yourself. Good on you mate!

    You'll find in the future that you'll likely write your code such that you don't need to fetch the contract anymore because you already have it and can pass it to getAvailableConsultants(). Its all about proper code design.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points