Adrian Ber
Adrian Ber

Reputation: 21380

Spring Data repository throws NPE on creation

I have the following bean

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

    @Id @GeneratedValue
    private Long id;

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

then I declared the repository

public interface ChildRepository extends CrudRepository<Child, Long> {

    List<Child> findByParent(Parent parent);

}

which is autowired in the controller

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

Starting the server I get the following error:

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)
...

If I remove the method findByParent everything is fine. What could be the problem?

I'm using spring-data-jpa:1.4.2.RELEASE and datanucleus-accessplatform-jpa-rdbms:3.3.4.

This is the full stack

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  

Downgrading to spring-data:1.3.0, solves the problem. I had a similar issue. Spring data error

Upvotes: 0

Views: 568

Answers (1)

Oliver Drotbohm
Oliver Drotbohm

Reputation: 83141

This is a bug in DataNucleus. From.getJoins() returns null if no join is currently present, as you can see here. I've filed a bug with DataNucleus and asked the guys to return an empty collection instead (which would be the correct behavior).

UPDATE: Andy already fixed the issue and stated it'll make it into the soon to be released 3.3.6. Meanwhile, you can grab nightly snapshots here.

Upvotes: 2

Related Questions