繁体   English   中英

Spring JPA @Query - 尝试对多个 SQL ZDBC11CAABD5BDA99F77E6FBE 使用 1 个参数; 收到有关 SQL 语法的错误消息

[英]Spring JPA @Query - Trying to use 1 Param for multiple SQL arguments; getting error message about SQL Syntax

我正在使用 Spring JPA 中的 @Query 注释来编写自定义查询。 仅针对 1 或 2 个小字符串执行此操作时我取得了成功 - 但是,我正在尝试创建 1 个名为“args”的参数,它可能有几个约束/参数。

例如,“args”参数被传递为“title like '%iphone%'”(title%20like%20%27%25iphone%25%27);

“addArgs”是附加约束,如“和标题,如 '%12%'”(和%20title%20like%20%27%2512%25%27)

    @Query(
            value = "select :id as Id, :session as sessionId, :secondary as secondary, :primary as returnedData from :table where :args :addargs order by :order ;",
            nativeQuery = true
    )
    List<TypeAhead> showMe(
            @Param("id") String id,
            @Param("session") String session,
            @Param("primary") String primary,
            @Param("secondary") String secondary,
            @Param("table") String table,
            @Param("args") String args,
            @Param("addArgs") String addArgs,
            @Param("order") String order
            );

当我这样做时,我收到有关 SQL 语法的错误消息。 我试图将 SQL 记录到控制台; 当我粘贴到 MySQL 工作台时,我自己编写的 System.out.println 工作正常,但在使用:args 参数时不起作用。

以下是 JPA 记录这些参数的方式:

2021-04-13 01:34:05.189 TRACE 6576 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [product_id]
2021-04-13 01:34:05.189 TRACE 6576 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - [666]
2021-04-13 01:34:05.189 TRACE 6576 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [VARCHAR] - [price]
2021-04-13 01:34:05.189 TRACE 6576 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [VARCHAR] - [title]
2021-04-13 01:34:05.190 TRACE 6576 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [5] as [VARCHAR] - [products]
2021-04-13 01:34:05.190 TRACE 6576 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [6] as [VARCHAR] - [title like '%iphone%']
2021-04-13 01:34:05.190 TRACE 6576 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [7] as [VARCHAR] - [and title like '%12%']
2021-04-13 01:34:05.190 TRACE 6576 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [8] as [VARCHAR] - [title asc]

下面是使用 System.out.println 打印到控制台的查询 - 当我将其复制并粘贴到 MYSQL 时,它工作正常。

select product_id as Id, 666 as session_id, price as secondary, title as returned_data  from products where title like '%iphone%' and title like '%12%' order by title asc;

当我将其粘贴到 MySQL 以测试查询时,它按预期返回并且看起来不错。 但是,在运行 Java 应用程序时出现错误。

java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''products' where title like 'title like \'%iphone%\'' 'and title like \'%12%\'' ' at line 1

我是新手,所以这可能是一种笨拙的方式。 有人可以帮助我了解发生了什么问题,或者为什么这不能按预期工作吗? 提前致谢!

编辑/更新:

需要的是使用 EntityManager 来动态构建查询。 https://www.baeldung.com/hibernate-entitymanager

您的查询中有语法错误。

要创建完全动态的查询,需要创建自定义存储库,并在那里构建动态查询并执行它。

下面是最小的代码:

@Repository
public interface CustomRepository {
    public List<ReturnObj> customQueryMethod(Long param);
}

@Repository
public class CustomRepositoryImpl implements CustomRepository {
    
    @Autowired
    private EntityManager entityManager;
    
    public List<ReturnObj> customQueryMethod(Long param)  {
        // build sql query here
        Query query = entityManager.createNativeQuery(sql, Tuple.class);
        List<Tuple> data = query.getResultList();
        // do operations on Tuple data and return
    }
}

@Repository
public interface MainRepository extends JpaRepository<MainEntity, Long>, CustomRepository {}

现在,无论您想使用这个自定义存储库方法,只需自动装配MainRepository ,并在那里调用customQueryMethod

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM