60264

JPA not receiving updated data

Question:

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>

Answer1:

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

Recommend

  • getClassLoader().getResourceAsStream(“../resources/config.properties”) returns null [duplicate]
  • Symfony2 A session had already been started - ignoring session_start()
  • Glassfish denies access to JSF page, returning 403 HTTP response code
  • wordpress featured image uploader not working
  • django start celery daemon in production with supervisor
  • Zend Framework: only index view renders 404 error when trying to access other views via /controller/
  • CakePHP 3.0 not running without intl extension
  • how to install mysqli on windows iis server?
  • cakephp emails not working
  • Warning: mail(): Failed to Receive in
  • How to mail a log file to yourself using PHP from a Windows machine ?
  • error: expected unqualified-id before string constant
  • JSF - Keep Faces Messages after redirect from @PostConstruct
  • Autoloading classes when not in same directory as zend library
  • Pip behind a proxy with a custom certificate file
  • Iterate within
  • Increase the limit of file upload size in Heroku while uploading to Dropbox
  • Get highest value from a file using mSL and mIRC
  • Enabling domxml in php
  • Change of MySQL database location not working
  • Changing MySQL charset to UTF8 on linux so it will work with JDBC
  • Will a method annotated with @PostConstruct be guaranteed to execute prior to a method with @Schedul
  • Spring boot 2.0.0.M4 required a bean named 'entityManagerFactory' that could not be found
  • How can I set a binding to a Combox in a UserControl?
  • Install PHP intl extension on MacOS
  • Create DicomImage from scratch using Dcmtk
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • one Local Olampyad Questions on Informatic in 2011
  • Cannot Parse HTML Data Using Android / JSOUP
  • KeystoneJS: Relationships in Admin UI not updating
  • Suggestions to manage Login/Logout transitions
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • Net Present Value in Excel for Grouped Recurring CF
  • How can I use threading to 'tick' a timer to be accessed by other threads?