【问题标题】:JPA connection works in Eclipse but gives NullPointerException when compiledJPA 连接在 Eclipse 中工作,但在编译时给出 NullPointerException
【发布时间】:2018-05-28 12:19:59
【问题描述】:

我正在开发这个 Java 项目,但是我是一名 C# 开发人员,并且对环境完全陌生,所以如果这里有任何非常基本的错误,我深表歉意,我完全愿意!

基本上,我的 JPA 连接在 Eclipse 中运行时工作正常,但一旦我编译为 .jar,我就会得到 java.lang.NullPointerException。

这是我的代码:

public List<DecUpdate> getDecUpdates() {

    EntityManagerFactory conn = null;
    EntityManager db = null;

    try {

        // Connect:
        conn = Persistence.createEntityManagerFactory("DatabaseName");
        db = conn.createEntityManager();            

        // Get entities:
        String queryString = "select d from DecUpdate d "
                            + "where d.Requested >= :from "
                            + "and d.Completed is null "
                            + "and d.Requested = "
                                + "(select max(d1.Requested) from DecUpdate d1 "
                                    + "where d.Requested >= :from "
                                    + "and d1.Completed is null "
                                    + "and d1.GroupId = d.GroupId "
                                    + "and d1.ServiceId = d.ServiceId) "
                            + "and not exists "
                                + "(select d2 from DecUpdate d2 "
                                    + "where d.Requested >= :from "
                                    + "and d2.Requested > d.Requested "
                                    + "and d2.GroupId = d.GroupId "
                                    + "and d2.ServiceId = d.ServiceId) ";

        Query query = db.createQuery(queryString);
        query.setParameter("from", new Timestamp(new DateTime().minusHours(3).getMillis()));

        List<DecUpdate> resultList = query.getResultList();

        return resultList;
    }
    catch (Exception e) {

        throw e;
    }
    finally { // Close context and connection:

        db.close();
        conn.close();
    }
}

这是我的 persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
   <persistence-unit name="DatabaseName" transaction-type="RESOURCE_LOCAL">
       <exclude-unlisted-classes>false</exclude-unlisted-classes>
       <properties>
           <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
           <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://1.1.1.1:1111;DatabaseName=DatabaseName"></property>
           <property name="javax.persistence.jdbc.user" value="foo"></property>
           <property name="javax.persistence.jdbc.password" value="bar"></property>

           <!-- EclipseLink should create the database schema automatically -->
           <property name="eclipselink.ddl-generation" value="create-tables" />
           <property name="eclipselink.ddl-generation.output-mode" value="database" />
       </properties>
   </persistence-unit>
</persistence>

我已经尽可能多地阅读了该主题,但我无法弄清楚为什么它在开发环境中有效,但在编译时却无效。我怀疑我缺乏一些我正在阅读的所有教程都假定的一些基本知识,但由于我是自学的,所以我没有这些知识。

有人愿意指出我的菜鸟错误吗?

编辑:

感谢@ThomasEdwin 的一些调查,我发现了一个更有帮助的错误:

org.eclipse.persistence.exceptions.PersistenceUnitLoadingException: 
Exception Description: An exception was thrown while trying to load persistence unit at url: rsrc:../
Internal Exception: Exception [EclipseLink-30004] (Eclipse Persistence Services - 2.7.0.v20170811-d680af5): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while processing persistence.xml from URL: rsrc:../
Internal Exception: java.net.MalformedURLException
    at...

在我看来,我好像错误地引用了我的 persistence.xml 还是什么?它在 src/META-INF 文件夹中,是否需要在编译过程中移动、包含或引用某处?

【问题讨论】:

  • 你是否添加了 com.microsoft.sqlserver.jdbc.SQLServerDriver 的依赖项
  • 堆栈跟踪是什么?
  • @vts :在添加依赖项方面,请原谅我的无知,但我不是 100% 确定你的意思。如果这就是您的意思,我在编译时勾选了“将所需的库打包到生成的 JAR 中”?
  • 是的..正是..
  • @ThomasEdwin:堆栈跟踪不是很有趣:java.lang.NullPointerException at myRepository.DatabaseNameContext.getDecUpdates(DatabaseNam‌​eContext.java:63) at Main$1.run(Main.java:51 ) 在 java.util.TimerThread.mainLoop(Unknown Source) 在 java.util.TimerThread.run(Unknown Source) - 基本上错误在我发布的代码中的某个地方

标签: java eclipse jpa eclipselink


【解决方案1】:

第一次编辑:

java.lang.NullPointerException at myRepository.DatabaseNameContext.getDecUpdates(DatabaseNam‌​‌​eContext.java:63)

  at Main$1.run(Main.java:51)
  at java.util.TimerThread.mainLoop(Unknown Source)
  at java.util.TimerThread.run(Unknown Source)

NPE 掩盖了实际错误。修改第63行为if (db != null) db.close();

第二次编辑:

org.eclipse.persistence.exceptions.PersistenceUnitLoadingException: 
Exception Description: An exception was thrown while trying to load persistence unit at url: rsrc:../
Internal Exception: Exception [EclipseLink-30004] (Eclipse Persistence Services - 2.7.0.v20170811-d680af5):
org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while processing persistence.xml from URL: rsrc:../
Internal Exception: java.net.MalformedURLException
    at...

正如eclipselink PersistenceUnitLoadingEception in executable JAR 中所讨论的,EclipseLink 在处理可执行jar 时存在问题。尝试选择选项“将所需的库复制到子文件夹中”。

【讨论】:

  • 这是完美的。非常感谢您的帮助,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多