我正在尝试使用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行,这是上述代码的第一行。
提前致谢!
答案 0 :(得分:1)
您的EntityManagerFactory
在初始化时未正确创建(因此为null
),因此当您尝试使用NullPointerException
获取EntityManager
时NPE
虽然在尝试为异常生成国际化消息时,EclipseLink肯定不会因[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 org.eclipse.persistence.exceptions.DatabaseException.unableToAcquireConnectionFromDriverException(DatabaseException.java:376)
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)
...
而失败(您应该将其记录为错误),但跟踪仍然包含有用的信息,如下面的粗体所示:
persistence.xml
您的连接池配置很可能出现问题,EclipseLink无法从中获取有效连接。
如果没有关于您的设置的更多详细信息,我无法提供更准确的答案,例如{{1}},Tomcat数据源配置,使用Derby的方式(在服务器模式下?)。但那就是在哪里看。