我正在研究这个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文件夹中,是否需要在编译期间移动,包含或引用?
答案 0 :(得分:1)
首先编辑:
java.lang.NullPointerException at myRepository.DatabaseNameContext.getDecUpdates(DatabaseNameContext.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时遇到了问题。尝试选择“将所需库复制到子文件夹”选项。