尝试在数据存储区中添加对象(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();
}
}
打印的最后一条消息是:到目前为止,实体管理器为空。 因此,我的假设是出现了问题:
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>
我不知道为什么我的实体管理器没有创建。我一直在使用这个代码并且它有效,但现在它没有,我不知道我错过了什么。我真的很感激有关这方面的任何想法。
谢谢。