我有一个查询试图从缺陷列表中获取一些详细信息:
String useIndexPrimary = "USE INDEX (PRIMARY)";
TypedQuery<Defect> query = getEntityManager().createQuery("select d from "+getEntityName()+" d " + useIndexPrimary + " WHERE d.id in :defectIds", Defect.class);
query.setParameter("defectIds", ids);
基本上可以翻译为:
SELECT d FROM DefectConcrete d USE INDEX(PRIMARY) WHERE d.id IN defects"
当我尝试执行我得到的代码时
2024-07-22 12:01:27,936 [pool-177-thread-2] ::: ERROR com.optimyth.csaas.infrastructure.queues.spring.AnalysisLifeCycleManager -
java.lang.RuntimeException: Could not process stream for Diff defect.
...
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: USE near line 1, column 77 [select d from com.optimyth.csaas.metamodel.implementations.DefectConcrete d USE INDEX (PRIMARY) WHERE d.id in :defectIds]
我认为我在正确的位置使用了“USE INDEX”,但我不确定为什么会出现“意外令牌”问题。
您在查询中使用的“USE INDEX”导致了问题。 JPQL 是一种与数据库无关的语言,而“USE INDEX”子句是特定于数据库的。
正确的查询应该是
SELECT d FROM DefectConcrete d WHERE d.id IN :defectIds