Spring Data存储库会在创建时抛出NPE

时间:2013-12-14 20:37:41

标签: java spring jpa spring-data spring-data-jpa

我有以下bean

@Entity
@Access(AccessType.FIELD)
public class Child {

    @Id @GeneratedValue
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    private Parent parent;
}

然后我宣布了存储库

public interface ChildRepository extends CrudRepository<Child, Long> {

    List<Child> findByParent(Parent parent);

}

在控制器中自动装配

@Controller
public class AController {
    @Autowired private ChildRepository childRepository;
}

启动服务器我收到以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'aController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ChildRepository aController.childRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'childRepository': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
...

如果我删除方法findByParent一切都很好。可能是什么问题?

我正在使用spring-data-jpa:1.4.2.RELEASE和datanucleus-accessplatform -jpa-rdbms:3.3.4。

这是完整的堆栈

QueryUtils.getOrCreateJoin(From<?,?>, String) line: 511 
QueryUtils.toExpressionRecursively(From<?,?>, PropertyPath) line: 453   
JpaQueryCreator$PredicateBuilder.build() line: 197  
JpaCountQueryCreator(JpaQueryCreator).toPredicate(Part, Root<?>) line: 144  
JpaCountQueryCreator(JpaQueryCreator).create(Part, Iterator<Object>) line: 86   
JpaCountQueryCreator(JpaQueryCreator).create(Part, Iterator) line: 44   
JpaCountQueryCreator(AbstractQueryCreator<T,S>).createCriteria(PartTree) line: 109  
JpaCountQueryCreator(AbstractQueryCreator<T,S>).createQuery(Sort) line: 88  
JpaCountQueryCreator(AbstractQueryCreator<T,S>).createQuery() line: 73  
PartTreeJpaQuery$CountQueryPreparer(PartTreeJpaQuery$QueryPreparer).<init>(PartTreeJpaQuery, boolean) line: 98  
PartTreeJpaQuery$CountQueryPreparer.<init>(PartTreeJpaQuery, boolean) line: 186 
PartTreeJpaQuery.<init>(JpaQueryMethod, EntityManager) line: 60 
JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryMethod, EntityManager, NamedQueries) line: 90 
JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryMethod, EntityManager, NamedQueries) line: 162  
JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy(JpaQueryLookupStrategy$AbstractQueryLookupStrategy).resolveQuery(Method, RepositoryMetadata, NamedQueries) line: 68  
RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport, RepositoryInformation, Object, Object) line: 290   
JpaRepositoryFactory(RepositoryFactorySupport).getRepository(Class<T>, Object) line: 158    
JpaRepositoryFactoryBean<T,S,ID>(RepositoryFactoryBeanSupport<T,S,ID>).getObject() line: 162    
JpaRepositoryFactoryBean<T,S,ID>(RepositoryFactoryBeanSupport<T,S,ID>).getObject() line: 44 
DefaultListableBeanFactory(FactoryBeanRegistrySupport).doGetObjectFromFactoryBean(FactoryBean<?>, String, boolean) line: 144    
DefaultListableBeanFactory(FactoryBeanRegistrySupport).getObjectFromFactoryBean(FactoryBean<?>, String, boolean) line: 103  
DefaultListableBeanFactory(AbstractBeanFactory).getObjectForBeanInstance(Object, String, String, RootBeanDefinition) line: 1514 
DefaultListableBeanFactory(AbstractBeanFactory).doGetBean(String, Class<T>, Object[], boolean) line: 252    
DefaultListableBeanFactory(AbstractBeanFactory).getBean(String, Class<T>) line: 200 
DefaultListableBeanFactory(AbstractBeanFactory).doGetBean(String, Class<T>, Object[], boolean) line: 273    
DefaultListableBeanFactory(AbstractBeanFactory).getBean(String) line: 195   
DefaultListableBeanFactory.findAutowireCandidates(String, Class<?>, DependencyDescriptor) line: 1014    
DefaultListableBeanFactory.doResolveDependency(DependencyDescriptor, String, Set<String>, TypeConverter) line: 957  
DefaultListableBeanFactory.resolveDependency(DependencyDescriptor, String, Set<String>, TypeConverter) line: 855    
AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(Object, String, PropertyValues) line: 480 
InjectionMetadata.inject(Object, String, PropertyValues) line: 87   
AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(PropertyValues, PropertyDescriptor[], Object, String) line: 289  
DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).populateBean(String, RootBeanDefinition, BeanWrapper) line: 1185 
DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).doCreateBean(String, RootBeanDefinition, Object[]) line: 537 
DefaultListableBeanFactory(AbstractAutowireCapableBeanFactory).createBean(String, RootBeanDefinition, Object[]) line: 475   
AbstractBeanFactory$1.getObject() line: 304 
DefaultListableBeanFactory(DefaultSingletonBeanRegistry).getSingleton(String, ObjectFactory<?>) line: 228   
DefaultListableBeanFactory(AbstractBeanFactory).doGetBean(String, Class<T>, Object[], boolean) line: 300    
DefaultListableBeanFactory(AbstractBeanFactory).getBean(String) line: 195   
DefaultListableBeanFactory.preInstantiateSingletons() line: 700 
XmlWebApplicationContext(AbstractApplicationContext).finishBeanFactoryInitialization(ConfigurableListableBeanFactory) line: 760 
XmlWebApplicationContext(AbstractApplicationContext).refresh() line: 482    
DispatcherServlet(FrameworkServlet).configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext) line: 643   
DispatcherServlet(FrameworkServlet).createWebApplicationContext(ApplicationContext) line: 606   
DispatcherServlet(FrameworkServlet).createWebApplicationContext(WebApplicationContext) line: 657    
DispatcherServlet(FrameworkServlet).initWebApplicationContext() line: 525   
DispatcherServlet(FrameworkServlet).initServletBean() line: 466 
DispatcherServlet(HttpServletBean).init() line: 136 
DispatcherServlet(GenericServlet).init(ServletConfig) line: 160 
StandardWrapper.initServlet(Servlet) line: 1280 
StandardWrapper.loadServlet() line: 1193    
StandardWrapper.load() line: 1088   
StandardContext.loadOnStartup(Container[]) line: 5176   
StandardContext.startInternal() line: 5460  
StandardContext(LifecycleBase).start() line: 150    
ContainerBase$StartChild.call() line: 1559  
ContainerBase$StartChild.call() line: 1549  
FutureTask$Sync.innerRun() line: 334    
FutureTask<V>.run() line: 166   
ThreadPoolExecutor.runWorker(ThreadPoolExecutor$Worker) line: 1145  
ThreadPoolExecutor$Worker.run() line: 615   
Thread.run() line: 724  

降级到spring-data:1.3.0,解决了这个问题。我有一个类似的问题。 Spring data error

1 个答案:

答案 0 :(得分:2)

这是DataNucleus中的一个错误。如果当前没有加入,则From.getJoins()会返回null,因为您可以看到here。我已经向DataNucleus提交了bug并要求这些人返回一个空集合(这将是正确的行为)。

更新:安迪已经解决了这个问题并表示它将进入即将发布的3.3.6。同时,您可以获取夜间快照here