gpt4 book ai didi

java - JPA 查询 LIKE 不起作用

转载 作者:行者123 更新时间:2023-12-02 13:24:51 25 4
gpt4 key购买 nike

此查询有什么问题:SELECT i FROM Item i WHERE 1 = 1 AND UPPER(i.nome) LIKE %:nome% ORDER BY UPPER(i.nome)?我尝试过在 LIKE '%:nome%' 中使用或不使用单引号,但它不起作用。

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Syntax error parsing [SELECT i FROM Item i WHERE 1 = 1 AND UPPER(i.nome) LIKE %:nome% ORDER BY UPPER(i.nome)].
[56, 63] The identification variable '%:nome%' is not following the rules for a Java identifier.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)

最佳答案

如上所述here ,设置参数时需要用%包裹值,例如:

query = em.createQuery("SELECT i FROM Item i WHERE 1 = 1  AND UPPER(i.nome) LIKE :nome  ORDER BY UPPER(i.nome)");
query.setParameter("nome","%"+nome.toUpperCase()+"%");

关于java - JPA 查询 LIKE 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43430757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com