使用join fetch with JPA2 CriteriaQuery的简单例子

  1. protected TypedQuery<SearchResult> getCriteriaQuery(SearchType searchType,SearchCriteria searchCriteria){  
  2.     CriteriaBuilder cb=histDao.getEntityManager().getCriteriaBuilder();  
  3.     CriteriaQuery cq=cb.createQuery(RequestInfo.class);  
  4. //  Metamodel m=histDao.getEntityManager().getMetamodel();  
  5. //  EntityType<RequestInfo> RequestInfo_=m.entity(RequestInfo.class);  
  6.     Root<RequestInfo> from=cq.from(RequestInfo.class);  
  7.     EntityType<RequestInfo> RequestInfo_=from.getModel();  
  8.     Join<RequestInfo, BatchInfo> join1=from.join(RequestInfo_.getSingularAttribute("batchInfo", BatchInfo.class), JoinType.INNER);  
  9.     Join<RequestInfo, PortInfo> join2=from.join(RequestInfo_.getSet("portInfo", PortInfo.class), JoinType.LEFT);  
  10.     from.fetch(RequestInfo_.getSet("portInfo"), JoinType.LEFT);  
  11.     cq.select(from);  
  12.     cq.where(cb.equal(from.get(RequestInfo_.getSingularAttribute("requestId", String.class)),"CE.0010650337_D"));  
  13.       
  14.     TypedQuery<SearchResult> typedQuery=histDao.getEntityManager().createQuery(cq);  
  15.     return typedQuery;  
  16.     } 
阅读更多
换一批

没有更多推荐了,返回首页