Spring数据JPA-动态SQL执行 [英] Spring data JPA - Dynamic SQL Execution

查看:63
本文介绍了Spring数据JPA-动态SQL执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行将在运行时完全构建的SQL,它可以查询架构中的任何表.

I want to Execute the SQL , which will be constructed completely at runtime and it can be querying any tables in the schema.

类似

@Repository public interface BaseRepository extends JpaRepository<Object, Integer> {        
    @Query(":dynamicSQL")   
    List<Object> dynamicExecution(@Param("dynamicSQL") String dynamicSQL);           
}

请建议如何实施

推荐答案

JpaRepository并非旨在像这样使用.如果要执行动态SQL查询,请使用EntityManager.这是一个糟糕的设计,但是如果您仍然想这样做,请使用默认方法并像下面这样传递EntityManager bean:

JpaRepository is not designed to be used like this. If you want to execute dynamic SQL query then use EntityManager. This is bad design but if you still want to do this then use default methods and pass a EntityManager bean like this:

default List<?> dynamicExecution(EntityManager em, String sql) {
    return em.createNativeQuery(sql).getResultList();
}

这篇关于Spring数据JPA-动态SQL执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆