Spring Data JPA Repository:IllegalArgumentException此参数是必需的

时间:2011-11-20 19:21:34

标签: hibernate spring jpa maven repository

我正在尝试使用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中重新运行我的测试,它们会因上述异常而失败。

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

java.lang.IllegalArgumentException in Spring-data-Jpa

我尝试了他们推荐的内容,但后来我发现没有持久性提供程序的错误。我似乎无法持续工作。

使用:

STS 2.8.0.RELEASE JDK 1.6

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

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

我发现如果我:

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

    我发现“JUnit and Spring - What You Don't Know”非常宝贵。

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