DataSource不能为null

时间:2015-06-01 16:29:25

标签: java jpa spring-boot datasource entitymanager

我的应用程序不使用persistence.xml。所以我尝试使用LocalContainerEntityManagerFactoryBean来创建自定义EntityManager。但我得到错误,因为DataSource不能为null。任何人都可以帮助解决这个问题吗?

PersistenceJPAConfig.java

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories
public class PersistenceJPAConfig {

@Autowired
 private DataSource dataSource;

@Autowired
JpaVendorAdapter jpaVendorAdapter;

@Bean(name = "personEntityManager")
public EntityManager entityManager() {
    return entityManagerFactory().createEntityManager();
}

@Bean(name = "personEntityManagerFactory")
public EntityManagerFactory entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
        lef.setDataSource(dataSource);
        lef.setJpaVendorAdapter(jpaVendorAdapter);
        lef.setPackagesToScan("com.myc.cc.domain.Person");
        lef.setPersistenceUnitName("personPersistenceUnit");
        lef.afterPropertiesSet();
        return lef.getObject();
}

@Primary
@Bean(name = "personTransactionManager")
 public PlatformTransactionManager transactionManager() {
        return new JpaTransactionManager(entityManagerFactory());
    } 
}

SearchPersonPredicates.java

public static Predicate findIdByEM(final Long pId) { 
    PersistenceJPAConfig jpaConfig = new PersistenceJPAConfig();
    EntityManager manager=  jpaConfig.entityManager(); 
    QPerson qPerson = QPerson.person; 
    QAddress qAddress = QAddress.address;
    BooleanBuilder booleanBuilder = new BooleanBuilder();
    JPAQuery jpaQuery = new JPAQuery(manager);
    JPASubQuery subQuery = new JPASubQuery();
    Predicate predicate;  
    if (pId != null) {
        booleanBuilder.or(QAddress.person_no.eq(pId));
    } 
    predicate = booleanBuilder.getValue();  
    jpaQuery = jpaQuery.from(qAddress).join(qPerson).on(predicate);  
    subQuery.from(qPerson, qAddress); 
    return predicate;
}

SearchPersonServiceImpl.java

public List<Person> findPnumberbyEM(Long id) { 
    Iterable<Person> person = personRepository.findAll(findIdByEM(id));
    return constructList(person);
}

private List<Person> constructList(Iterable<Person> persons) {
    List<Person> list = new ArrayList<Person>();
    for (Person person : persons) {
        list.add(person);
    } 
}

错误

17:52:35.337 ERROR 7016 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/cc_dev] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: DataSource must not be null] with root cause

    java.lang.IllegalArgumentException: DataSource must not be null
        at org.springframework.util.Assert.notNull(Assert.java:112)
        at org.springframework.jdbc.datasource.lookup.SingleDataSourceLookup.<init>(SingleDataSourceLookup.java:40)
        at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.setDataSource(LocalContainerEntityManagerFactoryBean.java:238)
        at com.myc.cc.repository.PersistenceJPAConfig.entityManagerFactory(PersistenceJPAConfig.java:43)
        at com.myc.cc.repository.PersistenceJPAConfig.entityManager(PersistenceJPAConfig.java:37)
        at com.myc.cc.repository.SearchPersonPredicates.findIdByEM(SearchPersonPredicates.java:121)
        at com.myc.cc.service.impl.SearchPersonServiceImpl.findPnumberbyEM(SearchPersonServiceImpl.java:70)
        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.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201)
        at com.sun.proxy.$Proxy114.findPnumberbyEM(Unknown Source)
        at com.myc.cc.web.SearchPersonController.searchPerson(SearchPersonController.java:109)
        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)

提前致谢

1 个答案:

答案 0 :(得分:3)

您的datasource属性可能未初始化。尝试将@Value添加到datasource属性:

@Autowired
@Value("${property.reference}") 
private DataSource dataSource;