2013-05-16 174 views
2

我想用JPA使用IntelliJ 12.1.3測試Hibernate 4.2.1.Final,並且出現以下錯誤。Hibernate拋出空指針異常

似乎一切都安裝好了(我讓IntelliJ生成映射),並且因爲我沒有Hibernate和JPA的經驗,所以我不知道爲什麼會發生這種情況。

爲什麼我會在EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("Heroku-production");上獲得NPE?

主類:

public static void main(final String[] args) throws Exception { 
     EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("Heroku-production"); 
     EntityManager entityManager = entityManagerFactory.createEntityManager(); 
     entityManager.getTransaction().begin(); 
     List<CountryCodesEntity> result = entityManager.createQuery("from CountryCodesEntity", CountryCodesEntity.class).getResultList(); 
     for (CountryCodesEntity countryCodesEntity : result) { 
      System.out.println("Code (" + countryCodesEntity.getCode() + ") : " + countryCodesEntity.getNsenum()); 
     } 
     entityManager.getTransaction().commit(); 
     entityManager.close(); 
    } 

錯誤:

May 16, 2013 3:02:49 PM org.hibernate.annotations.common.Version <clinit> 
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final} 
May 16, 2013 3:02:49 PM org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {4.2.1.Final} 
May 16, 2013 3:02:49 PM org.hibernate.cfg.Environment <clinit> 
INFO: HHH000206: hibernate.properties not found 
May 16, 2013 3:02:49 PM org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
May 16, 2013 3:02:54 PM org.hibernate.cfg.Configuration addResource 
INFO: HHH000221: Reading mappings from resource: com/***/heroku/CountryCodesEntity.xml 
May 16, 2013 3:02:54 PM org.hibernate.cfg.Configuration addResource 
INFO: HHH000221: Reading mappings from resource: com/***/heroku/StateCodesEntity.xml 
May 16, 2013 3:02:54 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!) 
May 16, 2013 3:02:54 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000115: Hibernate connection pool size: 20 
May 16, 2013 3:02:54 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000006: Autocommit mode: true 
May 16, 2013 3:02:54 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000401: using driver [org.postgresql.Driver] at URL [postgres://*********:***********@ec2-***-**-**-*****.compute-1.amazonaws.com:6232/******] 
May 16, 2013 3:02:54 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000046: Connection properties: {user=********, password=****, autocommit=true, release_mode=auto} 
Exception in thread "main" java.lang.NullPointerException 
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:214) 
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:242) 
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:117) 
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) 
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:78) 
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2293) 
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2289) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1758) 
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96) 
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:913) 
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:898) 
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:59) 
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63) 
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) 
    at Main.main(Main.java:20) 

Persistance.xml

<?xml version="1.0" encoding="UTF-8"?> 

<persistence-unit name="Heroku-production"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <mapping-file>com/***/heroku/CountryCodesEntity.xml</mapping-file> 
    <mapping-file>com/***/heroku/StateCodesEntity.xml</mapping-file> 
    <properties> 
     <property name="hibernate.connection.url" value="postgres://****:****@ec2-***-**-***-***.compute-1.amazonaws.com:6232/*****"/> 
     <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/> 
     <property name="hibernate.connection.username" value="****"/> 
     <property name="hibernate.connection.password" value="****"/> 
     <property name="hibernate.archive.autodetection" value="class"/> 
     <property name="hibernate.show_sql" value="true"/> 
     <property name="hibernate.format_sql" value="true"/> 
     <property name="hbm2ddl.auto" value="update"/> 
    </properties> 
</persistence-unit> 

回答

5

該消息表明它無法連接到數據庫。仔細檢查您的連接字符串,並確保您的驅動程序位於類路徑中。我也認爲你需要將'jdbc'作爲連接字符串的一部分,除非最近發生了變化。

<property name="hibernate.connection.url" value="jdbc:postgres://****:****@ec2-***-**-***-***.compute-1.amazonaws.com:6232/*****"/> 

編輯:我只是直勾勾的以前版本庫的來源,如果線214是一樣的B/T這兩個版本的連接肯定是空(即沒有發生)。

以下是在一個版本我已​​經得心應手DriverManagerConnectionProviderImpl線214:(4.2.0.CR2)

214  if (conn.getAutoCommit() != autocommit) { 
215   conn.setAutoCommit(autocommit); 
216  } 

下面是前面行:

198  Connection conn; 
199  if (driver != null) { 
200   // If a Driver is available, completely circumvent 
201   // DriverManager#getConnection. It attempts to double check 
202   // ClassLoaders before using a Driver. This does not work well in 
203   // OSGi environments without wonky workarounds. 
204   conn = driver.connect(url, connectionProps); 
205  } 
206  else { 
207   // If no Driver, fall back on the original method. 
208   conn = DriverManager.getConnection(url, connectionProps); 
209  } 
210  
211  if (isolation != null) { 
212   conn.setTransactionIsolation(isolation.intValue()); 
213  } 
214  if (conn.getAutoCommit() != autocommit) { 
215   conn.setAutoCommit(autocommit); 
216  } 
+0

我添加JDBC到連接字符串(這是我的一個遺漏),但沒有運氣。我會看看源代碼。 –

+0

兩者之間的來源是相同的。我將改變連接字符串並查看導致我的位置 –

+0

Port是否被阻塞? – zmf