请求为JPA Inbound Channel Adapter添加javax.persistence.Query直接支持以适配QueryDsl类型安全查询
QueryDSL + Spring Integration JPA Inbound Channel Adapter: 期望支持直接传入
javax.persistence.Query对象 我们团队非常青睐QueryDSL带来的类型安全查询能力,但在尝试将它与Spring Integration的JPA Inbound Channel Adapter结合时遇到了瓶颈——这个适配器目前仅支持字符串形式的查询(包括namedQuery、nativeQuery、jpaQuery三种方式)。
我们希望JPA Inbound Channel Adapter能够提供直接接受javax.persistence.Query对象的能力,以此避开字符串查询的类型不安全问题,实现像下面这样简洁直观的用法:
import com.querydsl.jpa.impl.JPAQueryFactory; import javax.persistence.Query; public JpaInboundChannelAdapterSpec getInboundAdapter() { JPAQueryFactory jpaQueryFactory = new JPAQueryFactory(entityManager); Query query = jpaQueryFactory.selectFrom(qFoo).where(qFoo.bar.eq("john")).createQuery(); return Jpa.inboundAdapter(entityManager) .query(query); }
目前探索的潜在替代方案
我们暂时找到了一个可能的替代思路:结合使用Query.unwrap()方法和JPA入站适配器的.parameterSource()来间接实现,但这个方案的实现细节比较复杂,目前还没有完全梳理清楚。如果后续验证这个方案可行,我们会更新相关内容。
替代方案的示例代码框架如下:
import com.querydsl.jpa.impl.JPAQueryFactory; import javax.persistence.Query; import org.springframework.integration.jpa.support.parametersource.PositionSupportingParameterSource; public JpaInboundChannelAdapterSpec getInboundAdapter() { JPAQueryFactory jpaQueryFactory = new JPAQueryFactory(entityManager); Query query = jpaQueryFactory.selectFrom(qFoo).where(qFoo.bar.eq("john")).createQuery(); String jpql = query.unwrap(org.hibernate.query.Query.class).getQueryString(); //TODO: 创建PositionSupportingParameterSource的实现类,从query中提取参数信息 PositionSupportingParameterSource parameterSource = new TodoParameterSource(query.getParameters()); return Jpa.inboundAdapter(entityManager) .jpaQuery(jpql) .parameterSource(parameterSource); }
内容的提问来源于stack exchange,提问作者Maxime Dutaut
相关产品推荐
相关产品推荐

