Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA 2.0 Criteria, fetching whole entity graph

 
Darek Osiennik
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

I have a entity class SysDic (application dictionary) with parent and childrens fields with annotation(Fetch = FetchType.Lazy). In my Ejb client bean I have a method dicGetFull. I would like to get full dictionary(whole graph) by using Criteria API so I have written following code:



But it is not working, only one level is fetched but I'm would like to fetch whole graph. It is possible using Criteria? How I can do it when my fields in entity definition are lazy?

Thanks for help
 
Darek Osiennik
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody knows? It is important for me.
 
Darek Osiennik
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public SysDic getRoot() throws Throwable {

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<SysDic> q = cb.createQuery(SysDic.class);
Root<SysDic> dic = q.from(SysDic.class);
q.select(dic).where(cb.equal(dic.get("kind"), SysConst.c_str_DIC_KIND_ROOT));

TypedQuery<SysDic> tq = entityManager.createQuery(q);
return tq.getSingleResult();
}

public SysDic getFull() throws Throwable {

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<SysDic> q = cb.createQuery(SysDic.class);
Root<SysDic> dic = q.from(SysDic.class);
q.distinct(true);
dic.fetch("childrens", JoinType.LEFT);

TypedQuery<SysDic> tq = entityManager.createQuery(q);
tq.getResultList();

SysDic root = getRoot();
return root;
}

Greetings , Darek
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic