java - JPA 连接在 Eclipse 中工作,但在编译时给出 NullPointerException

标签 java eclipse jpa eclipselink

我正在开发这个 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>

我已经尽可能多地阅读了这个主题,但我不明白为什么这在开发环境中有效,但在编译时却不起作用。我怀疑我缺乏一些我正在阅读的所有教程都假定的基本知识,但自从我 self 训练以来我就没有这些知识。

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

编辑:

感谢@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 文件夹中,是否需要在编译过程中移动、包含或引用某处?

最佳答案

第一次编辑:

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 时存在问题。尝试选择选项“将所需的库复制到子文件夹中”。

关于java - JPA 连接在 Eclipse 中工作,但在编译时给出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47812861/

相关文章:

java - 如何防止直接 URL 访问 Android 应用程序中使用的文件?

java - 读取 XML 元素的内部 XML

java - 如何使用 ModuleConfiguration.java 配置 Spring XD 模块?

java - 每当我向项目添加外部 JAR 文件时,Eclipse 都会在项目文件夹图标上显示警告标志

java - 无法通过 @Query 将集合传递给构造函数

java - HashMap 重新哈希会处理修改后的键吗?

android - android 的 scala 应用程序如何减小文件大小?

android - Galaxy S2 未在 Eclipse Mac 中显示

java - JPA:我可以强制将对象按照对象发送到 persist() 的顺序持久化到数据库吗?

java - 使用一个 JPQL 查询连接多个表