尝试在数据存储中添加对象 (JPA) 时遇到问题。我正在将其部署到 App Engine。我尝试执行一些非常基本的调试,因为我是 Web 开发的新手,通过尝试在我认为可能出现问题的每个步骤之前和之后执行打印。显然,问题似乎是没有创建实体管理器,因此(显然)没有执行数据库中的添加。这是我的代码:
// all the parameters are previously initialized.
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
Symptoms aSymptoms = new Symptoms(thePatientId,theHeadacke,theMorningHeadacke,theDiziness,theFatigue,theMuscularFatigue,
theTinitus,theVisualImpairment,theWeightLoss,theHeartPalpitation,thePallor,theWeightProblems,theUpperBodyObesity,
theBrutalIncreaseDiastolic,theLumbarPain,theFever,thePolyuria,theSweating,thePositionChangeDiziness);
// using a SymptomsDAOJPA object I can perform add in the datastore.
SymptomsDAO dao = new SymptomsDAOJPA();
try {
// Save the symptom.
out.println("Entered try in doPost.");
dao.add(aSymptoms);
resp.setStatus(HttpServletResponse.SC_OK);
out.println("Saved symptom.");
out.println(aSymptoms);
} catch (Exception e) {
resp.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
out.println("Problem saving symptom.");
}
}
add(sSymptom) 方法如下:
@Override
public void add(Symptoms setSymptoms) {
EntityManager em = null;
System.out.println("Entity Manager is null so far.");
try {
em = EMF.get().createEntityManager();
System.out.println("Created Entity Manager.");
SymptomsJPA setSymtomsJPA = new SymptomsJPA(setSymptoms);
System.out.println("Created SymptomsJPA object to be added in the database.");
em.persist(setSymtomsJPA);
} finally {
em.close();
}
}
打印的最后一条消息是:Entity Manager 到目前为止为空。因此,我的假设是这就是问题所在:
em = EMF.get().createEntityManager();
这里有我的 EMF 课程:
public final class EMF {
private static final EntityManagerFactory emfInstance = Persistence
.createEntityManagerFactory("transactions-optional");
private EMF() {
// intentionally empty
}
public static EntityManagerFactory get() {
return emfInstance;
}
}
我的persistance.xml(部分)是:
<persistence-unit name="transactions-optional">
<provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
<properties>
<property name="datanucleus.NontransactionalRead" value="true"/>
<property name="datanucleus.NontransactionalWrite" value="true"/>
<property name="datanucleus.ConnectionURL" value="appengine"/>
</properties>
</persistence-unit>
我不知道为什么没有创建我的实体管理器。我一直在使用这段代码并且它有效,但现在它没有,我不知道我错过了什么。我真的很感激对此的任何想法。
谢谢你。