意外的标记: ) 在 findAll 和 findByKeyIn 与 Spring 和 Eclipselink JPA

Unexpected token: ) in findAll and findByKeyIn with Spring and Eclipselink JPA

为什么以下两个简单的集成测试失败,使用 Spring 1.5.4.RELEASE 和 Eclipselink 2.6.4?

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MyApp.class})
@WebAppConfiguration
@DirtiesContext(classMode = ClassMode.BEFORE_CLASS)
public class DbDummyIT {

    @Autowired
    private DbDummyRepository repo;

    @Test
    public void findAllFails() {
        List<String> keys = Arrays.asList("1", "2");
        List<DbDummy> result = repo.findAll(keys);
        assertThat(result.isEmpty()).isTrue();
    }

    @Test
    public void findByKeyInFails() {
        List<String> keys = Arrays.asList("1", "2");
        List<DbDummy> result = repo.findByKeyIn(keys);
        assertThat(result.isEmpty()).isTrue();
    }

}

基于:

@Entity
@UuidGenerator(name = DbDummy.KEY_GENERATOR)
public class DbDummy {

    public static final String KEY_GENERATOR = "KeyGenerator";

    @Id
    @Column(name = ColumnName.KEY, nullable = false)
    @GeneratedValue(generator = KEY_GENERATOR)
    public String key;

}

@Repository
public interface DbDummyRepository extends JpaRepository<DbDummy, String> {

  List<DbDummy> findByKeyIn(List<String> keys);

}

调用 find 查询时,两个测试都失败并出现 JpaSystemException。错误信息是:

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: unexpected token: ) in statement [SELECT Key FROM DBDUMMY WHERE (Key IN ((?,?)))]
Error Code: -5581
Call: SELECT Key FROM DBDUMMY WHERE (Key IN ((?,?)))
    bind => [2 parameters bound]
Query: ReadAllQuery(referenceClass=DbDummy sql="SELECT Key FROM DBDUMMY WHERE (Key IN (?))")

这个说法在我看来确实是错误的。 Key IN ((?, ?)) 不查询双值元组列表吗?不应该将字符串列表查询为 Key IN (?, ?)?

尝试将类型更改为 Collection<String> keys 而不是 List<String> keys。根据documentation,这应该是Collection,可能Spring认为是你的自定义方法,不是查询方法。

看起来像是 Eclipselink 中的一个长期存在的错误:https://bugs.eclipse.org/bugs/show_bug.cgi?id=349477