在项目迁移期间,我在多个地方的EclipseLink(我使用的是2.7.4)中遇到了潜在的错误。我创建了一个简单的示例来说明我面临的问题。根据使用IN
编写CriteriaBuilder
查询的方式的不同,在IN
参数周围添加了一组括号:
AddressDaoTest.java
@Test
public void thisTestFails() {
objUnderTest.findAddressInStreetCached(Arrays.asList("Houston", "Broadway"));
}
@Test
public void thisTestIsFine() {
objUnderTest.findAddressInStreetUncached(Arrays.asList("Houston", "Broadway"));
}
AddressDao.java
public List<Adresse> findAddressInStreetCached(Collection<String> streets) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Adresse> query = cb.createQuery(Adresse.class);
Root<Adresse> adresseRoot = query.from(Adresse.class);
CriteriaParameterBuilder pb = new CriteriaParameterBuilder(cb);
query.where(cb.in(adresseRoot.get("street")).value(pb.collParameter("street", streets)));
TypedQuery<Adresse> typedQuery = entityManager.createQuery(query);
pb.applyParams(typedQuery);
return typedQuery.getResultList();
}
public List<Adresse> findAddressInStreetUncached(Collection<String> streets) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Adresse> query = cb.createQuery(Adresse.class);
Root<Adresse> adresseRoot = query.from(Adresse.class);
query.where(adresseRoot.get("street").in(streets));
TypedQuery<Adresse> typedQuery = entityManager.createQuery(query);
return typedQuery.getResultList();
}
测试结果
thisTestFails:
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.4.v20190115-ad5b7c6b2a): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: Syntax error: Encountered "," at line ...
但是请注意,到目前为止,对于完全相同的问题,我已经获得了各种不同的例外。我经常收到以下错误,只有在为Stackoverflow创建的小示例中,我才收到上述错误。
The parameter name [name] in the query's selection criteria does not match any parameter name defined in the query.
thisTestIsFine:测试运行没有问题
生成的SQL
thisTestFails:
SELECT * FROM ADRESSE WHERE (STREET IN ((?,?)))
thisTestIsFine:
SELECT * FROM ADRESSE WHERE (STREET IN (?,?))
我的问题: