Last active
January 15, 2016 21:35
-
-
Save lekant/4b5cfc89ef722382ce7c to your computer and use it in GitHub Desktop.
How to get an existing Join in a JPA criteria query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.emdeon.claimmaster.entities.Ubhcdata; | |
import com.emdeon.claimmaster.entities.Ubupload; | |
import com.emdeon.claimmaster.entities.Ubuploadclaimrelation; | |
import javax.persistence.criteria.Join; | |
import javax.persistence.criteria.JoinType; | |
import javax.persistence.criteria.Root; | |
import javax.persistence.metamodel.CollectionAttribute; | |
import javax.persistence.metamodel.ListAttribute; | |
import javax.persistence.metamodel.SingularAttribute; | |
import java.util.Iterator; | |
import java.util.Set; | |
/** | |
* Created by qlehenaff on 12/10/15. | |
* | |
* Find and return a join between R and J | |
* | |
*/ | |
public class QueryJoinHelper<R, J> { | |
/** | |
* Find and return a join between R and J on attribute | |
* @param root root entity | |
* @param attribute join attribute between R and J | |
* @param joinedEntity Class instance of the joined entity | |
* @return | |
*/ | |
public Join<R, J> getExistingJoin(Root<R> root, SingularAttribute<R, J> attribute, Class<J> joinedEntity) { | |
Join<R, J> ubhcdataRoot = null; | |
final Set<Join<R, ?>> joins = root.getJoins(); | |
if (!joins.isEmpty()) { | |
final Iterator<Join<R, ?>> iterator = joins.iterator(); | |
while (iterator.hasNext()) { | |
final Join<R, ?> next = iterator.next(); | |
if (next.getModel().getBindableJavaType().getSimpleName().equals(joinedEntity.getSimpleName())) { | |
ubhcdataRoot = (Join<R, J>) next; | |
break; | |
} | |
} | |
if (ubhcdataRoot == null) { | |
ubhcdataRoot = root.join(attribute); | |
} | |
} else { | |
ubhcdataRoot = root.join(attribute); | |
} | |
return ubhcdataRoot; | |
} | |
/** | |
* Find and return a join between R and J on attribute | |
* @param root root entity | |
* @param attribute join attribute between R and J | |
* @param joinedEntity Class instance of the joined entity | |
* @return | |
*/ | |
public Join<R, J> getExistingJoin(Root<R> root, CollectionAttribute<R, J> attribute, Class<J> joinedEntity) { | |
Join<R, J> ubhcdataRoot = null; | |
final Set<Join<R, ?>> joins = root.getJoins(); | |
if (!joins.isEmpty()) { | |
final Iterator<Join<R, ?>> iterator = joins.iterator(); | |
while (iterator.hasNext()) { | |
final Join<R, ?> next = iterator.next(); | |
if (next.getModel().getBindableJavaType().getSimpleName().equals(joinedEntity.getSimpleName())) { | |
ubhcdataRoot = (Join<R, J>) next; | |
break; | |
} | |
} | |
if (ubhcdataRoot == null) { | |
ubhcdataRoot = root.join(attribute, JoinType.INNER); | |
} | |
} else { | |
ubhcdataRoot = root.join(attribute); | |
} | |
return ubhcdataRoot; | |
} | |
public Join<R, J> getExistingJoin(Join<?, R> rJoin, SingularAttribute<R, J> attribute, Class<J> joinedEntity) { | |
Join<R, J> ubhcdataRoot = null; | |
final Set<Join<R, ?>> joins = rJoin.getJoins(); | |
if (!joins.isEmpty()) { | |
final Iterator<Join<R, ?>> iterator = joins.iterator(); | |
while (iterator.hasNext()) { | |
final Join<R, ?> next = iterator.next(); | |
if (next.getModel().getBindableJavaType().getSimpleName().equals(joinedEntity.getSimpleName())) { | |
ubhcdataRoot = (Join<R, J>) next; | |
break; | |
} | |
} | |
if (ubhcdataRoot == null) { | |
ubhcdataRoot = rJoin.join(attribute); | |
} | |
} else { | |
ubhcdataRoot = rJoin.join(attribute); | |
} | |
return ubhcdataRoot; | |
} | |
public Join<R, J> getExistingJoin(Root<R> root, ListAttribute<R, J> attribute, Class<J> joinedEntity) {{ | |
Join<R, J> ubhcdataRoot = null; | |
final Set<Join<R, ?>> joins = root.getJoins(); | |
if (!joins.isEmpty()) { | |
final Iterator<Join<R, ?>> iterator = joins.iterator(); | |
while (iterator.hasNext()) { | |
final Join<R, ?> next = iterator.next(); | |
if (next.getModel().getBindableJavaType().getSimpleName().equals(joinedEntity.getSimpleName())) { | |
ubhcdataRoot = (Join<R, J>) next; | |
break; | |
} | |
} | |
if (ubhcdataRoot == null) { | |
ubhcdataRoot = root.join(attribute); | |
} | |
} else { | |
ubhcdataRoot = root.join(attribute); | |
} | |
return ubhcdataRoot; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment