user1228739
user1228739

Reputation: 153

JPA Join criteria query with singular attribute

I have 2 entities like these:

EntityX:
id Long
name  String
entityYId  Long


EntityY (this entity is very hard, because it has a lot of data)
id Long
name String
xxx
xxx
.....

I need to do something like

Root<EntityX> xRoot = criteriaQuery.from(EntityX.class);
Join<EntityX, EntityY> yJoin = xRoot.join("entityYId", JoinType.LEFT);    

I need yJoin because I need with JPA criteria this multiselect:

pId = xRoot.get("id");   
pName = xRoot.get("name");
pEntityYName = yJoin.get("name");

cr.multiselect(
    cb.construct(
      ResultData.class,
      pId,
      pName,
      pEntityYName));

Can I do it? If it not possible, what can I do to solve this, REMEMBER! I need only "entityYId" in EntityX not all entityY element.

Thanks!

Upvotes: 1

Views: 7474

Answers (1)

cremersstijn
cremersstijn

Reputation: 2395

You can use the select method on Root class. See http://www.objectdb.com/java/jpa/query/jpql/select#SELECT_in_Criteria_Queries_ for more info

Upvotes: 1

Related Questions