下面列出了怎么用javax.persistence.criteria.Fetch的API类实例代码及写法,或者点击链接到github查看源代码。
@SuppressWarnings({ "unchecked" })
private void renderFetches(
StringBuilder jpaqlQuery,
RenderingContext renderingContext,
Collection<Fetch> fetches) {
if ( fetches == null ) {
return;
}
for ( Fetch fetch : fetches ) {
( (FromImplementor) fetch ).prepareAlias( renderingContext );
jpaqlQuery.append( renderJoinType( fetch.getJoinType() ) )
.append( "fetch " )
.append( ( (FromImplementor) fetch ).renderTableExpression( renderingContext ) );
renderFetches( jpaqlQuery, renderingContext, fetch.getFetches() );
}
}
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;
}
@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;
}
@Override
@SuppressWarnings({"unchecked"})
public <X, Y> Fetch<X, Y> fetch(String attributeName, JoinType jt) {
if ( !canBeFetchSource() ) {
throw illegalFetch();
}
Attribute<X, ?> attribute = (Attribute<X, ?>) locateAttribute( attributeName );
if ( attribute.isCollection() ) {
return (Fetch<X, Y>) fetch( (PluralAttribute) attribute, jt );
}
else {
return (Fetch<X, Y>) fetch( (SingularAttribute) attribute, jt );
}
}
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;
}
/**
* 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;
}
/**
* 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 void addFetch(Fetch<X, ?> fetch) {
throw new UnsupportedOperationException( "Cannot define fetch from a subquery correlation" );
}
@Override
public <Y> Fetch<X, Y> fetch(SingularAttribute<? super X, Y> singularAttribute) {
return fetch( singularAttribute, DEFAULT_JOIN_TYPE );
}
@Override
public <Y> Fetch<X, Y> fetch(PluralAttribute<? super X, ?, Y> pluralAttribute) {
return fetch( pluralAttribute, DEFAULT_JOIN_TYPE );
}
@Override
public <X, Y> Fetch<X, Y> fetch(String attributeName) {
return fetch( attributeName, DEFAULT_JOIN_TYPE );
}
public void addFetch(Fetch<X, ?> fetch);