JPA未收到更新的数据 [英] JPA not receiving updated data

查看:101
本文介绍了JPA未收到更新的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用进入数据库并使用db数据定义bean属性的ManagedBean.但是,当我更新数据库时,在我的bean中接收到的数据不会更新.

I use a managedBean that goes into the db and defines the bean properties with db data. However when I update the db the data I receive in my bean is not updated.

db中的查询是这样的:

The query in db is like this:

    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中进行查找:

The managed bean I use to get the category asks the ejb to make a lookup in the 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中也将保持不变.

If I change the Title of the category in my db, it's gonna be unchanged in my bean even though @PostConstruct is called and the id is correct.

<?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>

推荐答案

得到旧值的原因是您的实体存储在二级缓存中.当您第二次请求时,不会执行任何数据库调用,但是会返回内存中的值.

Reason that you get old value is that your entity is stored in second level cache. And when you request it second time no database call is executed, but value from memory is returned.

您可以通过在persistence.xml

这篇关于JPA未收到更新的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆