Search code examples
jpajpql

org.apache.openjpa.persistence.ArgumentException when doing in query openJPA


Suppose I had a list of ID associated with an entity stored in a list. If I pass this list into a function if I use the IN keyword in JPQL then I should get the same functionality as using in in reqular query language I am assuming...

But I am experiencing that exception whenever I attempt to run my code... My code looks like this...

public void UpdateEntityByPerson(int ID, int[] EntityIDs) {
    // TODO Auto-generated method stub
    List<EntityList> distList = null;//new ArrayList<EntityList>();
    try{        
        Provider prov = emf.find(Provider.class, new Long(ID));
        prov.setDistListPermCollection(null);
        distList = emf.createNamedQuery("getEntityListByListIds").setParameter("listIds", EntityIDs).getResultList();
        prov.setEntityListPermCollection(distList);         
    }
    catch(Exception ex){
        System.out.println("Exception : "+ex);
        ex.printStackTrace();
    }
}

Here is my JPQL named query...

@NamedQuery(name="getEntityistByListIds", query = "SELECT d FROM EntityList d WHERE d.listId in :listIds"),

What could I be doing wrong here? One thing that I thought of is that I have an array of integers... The EntityIDs are actually longs... that may be causing a slight brew-ha-ha!


Solution

  • When type of listId attribute in EntityList is long, then type of parameter to IN must be single Long or long value or List<Long>. So in your following kind of arguments are applicable:

    List<Long> EntityIDs = Arrays.asList(new Long[]{1L, 2L});
    Long EntityIDs = 0;
    long EntityIDs = 0;
    

    In specification this is spelled in more general form as follows:

    The literal and/or input parameter values must be like the same abstract schema type of the state_field_path_expression in type.

    Only the values of like types are permitted to be compared. A type is like another type if they correspond to the same Java language type, or if one is a primitive Java language type and the other is the wrappered Java class type equivalent (e.g., int and Integer are like types in this sense). There is one exception to this rule: it is valid to compare numeric values for which the rules of numeric promotion apply. Conditional expressions attempting to compare non-like type values are disallowed except for this numeric case.

    For me it looks that, according specification you could give also int parameter to IN when path points to long, but at least with Hibernate's implementation that is not the case.