2016-08-04 66 views
1

我對Hibernate和JPA的Spring應用程序使用了RepositoryConfig類,我得到了NullPointerException。我爲sping-mvc應用程序使用了相同的配置文件,並沒有遇到任何問題。在創建EnitityManagerFactory時出現NullPointerException

我試過將我的Hibernate降級到早期版本,但問題仍然存在。

RepositoryConfig.java

package com.csms.config; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.config.BeanPostProcessor; 
import org.springframework.context.annotation.*; 
import org.springframework.core.env.Environment; 
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; 
import org.springframework.jdbc.datasource.DriverManagerDataSource; 
import org.springframework.orm.jpa.JpaTransactionManager; 
import org.springframework.orm.jpa.JpaVendorAdapter; 
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 
import org.springframework.orm.jpa.vendor.Database; 
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; 
import org.springframework.transaction.PlatformTransactionManager; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 

import javax.persistence.EntityManagerFactory; 
import javax.sql.DataSource; 
import java.util.Properties; 

@Configuration 
@ComponentScan(basePackages = "com.csms") 
@PropertySource(value = "classpath:db.properties") 
@EnableTransactionManagement 
public class RepositoryConfig { 

    @Autowired 
    private Environment env; 

    @Bean 
    public DataSource dataSource() { 
     DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
     dataSource.setDriverClassName(env.getProperty("db.driver")); 
     dataSource.setUrl(env.getProperty("db.url")); 
     dataSource.setUsername(env.getProperty("db.user")); 
     dataSource.setPassword(env.getProperty("db.password")); 
     return dataSource; 
    } 

    @Bean 
    public JpaVendorAdapter jpaVendorAdapter() { 
     HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter(); 
     adapter.setShowSql(true); 
     adapter.setGenerateDdl(true); 
     adapter.setDatabase(Database.SQL_SERVER); 
     adapter.setDatabasePlatform("org.hibernate.dialect.SQLServer2012Dialect"); 
     return adapter; 
    } 

    @Bean 
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(
      DataSource dataSource, JpaVendorAdapter jpaVendorAdapter 
    ) { 
     Properties props = new Properties(); 
     props.setProperty("hibernate.format_sql", String.valueOf(true)); 

     LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean(); 
     emf.setDataSource(dataSource); 
     emf.setJpaVendorAdapter(jpaVendorAdapter); 
     emf.setPackagesToScan("com.csms.entities"); 
     emf.setJpaProperties(props); 
     return emf; 
    } 

    @Bean 
    public BeanPostProcessor persistenceTranslation() { 
     return new PersistenceExceptionTranslationPostProcessor(); 
    } 

    @Bean 
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { 
     return new JpaTransactionManager(emf); 
    } 
} 

的pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.team6</groupId> 
    <artifactId>c-sms</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-beans</artifactId> 
      <version>4.3.0.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-core</artifactId> 
      <version>5.2.1.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hibernate.javax.persistence</groupId> 
      <artifactId>hibernate-jpa-2.0-api</artifactId> 
      <version>1.0.1.Final</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-jdbc</artifactId> 
      <version>4.3.0.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>commons-dbcp</groupId> 
      <artifactId>commons-dbcp</artifactId> 
      <version>1.4</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>4.3.0.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>net.sourceforge.jtds</groupId> 
      <artifactId>jtds</artifactId> 
      <version>1.3.1</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
      <version>4.3.0.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.12</version> 
     </dependency> 
     <dependency> 
      <groupId>org.hamcrest</groupId> 
      <artifactId>hamcrest-library</artifactId> 
      <version>1.3</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-test</artifactId> 
      <version>4.3.0.RELEASE</version> 
     </dependency> 

    </dependencies> 
</project> 

堆棧跟蹤的一些小細節

SEVERE: Caught exception while allowing TestExecutionListener [org.springframewor[email protected]60addb54] to prepare test instance [[email protected]] 
     java.lang.IllegalStateException: Failed to load ApplicationContext 
      at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) 
      at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) 



java.lang.IllegalStateException: Failed to load ApplicationContext 
      at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) 

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactoryBean' defined in com.csms.config.RepositoryConfig: Invocation of init method failed; nested exception is java.lang.NullPointerException

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactoryBean' defined in com.csms.config.RepositoryConfig: Invocation of init method failed; nested exception is java.lang.NullPointerException 
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578) 

Full Stack traces is on Git and please check here

+0

您是否嘗試過使用不同版本的spring-orm?嘗試使用一個最高版本(可能是最固定的)。這一個:https://mvnrepository.com/artifact/org.springframework/spring-orm/3.2.17.RELEASE – Hrabosch

+0

@Hrabosch爲什麼3?正如你從pom.xml中看到的,OP使用Spring 4.x –

+0

你的'hibernate-entitymanager'依賴在哪裏? –

回答

0

你需要一個hibernate-entitymanager依賴添加到您的pom.xml。

+0

我很確定hibernate-core最新版本完全取代hibernate-entitymanager。它適用於我的其他項目。我也嘗試過添加hibernate-entitymanager,沒有工作 –

+0

對於hibernate-entitiy管理器:'(不推薦使用hibernate-core)'從http://www.mvnrepository.com/artifact/org.hibernate/hibernate- entitymanager,所以@ user3354205是對的 – Hrabosch

相關問題