当前位置: 首页 > 面试题库 >

hibernate引发NullPointerException

公孙阳文
2023-03-14
问题内容

我正在尝试使用IntelliJ 12.1.3使用JPA测试Hibernate 4.2.1.Final,但出现以下错误。

一切似乎都设置正确(我让IntelliJ生成了映射),由于我对Hibernate和JPA的经验为零,所以我无所适从。

为什么要启用NPEEntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("Heroku-production");

主类:

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>

问题答案:

该消息向我表明它无法连接到数据库。仔细检查您的连接字符串,并确保您的驱动程序在类路径中。我还认为您需要将“
jdbc”作为连接字符串的一部分,除非最近进行了更改。

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

编辑:我只是盯着这个库的先前版本的来源,如果第214行与这两个版本相同,则您的连接肯定为空(即未发生)。

这是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     }


 类似资料:
  • 问题内容: 有人看过此消息吗? 在Hibernate论坛上进行了一些讨论,但对于问题可能是什么却不太清楚。 我们正在使用JPA和Spring 3.0.5运行Hibernate 3.6.9。这个异常只是在两个版本之间弹出(就像一天前一样,今天在部署时会引起问题)。这可能与某些错误配置的orm文件有关吗?我们为模型提供了orm.xml文件,为命名查询提供了文件。 问题答案: 因此,我们发现了问题。不幸

  • 问题内容: 我正在使用Spring Boot,Spring Data REST,Hibernate(5.2.12.Final),并将存储库公开为REST服务。 我有一个引发NullPointerException的查询问题,我不明白原因。我正在通过HTTP请求调用此方法(Spring Data REST公开了此方法)。 这是代码: 这是堆栈跟踪: 我确实没有发现该查询有任何问题。我是在做错什么还是

  • departamentdaoimpl.java departamentservice.java DepartamentServiceImpl.java

  • 问题内容: 我有一个Web应用程序,该应用程序具有搜索表单,并且HQL是即时生成的。此外,用户可以单击列标题以根据需要对项目进行排序。一些列从非常深的结构中获取其他数据。 例如,我有这个HQL,它可以完美地工作: 但是这一失败惨败: 有人可以指出,我要去哪里错了。不支持这种深层语法吗? hibernate版本是3.2.1。 抱歉,忘记了堆栈跟踪: 问题答案: 正如@JBNizet正确指出的那样,问

  • 我试图在grails域对象上执行sql语句。当直接在db(h2)上执行时,is工作正常。 通过hibernate,我得到: 没有节点的数据类型:org.hibernate.hql.internal.ast.tree.MethodNode-[METHOD_CALL]MethodNode:'('-[METHOD_NAME]IdentNode:'to_char'{originalText=to_char

  • 我正在运行Hibernate 4.2.6。Final/JPA2,并尝试创建一个由@embedded对象组成的@ElementCollection列表,这些对象本身包含一个字符串的@ElementCollection列表。 但是,当尝试实例化EntityManager时,hibernate会抛出一个ContoptModificationException,我一点也不明白。 如果我注释掉我的