cxf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sergey Beryozkin <>
Subject Re: Correct way to use a JPA*QueryVisitor with a filter class that is not a JPA entity
Date Mon, 21 Oct 2013 09:18:39 GMT
On 21/10/13 07:21, Jim Talbut wrote:
> Hi,
> I have a JPA data model that I'm trying to use with CXF's SearchContext.
> In order to constrain the searching that can be done I'm trying to use a
> search class that is not a JPA entity (and that just contains a subset
> of the JPA fields).
> Q1. Is this the right way to solve this problem?
> Q2. How should I get it to work?
> Details:
> I have a "User" interface that the "User_JpaImpl" entity class derives
> from.
> I also have a "UserFilter" class that just has two properties (username
> & enabled) - the User interface also has getters and setters for these
> properties, but there is no relationship between UserFilter and User (or
> User_JpaImpl).
> My best attempt so far looks like this:
>          SearchCondition<UserFilter> sc =
> searchContext.getCondition(UserFilter.class);
>          JPACriteriaQueryVisitor<UserFilter, User_JpaImpl> visitor =
>              new JPACriteriaQueryVisitor<>(entityManager,
> UserFilter.class, User_JpaImpl.class);
>          searchCondition.accept(visitor);
>          TypedQuery<User_JpaImpl> query = visitor.getTypedQuery();
>          List<? extends User> result = query.getResultList();
> This throws an exception in accept because of this:
>          if (builder == null) {
>              builder = em.getCriteriaBuilder();
>              cq = builder.createQuery(queryClass);
>              root = cq.from(tClass);
>              predStack.push(new ArrayList<Predicate>());
>          }
> tClass is UserFilter.class, which is not known to JPA and thus cannot be
> used as root (cq.from(tClass) throws IllegalArgumentException).
> All I want to do is limit the set of fields that can be queried, if this
> is the wrong approach I'm happy to swap it for another.

I believe the JPA2 way is to use either JPA2 Tuple or capturing 
beans/arrays which will filter  the 'username' & 'enabled' properties at 
JPA/DB level, the JPA2 section has a couple of examples and 
JPATypedQueryVisitorTest has few more (to do with different styles of 
shaping the output), I'd probably go for using Tuple.

If you prefer to use a custom UserFilter then I guess you'd need to use
JPATypedQueryVisitor<User_JpaImpl> and then filter the result list with 
the filter afterwards

Thanks, Sergey

> Thanks.
> Jim

View raw message