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

时间:2013-10-03 13:24:36

标签: spring-data-jpa

我的依赖关系是:

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

我做错了什么?我在类似的帖子中尝试了很多不同的东西,但没有任何效果。

请帮忙。

1 个答案:

答案 0 :(得分:50)

如果还有一些帮助:你错过了

中的第二个参数
List<D3User> findByFirstNameAndLastName(String lastName);

必须是

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