2017-10-13 144 views
0

爲什麼使用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 (?, ?)

回答

0

嘗試將類型更改爲Collection<String> keys而不是List<String> keys。根據documentation,這應該是Collection,可能是Spring認爲它是你的自定義方法,而不是查詢方法。

+0

用'Collection'給它一個嘗試,(當我在它)'Iterable',但於事無補。我們得到一個JpaSystemException並且它裏面有SQL的事實似乎也證明了Spring *確實識別了用'List'聲明的方法。 – Florian