我使用进入db的managedBean并使用db data定义bean属性。但是,当我更新db时,我在bean中收到的数据不会更新。
db中的查询是这样的:
private static final String JPQL_FIND_BY_ID = "SELECT c FROM Category c WHERE c.idcategory=:id";
@Override
public Category findCatById(int id) {
Query query = em.createQuery(JPQL_FIND_BY_ID, Category.class);
query.setParameter("id", id);
Category cat = null;
try {
cat = (Category) query.getSingleResult();
} catch (Exception e) {
e.printStackTrace();
}
return cat;
}
我用来获取类别的托管bean要求ejb在db中进行查找:
@ManagedBean
public class CategoryBean {
private String idCategoryStr;
private Category category;
private int id;
@EJB
private CategoryLookUp categoryService;
@PostConstruct
public void init() {
this.category = categoryService.findCatById(id); //id defined in constructor
System.out.println(this.category.getName());//this will give the same
//name before and after db update
}
public CategoryBean() {
FacesContext fc = FacesContext.getCurrentInstance();
Map<String, String> paramsMap = fc.getExternalContext()
.getRequestParameterMap();
this.idCategoryStr = paramsMap.get("id");
try {
id = Integer.parseInt(idCategoryStr);
} catch (Exception e) {
}
}
//get&set
}
如果我更改了我的数据库中的类别标题,即使调用@PostConstruct并且id正确,它在我的bean中也会保持不变。
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="my-pu" transaction-type="JTA">
<jta-data-source>jdbc/forumcsDS</jta-data-source>
<class>main.java.entities.Category</class>
<class>main.java.entities.Country</class>
<class>main.java.entities.Forum</class>
<class>main.java.entities.Message</class>
<class>main.java.entities.Post</class>
<class>main.java.entities.Thethread</class>
<class>main.java.entities.Usercfg</class>
<class>main.java.entities.Usercredential</class>
<class>main.java.entities.Userinfo</class>
<class>main.java.entities.User</class>
<class>main.java.entities.Friend</class>
</persistence-unit>
</persistence>
答案 0 :(得分:2)
您获得旧值的原因是您的实体存储在二级缓存中。当您第二次请求时,不会执行数据库调用,但会返回内存中的值。
您可以通过将<property name="eclipselink.cache.shared.default" value="false"/>
添加到property
的{{1}}部分来停用缓存