代码之家  ›  专栏  ›  技术社区  ›  technophebe

JPA连接在Eclipse中工作,但在编译时提供NullPointerException

  •  1
  • technophebe  · 技术社区  · 7 年前

    我正在从事这个Java项目,但我是一名C#开发人员,对环境完全陌生,因此,如果这里有任何真正的基本错误,我深表歉意,我完全同意!

    基本上,当在Eclipse中运行时,我的JPA连接工作正常,但只要我编译到。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();
        }
    }
    

    这就是我的坚持。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>
    

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

    有人愿意指出我的新手错误吗?

    编辑:

    多亏了@Thomasewin的一些调查,我发现了一个更有用的错误:

    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...
    

    在我看来,这就像我引用了我的持久性。xml不正确还是什么?它位于src/META-INF文件夹中,是否需要在编译期间将其移动、包含或引用?

    1 回复  |  直到 7 年前
        1
  •  1
  •   ThomasEdwin    7 年前

    第一次编辑:

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