【问题标题】:JPQL : Named Parameters not detectedJPQL:未检测到命名参数
【发布时间】:2021-04-11 11:42:39
【问题描述】:

我正在尝试为我的数据库制作一个非常简单的搜索引擎。基本上,它选择名称或描述至少包含一个搜索词的元素。方法如下:

    public List<Product> getProductSearch(List<String> tokens) {
    em.clear();
    String query = "SELECT p FROM Product p WHERE ";
    TypedQuery<Product> queryExec;
    List<Product> result;
    
    for (int i = 0; i < tokens.size(); i++) {
        query += "p.nom LIKE :token" + i + " OR p.description LIKE :token" + i; //invoke a parameter named token<i> for the i-th token
        if(i != tokens.size()-1) query += " OR ";
    }
    System.out.println("Query : " + query);
    queryExec = em.createQuery(query, Product.class);
    
    for (int i = 0; i < tokens.size(); i++) {
        queryExec.setParameter("token"+i,"%" + tokens.get(i) + "%"); //Declare every used parameters
    }
    
    result = queryExec.getResultList();
    
    return result;
}

但是,当我使用简单的搜索查询(例如“word1 word2”)测试此方法时,JPA 的行为就像我没有声明参数一样,并且还修改了查询。当我的方法生成的最终查询是:

SELECT p FROM Product p WHERE p.nom LIKE :token0 OR p.description LIKE :token0 OR p.nom LIKE :token1 OR p.description LIKE :token1

token0token1 被声明为等于 word1word2,JPA 日志显示实际执行的查询是:

SELECT t0.id, t0.categorie, t0.description, t0.nom, t0.prix, t0.score, t0.stock FROM produits t0 WHERE (t0.nom LIKE ? ESCAPE '\\' OR t0.description LIKE ? ESCAPE '\\' OR t0.nom LIKE ? ESCAPE '\\' OR t0.description LIKE ? ESCAPE '\\') [params=?, ?, ?, ?]

所以我的问题是:

-这种行为正常吗?

-我的错误在哪里?

-修复它的最佳方法是什么?

我正在使用 eclipse、OpenJPA、MySQL 和 Java 8。

【问题讨论】:

    标签: java mysql jpa jpql openjpa


    【解决方案1】:

    是的,这种行为是正常的。

    在日志中,您会看到 SQL 查询,而不是 JPQL 查询。此 SQL 查询通常通过 JDBC PreparedStatement 执行,命名参数被转换为 位置参数 - 这是 JPA 提供程序的内部工作。

    【讨论】:

    • 谢谢,我确实没有犯任何错误,我对此进行了一些研究,现在我对 JPA/JPQL 的内部工作有了更好的了解。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    • 2016-11-29
    • 2022-01-04
    • 2011-09-29
    • 1970-01-01
    相关资源
    最近更新 更多