Hi
On 21/06/13 13:48, nikosdim wrote:
Hi again

I have implemented exactly what you told me but the selections are not
working. I still get back all the columns.

Here is my code

import org.apache.cxf.jaxrs.ext.search.SearchContext;

@Context
SearchContext searchContext;
EntityManagerFactory emf = Persistence
                                .createEntityManagerFactory("default");
EntityManager em = emf.createEntityManager();
SearchCondition<Book> sc = new FiqlParser<Book>
Book.class).parse(searchContext.getSearchExpression());                 
JPACriteriaQueryVisitor<Book, Book> visitor =
                                    new JPACriteriaQueryVisitor<Book, Book>(em, 
Book.class, Book.class,
beanPropertiesMap);
sc.accept(visitor);
List<SingularAttribute&lt;Book, ?>> selections = new
ArrayList<SingularAttribute&lt;Book, ?>>();
selections.add(Book_.firstname);  //where firstname is just a string
TypedQuery query = visitor.getOrderedTypedQuery (selections, true);
List<Book> lbooks = query.getResultList();

I guess the example I gave you was the wrong one,

"visitor.getOrderedTypedQuery" is a wrapper around visitor.orderBy() which returns CriteriaQuery, this wrapper then gets a typed query from it. So in this case the selection attributes affect the ordering policy only.

Perhaps you need to use shaping functions, like using Tuple or construct or array (all are supported) example, with Tuple (probably the cheapest approach):

JPACriteriaQueryVisitor<Book, Tuple> jpa =
new JPACriteriaQueryVisitor<Book, Tuple>(em, Book.class, Tuple.class);
        filter.accept(jpa);

        List<SingularAttribute<Book, ?>> selections =
            new ArrayList<SingularAttribute<Book, ?>>();
        selections.add(Book_.id);

        jpa.selectTuple(selections);

        CriteriaQuery<Tuple> cquery = jpa.getQuery();
        return em.createQuery(cquery).getResultList();

This will return List<Tuple>, and then you'd work with individual Tuples.

If you prefer a partially populated Book returned immediately then use a construct approach. Your code will look the same as it is now except that instead of

> TypedQuery query = visitor.getOrderedTypedQuery (selections, true);

you will do

jpa.selectConstruct(selections);
CriteriaQuery<Tuple> cquery = jpa.getQuery();
return em.createQuery(cquery).getResultList();

Note, Book will need to have a constructor with the parameters matching the types of those in the current selection list,

If this does not work - please find out at JPA2 forums how to get "select columnname from some table" done in terms of JPA2 CriteriaQuery API and let me know - I will then add a utility function to JPACriteriaQueryVisitor

Let me know how it goes

Sergey

Any ideas?

Thanks



--
View this message in context: 
http://cxf.547215.n5.nabble.com/How-to-select-specific-columns-to-retrieve-with-FIQL-using-JPATypedQueryVisitor-tp5729548p5729649.html
Sent from the cxf-user mailing list archive at Nabble.com.



--
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com

Reply via email to