Spring Data JPA 存储库:IllegalArgumentException 此参数是必需的

发布于 2024-12-17 14:31:33 字数 8868 浏览 1 评论 0原文

我正在尝试使用 Spring Data JPA 配置存储库。尝试执行测试时出现以下异常。我可以让它工作,然后我使用 Maven 进行编译,但我的测试失败了。然后我回去手动运行测试,但它们失败了(全部都没有更改任何代码)。我猜测这可能是路径问题,但我已经从一个干净的项目开始并继续遇到问题。我的例外是:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1440)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
    at com.vinoetic.vinoeticLibrary.util.ContextHelper.getContext(ContextHelper.java:15)
    at com.vinoetic.vinoeticLibrary.repository.TestPersonRepository.testRepository(TestPersonRepository.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.util.Assert.notNull(Assert.java:123)
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.<init>(SimpleJpaRepository.java:74)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:94)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:69)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:131)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:114)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:38)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
    ... 29 more
[/code]

我的 pom 包含以下依赖项(除其他外)

<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-entitymanager</artifactId>
   <version>3.6.8.Final</version>
</dependency> 

<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-core</artifactId>
   <version>3.6.8.Final</version>
</dependency> 

<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-ehcache</artifactId>
   <version>3.6.8.Final</version>
</dependency> 


<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-commons-core</artifactId>
    <version>${spring-data-commons-core.version}</version>
</dependency>
<dependency>
        <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>${spring-data-jpa.version}</version>
</dependency>       

我的实体是:

public interface PersonRepository extends CrudRepository<Person, Long> {
    List<Person> findByUserId(Long userId);
}

我的实体是:

@Entity
@Table(name="PERSON")
@ToString(includeFieldNames=true)
@Data public class Person {

    @Id
    private Long personId = null;
    private Long userId = null;
    private String lastName = null;
    private String firstName = null;
    private String email = null;
    private String phoneHome = null;
    private String phoneCell = null;
    private String phoneWork = null;
    private Date birthdate = null;
    private String namePrefix = null;
    private String title = null;
    @Column(columnDefinition="tinyint")
    private Boolean sendNewsletter = false;
    private String facebookURL = null;
    private String profileImageURL = null;

    @Transient
    private UserProfileImage profileImages = null;


}

我的上下文设置是:

<bean id="mySessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="vinoeticDataSource_MySQL" />
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
        </value>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="vinoeticDataSource_MySQL" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />


<!-- JPA -->
<jpa:repositories base-package="com.vinoetic.interfacesAndModels.repositories.sql"
    entity-manager-factory-ref="emf1" transaction-manager-ref="transactionManager" />

<bean id="emf1"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="vinoeticDataSource_MySQL" />
    <property name="persistenceUnitName" value="persistenceUnit" />
    <property name="loadTimeWeaver">
        <bean
            class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    </property>
</bean>

最后,我的 persistence.xml 是:

<persistence    xmlns="http://java.sun.com/xml/ns/persistence" 
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                version="2.0" 
                xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
    </persistence-unit>
</persistence>

我已经花了 3 天在这上面,但我似乎无法使其始终如一地发挥作用。正如我提到的,我可以让它工作,但如果我使用 Maven 编译,我的测试就会失败。然后在 Eclipse 中重新运行我的测试,它们因上述异常而失败。

我在这里查看了其他帖子并尝试了这些建议。我提到:

Spring-data-Jpa 中的 java.lang.IllegalArgumentException

我尝试了他们推荐的方法,但随后出现错误,提示没有持久性提供程序。我似乎无法让任何事情持续工作。

使用:

STS 2.8.0.RELEASE JDK 1.6

感谢您的帮助。

I am trying to configure repositories using Spring Data JPA. I get the following exception when trying to execute my tests. I can get it to work and then I compile using Maven and my tests fail. Then I go back to manually run the tests, and they fail (All without changing any code). I am guessing that it might be a path problem, but I've started with a clean project and continue to have issues. My exception is:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1440)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
    at com.vinoetic.vinoeticLibrary.util.ContextHelper.getContext(ContextHelper.java:15)
    at com.vinoetic.vinoeticLibrary.repository.TestPersonRepository.testRepository(TestPersonRepository.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.util.Assert.notNull(Assert.java:123)
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.<init>(SimpleJpaRepository.java:74)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:94)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:69)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:131)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:114)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:38)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
    ... 29 more
[/code]

My pom includes the following dependencies (among others)

<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-entitymanager</artifactId>
   <version>3.6.8.Final</version>
</dependency> 

<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-core</artifactId>
   <version>3.6.8.Final</version>
</dependency> 

<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-ehcache</artifactId>
   <version>3.6.8.Final</version>
</dependency> 


<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-commons-core</artifactId>
    <version>${spring-data-commons-core.version}</version>
</dependency>
<dependency>
        <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>${spring-data-jpa.version}</version>
</dependency>       

My entity is:

public interface PersonRepository extends CrudRepository<Person, Long> {
    List<Person> findByUserId(Long userId);
}

My entity is:

@Entity
@Table(name="PERSON")
@ToString(includeFieldNames=true)
@Data public class Person {

    @Id
    private Long personId = null;
    private Long userId = null;
    private String lastName = null;
    private String firstName = null;
    private String email = null;
    private String phoneHome = null;
    private String phoneCell = null;
    private String phoneWork = null;
    private Date birthdate = null;
    private String namePrefix = null;
    private String title = null;
    @Column(columnDefinition="tinyint")
    private Boolean sendNewsletter = false;
    private String facebookURL = null;
    private String profileImageURL = null;

    @Transient
    private UserProfileImage profileImages = null;


}

My context setup is:

<bean id="mySessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="vinoeticDataSource_MySQL" />
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
        </value>
    </property>
</bean>

<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="vinoeticDataSource_MySQL" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />


<!-- JPA -->
<jpa:repositories base-package="com.vinoetic.interfacesAndModels.repositories.sql"
    entity-manager-factory-ref="emf1" transaction-manager-ref="transactionManager" />

<bean id="emf1"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="vinoeticDataSource_MySQL" />
    <property name="persistenceUnitName" value="persistenceUnit" />
    <property name="loadTimeWeaver">
        <bean
            class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    </property>
</bean>

And finally, my persistence.xml is:

<persistence    xmlns="http://java.sun.com/xml/ns/persistence" 
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                version="2.0" 
                xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
    </persistence-unit>
</persistence>

I've spent 3 days on this and I can't seem to get this to work consistently. As I mentioned, I can get it to work, but if I compile using maven, my tests fail. Then rerunning my tests in Eclipse, they fail with the above exception.

I've looked at other posts here and tried the suggestions. I referred to:

java.lang.IllegalArgumentException in Spring-data-Jpa

I tried what they recommend, but then I get an error that there isn't a persistence provider. I can't seem to get anything to work consistently.

Using:

STS 2.8.0.RELEASE
JDK 1.6

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離人涙 2024-12-24 14:31:33

我偶然发现了这个问题,因为我遇到了同样的问题。

我发现如果我满足以下条件,我就能够在单元测试中使用 JPA 存储库:

  1. 创建一个单独的 ApplicationContext 用于提供存储库的测试目的。
  2. 引入了 ApplicationContext,以便与使用 @ContextConfiguration 注释的文本一起使用。
  3. 为存储库创建一个类属性,然后使用 @Inject 注释 setter 方法,

我发现“JUnit 和 Spring - 你不知道的事情" 是无价的。

我希望这对某人有帮助 - 我知道这是一个非常古老的线程,但 Google + StackOverFlow = 快乐的笑脸可能会在 2015 年的某个时候!

I have stumbled across this question as I had the same problem.

I found that I was able to use JPA Repositories in my Unit Tests if I:

  1. Created a seperate ApplicationContext for testing purposes which supplied the Repository.
  2. Introduced the ApplicationContext for use with the text using the @ContextConfiguration annotation.
  3. Creating a class property for the Repository, then annotating the setter method with @Inject

I found "JUnit and Spring - What You Don't Know" to be invaluable.

I hope this helps someone - I know this is a very old thread but Google + StackOverFlow = happy smiley faces probably sometime in 2015!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文