gpt4 book ai didi

spring-data-jpa - java.util.NoSuchElementException创建Spring Data JPA存储库bean

转载 作者:行者123 更新时间:2023-12-03 12:25:08 25 4
gpt4 key购买 nike

我的依赖项是:

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包中有我的存储库类:
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;

stacktrace是:
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);

关于spring-data-jpa - java.util.NoSuchElementException创建Spring Data JPA存储库bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19160411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com