我目前在 netbeans 中使用 java 开发 Web 应用程序。我将 JPA2.0 与 H2 数据库一起使用。
问题是,当我更改数据库中的数据并在浏览器中重新加载页面时=> 未设置任何更改。仅当我在 netbeans
中重新运行 jsp
页面,或者我重新启动 tomcat 时:
web.xml:
ajoutAutomobile
controlleurs.ajoutAutomobile
ajoutConducteur
controlleurs.ajoutConducteur
ajoutMission
controlleurs.ajoutMission
ajoutUtilisateur
controlleurs.ajoutUtilisateur
selectCinConducteur
controlleurs.selectCinConducteur
selectIdAuto
controlleurs.selectIdAuto
selectIdAutomobile
controlleurs.selectIdAutomobile
ajoutVignette
controlleurs.ajoutVignette
selectImei
controlleurs.selectImei
ajoutAssurance
controlleurs.ajoutAssurance
ajoutVisite
controlleurs.ajoutVisite
ajoutIntervention
controlleurs.ajoutIntervention
selectAutoLibre
controlleurs.selectAutoLibre
selectConducteurLibre
controlleurs.selectConducteurLibre
afficheInfoGeo
controlleurs.afficheInfoGeo
ajoutAffectation
controlleurs.ajoutAffectation
afficheListConducteur
controlleurs.afficheListConducteur
afficheInfoGeoRestreinte
controlleurs.afficheInfoGeoRestreinte
selectDate
controlleurs.selectDate
selectCoordsRestreinte
controlleurs.selectCoordsRestreinte
selectAssuranceExpir
controlleurs.selectAssuranceExpir
selectVignetteExpir
controlleurs.selectVignetteExpir
selectVisiteExpir
controlleurs.selectVisiteExpir
selectAssurance
controlleurs.selectAssurance
selectVignette
controlleurs.selectVignette
selectVisite
controlleurs.selectVisite
afficheListAutomobile
controlleurs.afficheListAutomobile
historiqueAffectationConducteur
controlleurs.historiqueAffectationConducteur
historiqueAffectationAutomobile
controlleurs.historiqueAffectationAutomobile
ajoutAutomobile
/ajoutAutomobile
ajoutConducteur
/ajoutConducteur
ajoutMission
/ajoutMission
ajoutUtilisateur
/ajoutUtilisateur
selectCinConducteur
/selectCinConducteur
selectIdAuto
/selectIdAuto
selectIdAutomobile
/selectIdAutomobile
ajoutVignette
/ajoutVignette
selectImei
/selectImei
ajoutAssurance
/ajoutAssurance
ajoutVisite
/ajoutVisite
ajoutIntervention
/ajoutIntervention
selectAutoLibre
/selectAutoLibre
selectConducteurLibre
/selectConducteurLibre
afficheInfoGeo
/afficheInfoGeo
ajoutAffectation
/ajoutAffectation
afficheListConducteur
/afficheListConducteur
afficheInfoGeoRestreinte
/afficheInfoGeoRestreinte
selectDate
/selectDate
selectCoordsRestreinte
/selectCoordsRestreinte
selectAssuranceExpir
/selectAssuranceExpir
selectVignetteExpir
/selectVignetteExpir
selectVisiteExpir
/selectVisiteExpir
selectAssurance
/selectAssurance
selectVignette
/selectVignette
selectVisite
/selectVisite
afficheListAutomobile
/afficheListAutomobile
historiqueAffectationConducteur
/historiqueAffectationConducteur
historiqueAffectationAutomobile
/historiqueAffectationAutomobile
30
index.jsp
服务器.xml:
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
connectionTimeout="20000"
redirectPort="8443" />
resourceName="UserDatabase"/>
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
请问如何解决这个问题?
biên tập:我尝试删除缓存,也在其他浏览器中打开页面 => 结果相同..
编辑 2:持久性.xml:
org.eclipse.persistence.jpa.PersistenceProvider
entities.Automobile
entities.Intervention
entities.Assurance
entities.Coordonnees
entities.Mission
entities.Affectation
entities.Conducteur
entities.Gps
entities.Vignette
entities.Utilisateur
entities.VisiteTechnique
false
我的小服务程序:
response.setContentType("application/json");
PrintWriter out = response.getWriter();
EntityManagerFactory emf=Persistence.createEntityManagerFactory("webPU");
Timestamp nd=new Timestamp(System.currentTimeMillis());//now
nd.setMinutes(nd.getMinutes()-20);
EntityManager em=emf.createEntityManager();
String req="select a from Automobile a where a.gpsImei.imei in (SELECT c.gpsImei.imei FROM Coordonnees c WHERE c.date < :nd )";
Query resultat=em.createQuery(req);
resultat.setParameter("nd", nd);
java.util.List la= resultat.getResultList();
Gson gson=new Gson();
String json =gson.toJson(La);//convert the List to json
thử {
out.println(json);//return respons to ajax
} finally {
out.close();
}
因为您正在使用 transaction-type="RESOURCE_LOCAL"
您总是希望只使用一个 EntityManager。有一个父类在您的应用程序中管理相同的 EntityManager 实例。
It is almost never a good idea to have more than one instance of an EntityManager in use (don't create a second one unless you've destroyed the first)
我认为问题在于在这里创建一个新的 EM
EntityManager em=emf.createEntityManager();
就个人而言,我更喜欢使用事务类型,transaction-type="TRANSACTION",而不用担心实体管理器https://stackoverflow.com/a/1963012/643500
您可以在此处阅读更多信息 http://openejb.apache.org/jpa-concepts.html
保存数据时是使用saveOrUpdate还是merge?
这可能不是正确的答案,但希望对您有所帮助。
Tôi là một lập trình viên xuất sắc, rất giỏi!