openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From HaricotBean <b...@wilkins-family.org.uk>
Subject Re: OpenJPA1.2.2 - NativeQuery - IN clause
Date Tue, 01 Sep 2009 14:10:45 GMT

Thanks to all.

I did try the Collection<String> approach but OpenJPA generates an error and
complains about the underlying type (ArrayList, HashSet, etc.) being an
invalid type for th expression.  Not being familiar with the OpenJPA source
I took a quick look at fisheye for
org.apache.openjpa.persistence.query.ArrayExpression and it seems to suggest
that it expects an String[].  That failed to produce the expect results.

In the end I fell back to generating the sql string manually and abandoning
NamedQuery, e.g:

private static String mostRefersType = "select count(e.eid), e.* from Entity
e, Property p where e.eid=p.refersTo and p.type IN ( #### ) group by e.eid
order by count(e.eid) desc;";

public static List<EntityHolder> findMostUsed(Registry registry,
Collection<String> types, int limit) {
  ...
  sql = mostRefersType.replace("####", asString(types));
  q = registry.getEntityManager().createNativeQuery(sql, "MostUsedResults");
  list = (List<Object[]>) q.getResultList();
  ...
}

    private static String asString(Collection<String> types) {
        StringBuffer typesAsString = new StringBuffer();
        int i = 0;
        for (String type : types) {
            if ( typesAsString.length() > 0 ) typesAsString.append(",");
            typesAsString.append("'"+type+"'");
        }
        return typesAsString.toString();
    }

This approach works fine.  I have to assume that there is either a bug in
the Native Named Query processing or some requirement that isnt very well
documented.

Regards....



-- 
View this message in context: http://n2.nabble.com/OpenJPA1-2-2-NativeQuery-IN-clause-tp3556433p3560749.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Mime
View raw message