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

protected TypedQuery<SearchResult> getCriteriaQuery(SearchType searchType,SearchCriteria searchCriteria){
	CriteriaBuilder cb=histDao.getEntityManager().getCriteriaBuilder();
	CriteriaQuery cq=cb.createQuery(RequestInfo.class);
//	Metamodel m=histDao.getEntityManager().getMetamodel();
//	EntityType<RequestInfo> RequestInfo_=m.entity(RequestInfo.class);
	Root<RequestInfo> from=cq.from(RequestInfo.class);
	EntityType<RequestInfo> RequestInfo_=from.getModel();
	Join<RequestInfo, BatchInfo> join1=from.join(RequestInfo_.getSingularAttribute("batchInfo", BatchInfo.class), JoinType.INNER);
	Join<RequestInfo, PortInfo> join2=from.join(RequestInfo_.getSet("portInfo", PortInfo.class), JoinType.LEFT);
	from.fetch(RequestInfo_.getSet("portInfo"), JoinType.LEFT);
	cq.select(from);
	cq.where(cb.equal(from.get(RequestInfo_.getSingularAttribute("requestId", String.class)),"CE.0010650337_D"));
	
	TypedQuery<SearchResult> typedQuery=histDao.getEntityManager().createQuery(cq);
	return typedQuery;
    }

你可能感兴趣的:(JOIN,jpa,query,fetch,Criteria)