-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: JPA criteria join - how to access joined entity
PostPosted: Fri Jun 20, 2014 4:58 am 
Newbie

Joined: Fri Jun 20, 2014 2:45 am
Posts: 4
How can I access joined part of Query ?

http://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/querycriteria.html#querycriteria-from-join :

Quote:
CriteriaQuery<Person> personCriteria = builder.createQuery( Person.class );
Root<Person> personRoot = person.from( Person.class );
// Person.address is an embedded attribute
Join<Person,Address> personAddress = personRoot.join( Person_.address );
// Address.country is a ManyToOne
Join<Address,Country> addressCountry = personAddress.join( Address_.country );


In documentation there is no code showing how to access address or country part of query,
I am able only to get persons, which are multiplied by joined relations, but I cannot access address and country.


Top
 Profile  
 
 Post subject: Re: JPA criteria join - how to access joined entity
PostPosted: Fri Jun 20, 2014 6:08 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Hi,
I haven't tried this but It should be something like:
Code:
personCriteria.multiselect(addressCountry, personAddress);


or eventually you can select a particular field using:
Code:
personCriteria.multiselect(addressCountry.get(Country_.name), personAddress.get(Address_.mainAddress));


Top
 Profile  
 
 Post subject: Re: JPA criteria join - how to access joined entity
PostPosted: Fri Jun 20, 2014 6:12 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
By the way,
you should be able to find more example of the API in the JPA 2.1 specification, Chapter 6.4: http://download.oracle.com/otndocs/jcp/persistence-2_1-fr-eval-spec/index.html


Top
 Profile  
 
 Post subject: Re: JPA criteria join - how to access joined entity
PostPosted: Fri Jun 20, 2014 7:39 am 
Newbie

Joined: Fri Jun 20, 2014 2:45 am
Posts: 4
Thx for Your reply,

Multiselect is ok, but I cannot tie it with join,

below query, without join is ok,

@Test
public void testMultiselect() {
System.out.println("testMultiselect start");
CriteriaBuilder builder = entityManager.getCriteriaBuilder();

CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
Root<Order> orderRoot = criteria.from(Order.class);
Root<Item> itemRoot = criteria.from(Item.class);
criteria.where(builder.and(
builder.like(orderRoot.<String>get("customer"), "J%"),
builder.equal(orderRoot.get(Order_.id), itemRoot.get(Item_.order))));
criteria.multiselect(orderRoot, itemRoot);
TypedQuery<Tuple> typedQuery = entityManager.createQuery(criteria);

List<Tuple> result= typedQuery.getResultList();

for (Tuple tuple : result) {
System.out.println("---------");
Order order = (Order) tuple.get(0);
Item item = (Item) tuple.get(1);
System.out.println("tuple: " + " " + tuple );

System.out.println("Order: " + order.getCustomer());
System.out.println("Item: " + item.getProduct());

}

System.out.println("testMultiselect end " + result.size());
}


but If I try to add multiselect into it, I get additional cartesian product.

Possibly there is some possibility, to replace
Root<Item> itemRoot = criteria.from(Item.class);
with
Join<Order,Item> orderItems = orderRoot.join(Order_.items);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.