Spring Data JPA。存储库继承,抛出BeanCreationException,NullPointerException

时间:2014-08-23 06:19:12

标签: spring jpa inheritance nullpointerexception repository-pattern

所以我开始使用Spring Data JPA,我发现它最初很容易使用,特别是对于简单的POJO实体,我设法用单个实体(Person)执行简单的CRUD操作,但是当我深入挖掘我的设计时(继承),在继承设计方面,我开始很难处理Spring JPA存储库。

传奇:

的POJO

存储库

使用上述两个

的示例类

抛出异常

xml配置

人类(基础,抽象类)

 @MappedSuperclass
 public abstract class Person {
       ..properties, getters and setters with hibernate annotations
}

学生班(孩子,延伸人)

 @Entity
 @Table(name = "STUDENT")
 public class Student extends Person {
        .. properties, getters and setters SPECIFIC for a student
 }

贮库

PersonRepository (基础,父存储库)

 public interface PersonRepository<T extends Person, ID extends Serializable> extends JpaRepository<T, ID> {
 }    

StudentRepository (child,extends PersonRepository)

 public interface StudentRepository extends PersonRepository<Student, Integer>{
 }

示例类

 @Service ("manager")
  public class Manager {

  @SuppressWarnings("rawtypes")
  @Resource (name = "personRepository")
  private PersonRepository personRepository;

  @SuppressWarnings("unchecked")
  public void savePerson(Person p) {

      personRepository.save(p);
  }
}

抛出异常

  

线程中的异常&#34; main&#34; org.springframework.beans.factory.BeanCreationException:使用名称&#39; manager&#39;创建bean时出错:资源依赖注入失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为&#39; personRepository&#39;:FactoryBean的bean在创建对象时引发异常;嵌套异常是java.lang.NullPointerException       at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:308)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)       在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:302)       在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)       在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)       在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)       at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)       在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)       at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)       在org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139)       在org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83)       在edu.main.Main.main(Main.java:12)   引起:org.springframework.beans.factory.BeanCreationException:创建名称为&#39; personRepository&#39;:FactoryBean的bean在创建对象时引发异常;嵌套异常是java.lang.NullPointerException       在org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:175)       在org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)       在org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1512)       在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:313)       在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)       at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:446)       在org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:420)       at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor $ ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:545)       在org.springframework.beans.factory.annotation.InjectionMetadata $ InjectedElement.inject(InjectionMetadata.java:155)       在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)       在org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:305)       ......还有13个   引起:java.lang.NullPointerException       at java.lang.Class.isAssignableFrom(Native Method)       在org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getMetadata(JpaEntityInformationSupport.java:58)       在org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:145)       在org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:83)       在org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:66)       在org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:146)       在org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:120)       在org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:39)       在org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)

xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:repository="http://www.springframework.org/schema/data/repository"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:tx="http://www.springframework.org/schema/tx"             xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     http://www.springframework.org/schema/context  
     http://www.springframework.org/schema/context/spring-context-4.0.xsd
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
     http://www.springframework.org/schema/data/repository
     http://www.springframework.org/schema/data/repository/spring-repository.xsd
     http://www.springframework.org/schema/data/jpa 
     http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">


<context:annotation-config />
<context:component-scan base-package="edu.service" />
<jpa:repositories base-package="edu.repository" />
<tx:annotation-driven />

<context:property-placeholder
    location="classpath:properties/database.properties"
    ignore-unresolvable="false" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
        <list>
            <value>edu.domain</value>
        </list>
    </property>
    <property name="persistenceUnitName" value="personPU"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
        </props>
    </property>
</bean>

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

我试图找到一个特定的单词来搜索类似的问题,但没有运气,可能导致我的存储库没有存储库bean的问题(NullPointerException / BeanCreationException)?还有一件事我想补充一下,我的设计模式有问题吗?我应该将我的POJO继承模式反映到我的存储库吗?我试图让我的PersonRepository在作为Person类(抽象父类)的子节点的POJO / Entites上执行操作,这就是为什么我想出了存储库继承的想法。我的具体目标是,对使用PersonRepository扩展Person的任何对象持久/执行CRUD。非常感谢任何帮助/建议/意见。请。非常感谢你

1 个答案:

答案 0 :(得分:1)

我认为PersonRepository应注明@NoRepositoryBean

在我的申请中,我这样做了:

父:

@NoRepositoryBean
public interface UserRepository<T> extends JpaRepository<T, Long> {
}

子:

@Repository
public interface EmployeeRepository extends UserRepository<Employee> {
}

希望它有所帮助。