欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring data jpa将@ManyToOne查询从cross join变成left outer join

程序员文章站 2022-04-12 20:59:28
...

在多对一关系中,当多端的表作为主表,并且需要连接一端的表时,默认的连接方式为cross join。在某些业务场景下,查询的数据是不完整的。因为cross join会过滤掉关联字段为null的行数据。所以需要将连接方式从cross join变成其他方式,如left outer join。

见QueryDslJpaRepository类,有如下对于QueryDsl的定义

	private final EntityPath<T> path;
	private final PathBuilder<T> builder;
	private final Querydsl querydsl;

	public QueryDslJpaRepository(JpaEntityInformation<T, ID> entityInformation, EntityManager entityManager) {
		this(entityInformation, entityManager, DEFAULT_ENTITY_PATH_RESOLVER);
	}

	public QueryDslJpaRepository(JpaEntityInformation<T, ID> entityInformation, EntityManager entityManager,
			EntityPathResolver resolver) {

		super(entityInformation, entityManager);

		this.path = resolver.createPath(entityInformation.getJavaType());
		this.builder = new PathBuilder<T>(path.getType(), path.getMetadata());
		this.querydsl = new Querydsl(entityManager, builder);
	}

实现连接方式转换的关键就是要定义自己的QueryDsl,并覆写其中的createQuery方法

public class CustomQuerydsl extends Querydsl {

	public CustomQuerydsl(EntityManager em, PathBuilder<?> builder) {
		super(em, builder);
	}

	@Override
	public AbstractJPAQuery<Object, JPAQuery<Object>> createQuery(EntityPath<?>... paths) {
		AbstractJPAQuery<Object, JPAQuery<Object>> jpaQuery = createQuery();
		for(EntityPath<?> path:paths) {
			jpaQuery = jpaQuery.leftJoin(path);
			Class<?> clazz = path.getClass();
			Field[] fields = clazz.getDeclaredFields();
			for(Field field:fields) {
				Class<?> type = field.getType();
				if(EntityPath.class.isAssignableFrom(type)&&!type.equals(path.getClass())) {
					try {
						EntityPath<?> newPath = (EntityPath<?>)field.get(path);
						jpaQuery = jpaQuery.leftJoin(newPath);
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return jpaQuery;
	}
}
然后我们需要定义一个QueryDslJpaRepository的实现类去定义我们自己的QueryDsl并让我们的Querydsl去创建查询
public class BaseDaoImpl<T, ID extends Serializable> extends QueryDslJpaRepository<T, ID> implements BaseDao<T, ID> {
	
	final Log log = LogFactory.getLog(getClass());
	
	protected final EntityManager entityManager;
	
	protected final JpaEntityInformation<T, ID> entityInformation;
	
	private static final EntityPathResolver DEFAULT_ENTITY_PATH_RESOLVER = SimpleEntityPathResolver.INSTANCE;

    private final EntityPath<T> path;
    private final PathBuilder<T> builder;
    private final Querydsl querydsl;

	public BaseDaoImpl(JpaEntityInformation<T, ID> entityInformation, EntityManager entityManager,
			EntityPathResolver resolver) {
		super(entityInformation, entityManager, resolver);

		this.entityManager = entityManager;
		this.entityInformation = entityInformation;
		
		this.path = resolver.createPath(entityInformation.getJavaType());
        this.builder = new PathBuilder<>(path.getType(), path.getMetadata());
        this.querydsl = new CustomQuerydsl(entityManager, builder);
	}
同时,我们需要覆写createQuery方法,因为Querydsl在QueryDslJpaRepository中被定义为final,如果我们不覆写该方法,创建查询时用的还是QueryDslJpaRepository的QueryDsl

@Override
	protected JPQLQuery<?> createQuery(Predicate... predicate) {

		AbstractJPAQuery<?, ?> query = querydsl.createQuery(path).where(predicate);
		CrudMethodMetadata metadata = getRepositoryMethodMetadata();

		if (metadata == null) {
			return query;
		}

		LockModeType type = metadata.getLockModeType();
		query = type == null ? query : query.setLockMode(type);

		for (Entry<String, Object> hint : getQueryHints().entrySet()) {
			query.setHint(hint.getKey(), hint.getValue());
		}

		return query;
	}
这样ManyToOne的查询就从cross join转换成left outer join