我正在尝试使用 jpa 2.0 和 derby 进行简单的持久化,但我不断收到 NullPointerException。
异常和实体类 - http://pastebin.com/QqXhRdcN
我试图坚持的代码
EntityManager em = emf.createEntityManager();
Course c = new Course("cse", 214, "fall", 2010);
em.getTransaction().begin();
em.persist(c);
em.getTransaction().commit();
em.close();
空指针异常发生在上述代码第一行的第171行。
提前致谢!
最佳答案
您的 EntityManagerFactory
在初始化时没有正确创建(因此它是 null
),因此当您尝试使用 NullPointerException
它获得一个 EntityManager
。
虽然 EclipseLink 在尝试为异常生成国际化消息时肯定不会因 NPE
而失败(您应该将其记录为错误),但跟踪仍然包含有用的信息,如中所示下面加粗:
[EL Severe]: 2010-10-23 05:27:14.092--ServerSession(1673653)--java.lang.NullPointerException
at java.util.ResourceBundle.getBundle(Unknown Source)
at org.eclipse.persistence.exceptions.i18n.ExceptionMessageGenerator.buildMessage(ExceptionMessageGenerator.java:47)
at <b>org.eclipse.persistence.exceptions.DatabaseException.unableToAcquireConnectionFromDriverException(DatabaseException.java:376)</b>
at org.eclipse.persistence.sessions.DefaultConnector.connect(DefaultConnector.java:91)
at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:579)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:228)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:380)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:157)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:214)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:202)
...
您的连接池配置很可能有问题,EclipseLink 无法从中获取有效连接。
如果没有关于您的设置的更多详细信息,例如 persistence.xml
、Tomcat 数据源配置、您使用 Derby 的方式(在服务器模式下?),我无法给出更准确的答案。但这就是要看的地方。
关于java - JPA 和 derby 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4003069/