gpt4 book ai didi

java - 如何从 JPA resultList 获取更新的实体?

转载 作者:太空宇宙 更新时间:2023-11-04 12:23:07 24 4
gpt4 key购买 nike

我已经更新了事务中的一个对象,它已成功更新并与数据库同步...但是每当我使用 query.getResultList() 检索实体时,它都不会返回包含更新的对象和值的列表。

这里的代码指的是我在做什么......

    javax.persistence.EntityManager entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("jdbc:oracle:thin:@Siddhant-PC:1521:XEPU").createEntityManager(); 
javax.persistence.Query professionalWorkQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT p FROM ProfessionalWork p order by p.jobNo");
java.util.List<ProfessionalWork> professionalWorkList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : professionalWorkQuery.getResultList();

用于更新对象的代码...

ProfessionalWork proWork = entityManager.find(ProfessionalWork.class, jobNo);

entityManager.getTransaction().begin();
proWork.setStatus("Ready");
proWork.setReadyTime(DateWrapper.getTime());
entityManager.getTransaction().commit();

用于检索所有对象的代码...

professionalWorkList = professionalWorkQuery.getResultList();
Iterator lab = professionalWorkList.listIterator();
while(lab.hasNext()){
ProfessionalWork pWork = (ProfessionalWork) lab.next();
System.out.println(pWork.getJobNo()+" -- "+pWork.getStatus());
//Here i'm getting All Objects(even newly added objects also...but i'm not getting my modified values in objects)
}
professionalWorkList.clear();

最佳答案

刚刚在这里引用了该文档:https://docs.oracle.com/javaee/6/tutorial/doc/gkjjj.html 。很高兴它成功了!

professionalWorkQuery.setHint("javax.persistence.cache.storeMode", "REFRESH");

关于java - 如何从 JPA resultList 获取更新的实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38632243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com