创建Spring Data JPA存储库bean的java.util.NoSuchElementException

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (169)

我的依赖是:

springVersion ='3.2.4.RELEASE'

compile "org.springframework:spring-***:$springVersion"
*** = all of them are included just in case!
compile "org.springframework.data:spring-data-jpa:1.4.1.RELEASE"
compile "org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final"
runtime "org.hibernate:hibernate-entitymanager:4.2.6.Final"
testCompile "org.springframework:spring-test:$springVersion"
runtime "com.h2database:h2:1.3.173"

我有以下的上下文文件:

<tx:annotation-driven />

<jpa:repositories base-package="au.com.data3.samples.user.priv" />

<jdbc:embedded-database id="dataSource" type="H2">
</jdbc:embedded-database>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="au.com.data3.samples.user.model" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="false" />
            <property name="generateDdl" value="true" />
            <property name="database" value="H2" />
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <!--<prop key="hibernate.max_fetch_depth"></prop>-->
            <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

我在.... user.priv包中有我的Repository类:

public interface D3UserRepository extends JpaRepository<D3User, Long> {

D3User findByUsername(String username);
    List<D3User> findByFirstName(String firstName);
    List<D3User> findByLastName(String lastName);
    List<D3User> findByFirstNameAndLastName(String lastName);
}

我有一个Spring / JUnit测试,它有以下内容导致错误:

@Autowired
private D3UserService mD3UserService;

堆栈跟踪是:

Caused by: java.util.NoSuchElementException: null
    at java.util.ArrayList$Itr.next(ArrayList.java:834) ~[na:1.7.0_40]
    at org.springframework.data.jpa.repository.query.ParameterMetadataProvider.next(ParameterMetadataProvider.java:76) ~[spring-data-jpa-1.3.5.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.build(JpaQueryCreator.java:242) ~[spring-data-jpa-1.3.5.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:144) ~[spring-data-jpa-1.3.5.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.and(JpaQueryCreator.java:96) ~[spring-data-jpa-1.3.5.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.and(JpaQueryCreator.java:44) ~[spring-data-jpa-1.3.5.RELEASE.jar:na]
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:109) ~[spring-data-commons-1.5.3.RELEASE.jar:na]
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:88) ~[spring-data-commons-1.5.3.RELEASE.jar:na]
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:73) ~[spring-data-commons-1.5.3.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.<init>(PartTreeJpaQuery.java:98) ~[spring-data-jpa-1.3.5.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:60) ~[spring-data-jpa-1.3.5.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:90) ~[spring-data-jpa-1.3.5.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162) ~[spring-data-jpa-1.3.5.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:68) ~[spring-data-jpa-1.3.5.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:279) ~[spring-data-commons-1.5.3.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:147) ~[spring-data-commons-1.5.3.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:153) ~[spring-data-commons-1.5.3.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:43) ~[spring-data-commons-1.5.3.RELEASE.jar:na]
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142) ~[spring-beans-3.2.4.RELEASE.jar:3.2.4.RELEASE]
    ... 83 common frames omitted

我究竟做错了什么?我已经尝试了类似帖子的许多不同的东西,但没有任何工作。

提问于
用户回答回答于

如果仍然有帮助:你错过了第二个参数

List<D3User> findByFirstNameAndLastName(String lastName);

它一定要是

List<D3User> findByFirstNameAndLastName(String firstName, String lastName);

扫码关注云+社区