Search code examples
javajpaejbpersistence.xml

Java EJB + JPA + Problems With Getting Refreshed List


I am Working on a Project.

In which i am getting the list from DB using named queries already available in the enitiy

whenever i update the data of a field it get changed in DB

but it is not reflected in the list i am getting from the Named Query

Is there any way to get the list refreshed everytime ?

EJB is built as a different project and i have added it as a library to the web appliication.

If i undeploy the project and re-deploy it then the list i recevies is new..

Help Needed

Thanks in Advance.

persistence.xml

<jta-data-source> goes </jta-data-source>


<exclude-unlisted-classes>  false  </exclude-unlisted-classes>


<shared-cache-mode>  NONE </shared-cache-mode>


<properties/>

ejb code

@Override

public List<Universityview>  getAllUniversitys () {


    return em.createNamedQuery ("Universityview.findAll"). 

getResultList(); }


Solution

  • Your question has litle to do with EJB itself. Its more of a hibernate/jpa issue. Depends on which one you are using. I assume you are editing the database in a way that your application does not know about? Like with a native query or directly on the database? The problem then is that hibernate uses a 2nd level cache in which the objects from your resultlist are cached. Since you did not update the object through hibernate hibernate has no way of knowing that it changed so it just assumes its still the same and takes theindividual objects from its 2nd level cache.

    You can look into this question for answers on how to invalidate the 2nd level cache.

    Hibernate 2nd level cache invalidation when another process modifies the database