@SuppressWarnings("unchecked") public <E, F> Path<F> getPath(Root<E> root, List<Attribute<?, ?>> attributes) { Path<?> path = root; for (Attribute<?, ?> attribute : attributes) { boolean found = false; if (path instanceof FetchParent) { for (Fetch<E, ?> fetch : ((FetchParent<?, E>) path).getFetches()) { if (attribute.getName().equals(fetch.getAttribute().getName()) && (fetch instanceof Join<?, ?>)) { path = (Join<E, ?>) fetch; found = true; break; } } } if (!found) { if ((attributes.indexOf(attribute) != (attributes.size() - 1)) && (attribute instanceof Bindable) && Identifiable.class.isAssignableFrom(((Bindable<?>) attribute).getBindableJavaType()) && (path instanceof From)) { path = ((From<?, ?>) path).join(attribute.getName(), JoinType.LEFT); } else { path = path.get(attribute.getName()); } } } return (Path<F>) path; }
@SuppressWarnings({ "unchecked", "rawtypes" }) public <E> void fetches(SearchParameters<E> sp, Root<E> root) { for (jpasearch.repository.query.Path<E, ?> path : sp.getFetches()) { FetchParent<?, ?> from = root; for (Attribute<?, ?> arg : metamodelUtil.toAttributes(root.getJavaType(), path.getPath())) { boolean found = false; for (Fetch<?, ?> fetch : from.getFetches()) { if (arg.equals(fetch.getAttribute())) { from = fetch; found = true; break; } } if (!found) { if (arg instanceof PluralAttribute) { from = from.fetch((PluralAttribute) arg, JoinType.LEFT); } else { from = from.fetch((SingularAttribute) arg, JoinType.LEFT); } } } } }
private Fetch<?, ?> getFetchedAssoc(final FetchParent<?, ?> parent, final JoinType joinType, final String propertyToFetch) { // Search within current fetches for (final Fetch<?, ?> fetch : parent.getFetches()) { if (fetch.getAttribute().getName().equals(propertyToFetch)) { return fetch; } } // Create a new one return parent.fetch(propertyToFetch, joinType); }
private void copyJoins(From<?, ?> from, From<?, ?> to) { for (Join<?, ?> join : from.getJoins()) { Join<?, ?> toJoin = to.join(join.getAttribute().getName(), join.getJoinType()); toJoin.alias(getAlias(join)); copyJoins(join, toJoin); } for (Fetch<?, ?> fetch : from.getFetches()) { Fetch<?, ?> toFetch = to.fetch(fetch.getAttribute().getName()); copyFetches(fetch, toFetch); } }
/** * Convert the passed propertyPath into a JPA path. <br> * Note: JPA will do joins if the property is in an associated entity. */ @SuppressWarnings("unchecked") private static <E> Path<?> getPropertyOrderPath(Root<E> root, String propertyPath, SearchParameters sp) { String[] pathItems = StringUtils.split(propertyPath, "."); Path<?> path = null; String pathItem = pathItems[0]; if (sp.getDistinct()) { // handle case when order on already fetched attribute for (Fetch<E, ?> fetch : root.getFetches()) { if (pathItem.equals(fetch.getAttribute().getName()) && fetch instanceof Join<?, ?>) { path = (Join<E, ?>) fetch; } } for (Join<E, ?> join : root.getJoins()) { if (pathItem.equals(join.getAttribute().getName()) && join instanceof Join<?, ?>) { path = (Join<E, ?>) join; } } } // if no fetch matches the required path item, load it from root if (path == null) { path = root.get(pathItem); } for (int i = 1; i < pathItems.length; i++) { path = path.get(pathItems[i]); } return path; }
private static boolean containsMultiRelationFetch(Set<?> fetches) { for (Object fetchObj : fetches) { Fetch<?, ?> fetch = (Fetch<?, ?>) fetchObj; Attribute<?, ?> attr = fetch.getAttribute(); if (attr.isAssociation() && attr.isCollection()) return true; if (containsMultiRelationFetch(fetch.getFetches())) return true; } return false; }
private static boolean containsMultiRelationJoin(Set<?> fetches) { for (Object fetchObj : fetches) { Fetch<?, ?> fetch = (Fetch<?, ?>) fetchObj; Attribute<?, ?> attr = fetch.getAttribute(); if (attr.isAssociation() && attr.isCollection()) return true; if (containsMultiRelationFetch(fetch.getFetches())) return true; } return false; }
/** * * @param name * @return */ public Project findByName(@Nonnull String name) { Project project = null; EntityManager em = getEntityManager(); try { begin(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Project> query = cb.createQuery(Project.class); Root<Project> root = query.from(Project.class); Fetch<Project, Workload> wl = root.fetch(Project.PROPERTY_WORKLOADS); wl.fetch("jobConfiguration"); query.select(root); query.where(cb.equal(root.<String>get(Project.PROPERTY_NAME), name)); project = em.createQuery(query).getSingleResult(); if( project != null) { project.getWorkloads().get(0).getJobConfiguration().readConfig(); project.getWorkloads().get(0).getJobConfiguration().getJobRegions(); project.getWorkloads().get(0).getJobConfiguration().getVariables(); project.getWorkloads().get(0).getJobConfiguration().getDataFileIds(); project.getWorkloads().get(0).getJobConfiguration().getNotifications(); for ( TestPlan tp : project.getWorkloads().get(0).getTestPlans() ) { for (ScriptGroup sg : tp.getScriptGroups() ) { sg.getScriptGroupSteps(); } } } commit(); } catch (Exception e) { rollback(); e.printStackTrace(); throw new RuntimeException(e); } finally { cleanup(); } return project; }
/** * Finds all Objects of type T_ENTITY * * @return the nonnull list of entities * @throws HibernateException * if there is an error in persistence */ @Nonnull public List<Project> findAll() throws HibernateException { List<Project> results = null; EntityManager em = getEntityManager(); try { begin(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Project> query = cb.createQuery(Project.class); Root<Project> root = query.from(Project.class); Fetch<Project, Workload> wl = root.fetch(Project.PROPERTY_WORKLOADS); wl.fetch("jobConfiguration"); query.select(root); results = em.createQuery(query).getResultList(); for (Project project : results) { project.getWorkloads().get(0).getJobConfiguration().getVariables(); project.getWorkloads().get(0).getJobConfiguration().getDataFileIds(); } commit(); } catch (Exception e) { rollback(); e.printStackTrace(); throw new RuntimeException(e); } finally { cleanup(); } return results; }
/** * Convert the passed propertyPath into a JPA path. * <p> * Note: JPA will do joins if the property is in an associated entity. */ @SuppressWarnings("unchecked") public static <E, F> Path<F> getPath(Root<E> root, List<Attribute<?, ?>> attributes) { Path<?> path = root; for (Attribute<?, ?> attribute : attributes) { boolean found = false; // handle case when order on already fetched attribute for (Fetch<E, ?> fetch : root.getFetches()) { if (attribute.getName().equals(fetch.getAttribute().getName()) && (fetch instanceof Join<?, ?>)) { path = (Join<E, ?>) fetch; found = true; break; } } for (Join<E, ?> join : root.getJoins()) { if (attribute.getName().equals(join.getAttribute().getName())) { path = join; found = true; break; } } if (!found) { path = path.get(attribute.getName()); } } return (Path<F>) path; }
private void bindFetches(FetchParent<?, ?> lhs, SqmNavigableReference lhsBinding, SqmFromElementSpace space) { if ( !SqmNavigableSourceReference.class.isInstance( lhsBinding ) ) { if ( !lhs.getFetches().isEmpty() ) { throw new ParsingException( "Attempt to bind fetches against a NavigableBinding that is not also a NavigableSourceBinding " ); } else { return; } } final SqmNavigableSourceReference sourceBinding = (SqmNavigableSourceReference) lhsBinding; for ( Fetch<?, ?> fetch : lhs.getFetches() ) { final JpaFetch<?,?> jpaFetch = (JpaFetch<?, ?>) fetch; final SqmAttributeReference attrBinding = (SqmAttributeReference) parsingContext.findOrCreateNavigableBinding( sourceBinding, fetch.getAttribute().getName() ); // todo : handle treats final SqmAttributeJoin sqmFetch = querySpecProcessingStateStack.getCurrent().getFromElementBuilder().buildAttributeJoin( attrBinding, interpretAlias( jpaFetch.getAlias() ), // todo : this is where treat would be applied null, convert( fetch.getJoinType() ), true, false ); space.addJoin( sqmFetch ); bindFetches( fetch, sqmFetch.getBinding(), space ); jpaPathResolutionMap.put( jpaFetch, sqmFetch.getBinding() ); } }
@Override public void addFetch(Fetch<X, ?> fetch) { if ( fetches == null ) { fetches = new LinkedHashSet<Fetch<X, ?>>(); } fetches.add( fetch ); }
@Override @SuppressWarnings({"unchecked"}) public Set<Fetch<X, ?>> getFetches() { return fetches == null ? Collections.EMPTY_SET : fetches; }
private void addFetches(Collection<String> fetches) { Map<String, Fetch> created = new HashMap<>(); for (String fetch : fetches) { Fetch parent = null; final String[] parts = StringUtils.split(fetch, '.'); for (int i = 0; i < parts.length; i++) { final String path = StringUtils.join(parts, '.', 0, i + 1); Fetch existing = created.get(path); if (existing == null) { if (parent == null) { // attribute of root existing = root.fetch(parts[i], JoinType.LEFT); } else { // attribute of parent existing = parent.fetch(parts[i], JoinType.LEFT); } created.put(path, existing); } parent = existing; } } }
public static Iterable<Fetch<?,?>> getAllFetches(FetchParent<?, ?> parent) { return flatMap(new Transformer<Fetch<?,?>,Iterable<Fetch<?,?>>>() { @Override public Iterable<Fetch<?, ?>> transform(Fetch<?, ?> source) { return cons(source, getAllFetches(source)); } }, parent.getFetches()); }
private static void copyFetches(FetchParent<?, ?> from, FetchParent<?, ?> to) { for (Fetch<?, ?> fetch : sort(fetchComparator, from.getFetches())) { Attribute<?, ?> attr = fetch.getAttribute(); @SuppressWarnings({ "rawtypes", "unchecked" }) Fetch<?, ?> f = attr instanceof SingularAttribute ? to.fetch((SingularAttribute) fetch.getAttribute(), fetch.getJoinType()) : attr instanceof CollectionAttribute ? to.fetch((CollectionAttribute) fetch.getAttribute(), fetch.getJoinType()) : attr instanceof SetAttribute ? to.fetch((SetAttribute) fetch.getAttribute(), fetch.getJoinType()) : attr instanceof ListAttribute ? to.fetch((ListAttribute) fetch.getAttribute(), fetch.getJoinType()) : attr instanceof MapAttribute ? to.fetch((MapAttribute) fetch.getAttribute(), fetch.getJoinType()) : to.fetch((CollectionAttribute) fetch.getAttribute(), fetch.getJoinType()); copyFetches(fetch, f); } }
/** * Copy Fetches * @param from source From * @param to destination From */ public static void copyFetches(From<?, ?> from, From<?, ?> to) { for (Fetch<?, ?> f : from.getFetches()) { Fetch<?, ?> toFetch = to.fetch(f.getAttribute().getName()); copyFetches(f, toFetch); } }
/** * Copy Fetches * @param from source Fetch * @param to dest Fetch */ public static void copyFetches(Fetch<?, ?> from, Fetch<?, ?> to) { for (Fetch<?, ?> f : from.getFetches()) { Fetch<?, ?> toFetch = to.fetch(f.getAttribute().getName()); // recursively copy fetches copyFetches(f, toFetch); } }
@Override public Set<Fetch<X, ?>> getFetches() { // TODO Auto-generated method stub return null; }
@Override public <Y> Fetch<X, Y> fetch(SingularAttribute<? super X, Y> attribute) { // TODO Auto-generated method stub return null; }
@Override public <Y> Fetch<X, Y> fetch(SingularAttribute<? super X, Y> attribute, JoinType jt) { // TODO Auto-generated method stub return null; }
@Override public <Y> Fetch<X, Y> fetch(PluralAttribute<? super X, ?, Y> attribute) { // TODO Auto-generated method stub return null; }
@Override public <Y> Fetch<X, Y> fetch(PluralAttribute<? super X, ?, Y> attribute, JoinType jt) { // TODO Auto-generated method stub return null; }
@Override public <X, Y> Fetch<X, Y> fetch(String attributeName) { // TODO Auto-generated method stub return null; }
@Override public <X, Y> Fetch<X, Y> fetch(String attributeName, JoinType jt) { // TODO Auto-generated method stub return null; }
private void copyFetches(Fetch<?, ?> from, Fetch<?, ?> to) { for (Fetch<?, ?> fetch : from.getFetches()) { Fetch<?, ?> toFetch = to.fetch(fetch.getAttribute().getName()); copyFetches(fetch, toFetch); } }
@Override public void addFetch(Fetch<X, ?> fetch) { throw new UnsupportedOperationException( "Cannot define fetch from a subquery correlation" ); }
@Override public <Y> Fetch<T, Y> fetch( SingularAttribute<? super T, Y> arg0 ) { // TODO Auto-generated method stub return null; }
@Override public <Y> Fetch<T, Y> fetch( PluralAttribute<? super T, ?, Y> arg0 ) { // TODO Auto-generated method stub return null; }
@Override public <X, Y> Fetch<X, Y> fetch( String arg0 ) { // TODO Auto-generated method stub return null; }
@Override public <Y> Fetch<T, Y> fetch( SingularAttribute<? super T, Y> arg0, JoinType arg1 ) { // TODO Auto-generated method stub return null; }
@Override public <Y> Fetch<T, Y> fetch( PluralAttribute<? super T, ?, Y> arg0, JoinType arg1 ) { // TODO Auto-generated method stub return null; }
@Override public <X, Y> Fetch<X, Y> fetch( String arg0, JoinType arg1 ) { // TODO Auto-generated method stub return null; }
@Override public Set<Fetch<T, ?>> getFetches() { // TODO Auto-generated method stub return null; }