Hibernate抛出NullPointerException [英] Hibernate throwing NullPointerException

查看:112
本文介绍了Hibernate抛出NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用IntelliJ 12.1.3测试Hibernate 4.2.1.Final和JPA,我得到以下错误。



一切似乎都设置好了(我让IntelliJ生成映射),并且因为我没有Hibernate和JPA的经验,所以我对此感到不知所措为什么我在 EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(Heroku-production); 上获得NPE c $ c $?b
$ b $ p

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

错误:

  2013年5月16日下午3:02:49 org.hibernate.annotations.common.Version< clinit> 
INFO:HCANN000001:Hibernate Commons Annotations {4.0.1.Final}
2013年5月16日3:02:49 org.hibernate.Version logVersion
INFO:HHH000412:Hibernate Core {4.2 .1.Final}
2013年5月16日3:02:49 org.hibernate.cfg.Environment< clinit>
INFO:HHH000206:找不到hibernate.properties
org.hibernate.cfg.Environment buildBytecodeProvider
INFO:HHH000021:字节码提供程序名称:javassist
May 16,2013 3:02:54 PM org.hibernate.cfg.Configuration addResource
INFO:HHH000221:从资源读取映射:com / *** / heroku / CountryCodesEntity.xml
May 16 ,2013 3:02:54 PM org.hibernate.cfg.Configuration addResource
INFO:HHH000221:从资源读取映射:com / *** / heroku / StateCodesEntity.xml
2013年5月16日3: 02:54 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO:HHH000402:使用Hibernate内置连接池(不适合生产使用!)
2013年5月16日3: 02:54 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO:HHH000115:Hibernate连接池大小:20
2013年5月16日下午3:02:54 org.hibernate。 service.jdbc.connections.internal.DriverManage rConnectionProviderImpl configure
INFO:HHH000006:Autocommit模式:true
2013年5月16日下午3:02:54 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO:HHH000401:在URL上使用驱动程序[org.postgresql.Driver] [postgres:// *********:*********** @ ec2 - *** - ** - ** - *****。compute-1.amazonaws.com:6232/******]
2013年5月16日3:02:54 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl配置
INFO:HHH000046:连接属性:{user = ********,password = ****,autocommit = true,release_mode = auto}
线程mainjava中的异常.lang.NullPointerException
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:214)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl $ ConnectionProviderJdbcConnectionAccess。获取连接(JdbcServicesImpl.java:242)
在org.hibernate.engine.jdbc.int ernal.JdbcServicesImpl.configure(JdbcServicesImpl.java:117)
位于org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
位于org.hibernate.service.internal.AbstractServiceRegistryImpl。在org.hibernate.cfg.SettingsFactory.buildSettings中设置
(SettingsFactory.java:
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2293)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2289)
at org.hibernate.cfg.Configuration.buildSettingsInternal .hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1758)
位于org.hibernate.ejb.EntityManagerFactoryImpl。< init>(EntityManagerFactoryImpl.java:96)
位于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)
Main.main(Main.java:20)

Persistance.xml

 <?xml version = 1.0encoding =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>
<属性>
< property name =hibernate.connection.urlvalue =postgres:// ****:**** @ ec2 - *** - ** - *** - ***。 compute-1.amazonaws.com:6232/*****\"/>
< property name =hibernate.connection.driver_classvalue =org.postgresql.Driver/>
< property name =hibernate.connection.usernamevalue =****/>
< property name =hibernate.connection.passwordvalue =****/>
< property name =hibernate.archive.autodetectionvalue =class/>
< property name =hibernate.show_sqlvalue =true/>
< property name =hibernate.format_sqlvalue =true/>
< property name =hbm2ddl.autovalue =update/>
< / properties>
< / persistence-unit>


解决方案

该消息表明它无法连接到数据库。仔细检查您的连接字符串,并确保您的驱动程序位于类路径中。我也认为你需要将'jdbc'作为连接字符串的一部分,除非最近发生了变化。

 < property name =hibernate.connection.urlvalue =jdbc:postgres:// ****: **** @ EC2  -  ***  -  **  -  ***  -  *** compute-1.amazonaws.com:6232/*****\"/> 

编辑:我只是看了这个库的以前版本的源码,并且如果第214行是相同的b / t这两个版本你的连接肯定是空的(即没有发生)。

以下是我用过的一个版本中的 DriverManagerConnectionProviderImpl 的第214行:(4.2.0.CR2) p>

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

以下是这些行:

  198连接conn; 
199如果(driver!= null){
200 //如果驱动程序可用,则完全规避
201 // DriverManager#getConnection。它试图在使用驱动程序之前仔细检查
202 // ClassLoaders。这在
203 // OSGi环境中效果不好,没有不可靠的解决方法。
204 conn = driver.connect(url,connectionProps);
205}
206 else {
207 //如果没有驱动程序,则回退原始方法。
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}


I'm trying to test Hibernate 4.2.1.Final with JPA using IntelliJ 12.1.3 and I am getting the following error.

Everything seems to be setup OK (I let IntelliJ generate the mappings), and as I have zero experience with Hibernate and JPA I am at a loss as to why this is occurring.

Why am I getting an NPE on EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("Heroku-production");?

main class:

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();
    }

Error:

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>

解决方案

That message indicates to me that its having trouble connecting to the db. Double check your connection string, and make sure your driver is on the classpath. I also think you need to have 'jdbc' as part of your connection string unless that's changed recently.

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

Edit: I just eyeballed the source of previous version of this library, and if line 214 is the same b/t those two versions your connection is definitely null (i.e. not happening).

Here is line 214 of DriverManagerConnectionProviderImpl in a version i have handy: (4.2.0.CR2)

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

Here are the preceding lines:

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     }

这篇关于Hibernate抛出NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆