| Re: [eclipselink-users] query a subpart of complex graph and return to a short object |
|
Hi,
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(); basicCustomer.setName(name); basicCustomer.setZipCode(zipCode); basicCustomerList.add(basicCustomer); } Best regards, Yannick Majoros Le 7/12/2012 00:14, Julien Lamandé a écrit :
|