访问Autowired Repository时出现NullPointerException

时间:2015-09-01 17:16:16

标签: java spring jpa

Java JPA配置文件的相关部分

    @Bean(name = "dataSource")
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("oracle.jdbc.OracleDriver");
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean(name = "transManager") @DependsOn("emf")
    public PlatformTransactionManager transactionManager() {
        return new JpaTransactionManager(entityManagerFactory().getObject());
    }

    @Bean
    public HibernateExceptionTranslator hibernateExceptionTranslator() {
        return new HibernateExceptionTranslator();
    }

    @Bean(name = "emf") @DependsOn("dataSource")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();

        vendorAdapter.setShowSql(true);
        vendorAdapter.setGenerateDdl(false);
        vendorAdapter.setDatabase(Database.ORACLE);
        vendorAdapter.setDatabasePlatform("org.hibernate.dialect.Oracle9Dialect");

        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setPackagesToScan(packagesToScan);
        factory.setDataSource(dataSource());
        factory.afterPropertiesSet();

        return factory;
        }
@Bean
    public AppWindow mainBean() {
        return new AppWindow();
    }

我如何连接存储库:

@Autowired
private SomeRepository someRepository;

上下文:创建使用Spring Data进行数据库管理的Java 7 SE程序。

问题:我在尝试访问存储库时不断收到NullPointerException。

堆栈追踪:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mainBean' defined in config.JpaConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [app.AppWindow]: Factory method 'mainBean' threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
at app.App.main(App.java:12)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [app.AppWindow]: Factory method 'mainBean' threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 12 more
Caused by: java.lang.NullPointerException
    at app.AppWindow.loadFromDB(AppWindow.java:355)
    at app.AppWindow.<init>(AppWindow.java:88)
    at config.JpaConfig.mainBean(JpaConfig.java:71)
    at config.JpaConfig$$EnhancerBySpringCGLIB$$2699e960.CGLIB$mainBean$2(<generated>)
    at config.JpaConfig$$EnhancerBySpringCGLIB$$2699e960$$FastClassBySpringCGLIB$$d50a1d29.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:318)
    at config.JpaConfig$$EnhancerBySpringCGLIB$$2699e960.mainBean(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 13 more

发生NullPointerException的方法:

private List<SomeObject> loadFromDB()
    {return someRepository.retrieveAllByCodeLength(5);}

存储库代码:

public interface SomeRepository extends JpaRepository<SomeObject, Integer> {

    @Query(" from SomeObject o where o.code = :code")
    SomeObject findByCode(int code);

    @Query(" from SomeObject o where o.description = :description")
    SomeObject findByDescription(String description);

    @Query(" from SomeObject o where LENGTH(o.code) = :length ORDER BY o.description")
    List<SomeObject> retrieveAllByCodeLength(int length);
}

1 个答案:

答案 0 :(得分:0)

修正了它;为entityManagerFactory更改了emf。还必须在Repository查询中添加@Param。稍后将使用更正的代码进行更新。