JPA 没有收到更新的数据
JPA not receiving updated data
我使用进入数据库并使用数据库数据定义 bean 属性的 managedBean。但是,当我更新数据库时,我在我的 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 在数据库中进行查找:
@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>
您获得旧值的原因是您的实体存储在二级缓存中。当你第二次请求它时,没有执行数据库调用,而是返回内存中的值。
您可以通过将 <property name="eclipselink.cache.shared.default" value="false"/>
添加到 persistence.xml
的 property
部分来禁用缓存
我使用进入数据库并使用数据库数据定义 bean 属性的 managedBean。但是,当我更新数据库时,我在我的 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 在数据库中进行查找:
@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>
您获得旧值的原因是您的实体存储在二级缓存中。当你第二次请求它时,没有执行数据库调用,而是返回内存中的值。
您可以通过将 <property name="eclipselink.cache.shared.default" value="false"/>
添加到 persistence.xml
property
部分来禁用缓存