意外的令牌:)findAll和findByKeyIn使用Spring和Eclipselink JPA
问题描述:
为什么使用Spring 1.5.4.RELEASE和Eclipselink 2.6.4进行以下两个简单的集成测试失败?意外的令牌:)findAll和findByKeyIn使用Spring和Eclipselink JPA
@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认为它是你的自定义方法,而不是查询方法。
用'Collection'给它一个尝试,(当我在它)'Iterable',但于事无补。我们得到一个JpaSystemException并且它里面有SQL的事实似乎也证明了Spring *确实识别了用'List'声明的方法。 – Florian