Skip to content

Instantly share code, notes, and snippets.

@composite
Created May 3, 2021 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save composite/2b6b4a1d7afa052ace930ddc66588b53 to your computer and use it in GitHub Desktop.
Save composite/2b6b4a1d7afa052ace930ddc66588b53 to your computer and use it in GitHub Desktop.
QueryDsl EntityPath to RelationalPath Wrapper for QueryDsl JPA with QueryDsl SQL (SQLQueryFactory)
import com.querydsl.core.types.EntityPath;
import com.querydsl.sql.RelationalPath;
import com.querydsl.sql.RelationalPathBase;
import javax.persistence.Table;
import java.lang.reflect.AnnotatedElement;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public final class QueryDslUtils {
/**
* Simple Type cache (If you want another cache strategy, make it your own.)
*/
private static final ConcurrentMap<EntityPath<?>, RelationalPath<?>> relationalMap = new ConcurrentHashMap<>();
/**
* Entity Class to SQLQueryFactory RelationalPath
* @param entityPath
* @param <T>
* @return
*/
@SuppressWarnings("unchecked")
public static <T> RelationalPath<T> asRelational(EntityPath<T> entityPath) {
AnnotatedElement annotatedElement = Objects.requireNonNull(Objects.requireNonNull(entityPath, "entityPath is null").getAnnotatedElement(), "no annotation");
Table table = Objects.requireNonNull(annotatedElement.getAnnotation(Table.class), "no entity table");
RelationalPath<?> result = relationalMap.get(entityPath);
if(result == null)
relationalMap.put(entityPath, result = new RelationalPathBase<T>(entityPath.getType(), entityPath.getMetadata(), table.schema(), table.name()));
return (RelationalPath<T>) result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment