[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] query a subpart of complex graph and return to a short object


Just use a TupleQuery which returns those 2 fields. Here is an example using the metamodel, but you could use replace customerRoot.get(Customer_name) with customerRoot.get("name"), although the metamodel is type-safe so use it if you can.

        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Tuple> criteriaQuery = criteriaBuilder.createTupleQuery();
        Root<Customer> customerRoot = criteriaQuery.from(Customer.class);

        Path<String> namePath = customerRoot.get(Customer_.name);
        Path<String> zipCodePath = customerRoot.get(ZipCode_.name);
        criteriaQuery.multiselect(namePath, zipCodePath);

        TypedQuery<Tuple> typedQuery = entityManager.createQuery(criteriaQuery);
        List<Tuple> tupleResultList = typedQuery.getResultList();

        List<Customer> basicCustomerList = new ArrayList<Customer>();

        for (Tuple tuple : tupleResultList) {
            String name = tuple.get(namePath);
            String zipCode = tuple.get(zipCodePath);

            Customer basicCustomer = new Customer();


Best regards,

Yannick Majoros

Le 7/12/2012 00:14, Julien Lamandé a écrit :

Hi everyone,
I need some advice about querying a complex graph of entities in order to return a simple object.

Assume I have a Customer which has a onetoone to a person entity which has too a onetoone to an address entity. Each of those entities have their own fields.
I need only 2 fields : the name of the customer and the zipcode of the address but not the other fields (I didn't mention that the Customer owns other relations to other types of entities)

My need is just for readonly.

The number of Customers is really huge and I must get those 2 informations really quick. I must avoid pre-caching but my question is more general ?

Is it possible to use something as projections, fetch groups or something else to get those 2 fields ? And how ? Could it be JPA2 compliant ?

I could use SQL as I see how to do this in sql but it would mean coupling a sql query to physical tables that are already defined in my mappings of entities. So will need to maintain the two if a change must occur on the tables.
I could create a view in db this would mean too to maintain the sql query behind the view.


eclipselink-users mailing list