JPA not receiving updated data

593 views Asked by At

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.

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;
}

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
}

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

There are 1 answers

0
talex On BEST ANSWER

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.

You can disable cache by adding <property name="eclipselink.cache.shared.default" value="false"/> to property section of your persistence.xml