Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7904185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:56:44+00:00 2026-06-03T09:56:44+00:00

I’ve got a @ViewScoped bean that calls a @Stateless bean which does a simple

  • 0

I’ve got a @ViewScoped bean that calls a @Stateless bean which does a simple query to return some values from my DB.

This should be enough to make the query everytime I load the page, and this should lead me to have always updated data on each page load.

But this won’t work, and I don’t know how to solve it!

My query returns the old value, even after changing it with MySql Workbench.
(Doing the query on Workbench returns correct data!)

Here’s the code :

DispensaListBean.java

package ManagedBeans;

import ejb.DispensaManager;
import ejb.DispensaManagerLocal;
import entity.Dispensa;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author stefano
 */
@ManagedBean
@ViewScoped
public class DispensaListBean {
    @EJB
    private DispensaManagerLocal dispensaManager;


    /**
     * Creates a new instance of DIspensaListBean
     */
    public DispensaListBean() {
    }

    public List<Dispensa> getTopDispense(){
        List<Dispensa> l = dispensaManager.findByVoto(DispensaManager.DESC);
        for(Dispensa d : l){
            System.out.println(d.getTitolo() + " | " + d.getVoto()); //This code prints ALWAY the old getVoto() value, it takes the new one just after restarting the server
        }
        return l;
    }

    public List<Dispensa> getDispense(){
        return dispensaManager.findAll();
    }

    public Dispensa getById(int i){
        return dispensaManager.findById(i);
    }
}

DispensaManager.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb;

import entity.Dispensa;
import facade.DispensaFacadeLocal;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author stefano
 */
@Stateless
public class DispensaManager implements DispensaManagerLocal {

    public static final int ASC=0, DESC=1;

    @EJB
    private DispensaFacadeLocal dispensaFacade;

    @Override
    public java.util.List<Dispensa> findByVoto(int order) {
        return (order==DispensaManager.ASC) ? dispensaFacade.findByVotoAsc() : dispensaFacade.findByVotoDesc();
    }

    @Override
    public List findAll() {
        return dispensaFacade.findAll();
    }

    @Override
    public Dispensa findById(int id) {
        return dispensaFacade.find(id);
    }                
}

DispensaFacade.java

package facade;

import entity.Dispensa;
import entity.Post;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author stefano
 */
@Stateless
public class DispensaFacade extends AbstractFacade<Dispensa> implements DispensaFacadeLocal {
    @PersistenceContext(unitName = "UNILIFE-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public DispensaFacade() {
        super(Dispensa.class);
    }

    @Override
    public List<Dispensa> findByVotoDesc() {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Dispensa> q = cb.createQuery(Dispensa.class);
        Root<Dispensa> c = q.from(Dispensa.class);
        q.select(c);
        q.where(cb.isNotNull(c.get("datiFile")));
        q.orderBy(cb.desc(c.get("voto")));
        TypedQuery<Dispensa> typedQuery = em.createQuery(q);
        return typedQuery.getResultList();
    }

    @Override
    public java.util.List<Dispensa> findByVotoAsc() {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Dispensa> q = cb.createQuery(Dispensa.class);
        Root<Dispensa> c = q.from(Dispensa.class);
        q.select(c);
        q.where(cb.isNotNull(c.get("datiFile")));
        q.orderBy(cb.asc(c.get("voto")));
        TypedQuery<Dispensa> typedQuery = em.createQuery(q);
        return typedQuery.getResultList();
    }
}

Dispensa.java

package entity;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author stefano
 */
@Entity
@Table(name = "Dispensa")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Dispensa.findAll", query = "SELECT d FROM Dispensa d"),
    @NamedQuery(name = "Dispensa.findById", query = "SELECT d FROM Dispensa d WHERE d.id = :id"),
    @NamedQuery(name = "Dispensa.findByTitolo", query = "SELECT d FROM Dispensa d WHERE d.titolo = :titolo"),
    @NamedQuery(name = "Dispensa.findByDescrizione", query = "SELECT d FROM Dispensa d WHERE d.descrizione = :descrizione"),
    @NamedQuery(name = "Dispensa.findByTag", query = "SELECT d FROM Dispensa d WHERE d.tag = :tag"),
    @NamedQuery(name = "Dispensa.findByData", query = "SELECT d FROM Dispensa d WHERE d.data = :data"),
    @NamedQuery(name = "Dispensa.findByVoto", query = "SELECT d FROM Dispensa d WHERE d.voto = :voto"),
    @NamedQuery(name = "Dispensa.findByNumVoti", query = "SELECT d FROM Dispensa d WHERE d.numVoti = :numVoti"),
    @NamedQuery(name = "Dispensa.findByNumDownloads", query = "SELECT d FROM Dispensa d WHERE d.numDownloads = :numDownloads")})
public class Dispensa implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @NotNull
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "titolo")
    private String titolo;
    @Size(max = 255)
    @Column(name = "descrizione")
    private String descrizione;
    @Size(max = 255)
    @Column(name = "tag")
    private String tag;
    @Basic(optional = true)
    @NotNull
    @Lob
    @Column(name = "datiFile")
    private byte[] datiFile;
    @Basic(optional = false)
    @NotNull
    @Column(name = "data")
    @Temporal(TemporalType.DATE)
    private Date data;
    @Basic(optional = false)
    @NotNull
    @Column(name = "voto")
    private int voto;
    @Basic(optional = false)
    @NotNull
    @Column(name = "numVoti")
    private int numVoti;
    @Basic(optional = false)
    @NotNull
    @Column(name = "numDownloads")
    private int numDownloads;
    @JoinTable(name = "Scaricati", joinColumns = {
        @JoinColumn(name = "dispensa", referencedColumnName = "id")}, inverseJoinColumns = {
        @JoinColumn(name = "utente", referencedColumnName = "username")})
    @ManyToMany(fetch = FetchType.LAZY)
    private Collection<Utente> downloaders;
    @JoinColumn(name = "materia", referencedColumnName = "id")
    @ManyToOne(optional = true)
    private Materia materia;
    @JoinColumn(name = "autore", referencedColumnName = "username")
    @ManyToOne(optional = false)
    private Utente autore;

    public Dispensa() {
    }

    public Dispensa(Integer id) {
        this.id = id;
    }

    public Dispensa(Integer id, String titolo, byte[] datiFile, Date data, int voto, int numVoti, int numDownloads) {
        this.id = id;
        this.titolo = titolo;
        this.datiFile = datiFile;
        this.data = data;
        this.voto = voto;
        this.numVoti = numVoti;
        this.numDownloads = numDownloads;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitolo() {
        return titolo;
    }

    public void setTitolo(String titolo) {
        this.titolo = titolo;
    }

    public String getDescrizione() {
        return descrizione;
    }

    public void setDescrizione(String descrizione) {
        this.descrizione = descrizione;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public byte[] getDatiFile() {
        return datiFile;
    }

    public void setDatiFile(byte[] datiFile) {
        this.datiFile = datiFile;
    }

    public Date getData() {
        return data;
    }

    public void setData(Date data) {
        this.data = data;
    }

    public int getVoto() {
        return voto;
    }

    public void setVoto(int voto) {
        this.voto = voto;
    }

    public int getNumVoti() {
        return numVoti;
    }

    public void setNumVoti(int numVoti) {
        this.numVoti = numVoti;
    }

    public int getNumDownloads() {
        return numDownloads;
    }

    public void setNumDownloads(int numDownloads) {
        this.numDownloads = numDownloads;
    }

    @XmlTransient
    public Collection<Utente> getDownloaders() {
        return downloaders;
    }

    public void setDownloaders(Collection<Utente> utenteCollection) {
        this.downloaders = utenteCollection;
    }

    public Materia getMateria() {
        return materia;
    }

    public void setMateria(Materia materia) {
        this.materia = materia;
    }

    public Utente getAutore() {
        return autore;
    }

    public void setAutore(Utente autore) {
        this.autore = autore;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Dispensa)) {
            return false;
        }
        Dispensa other = (Dispensa) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.Dispensa[ id=" + id + " ]";
    }

}

Now, I’ve faced this problem before with other entities and methods, and I solved it by refreshing the entities, but why should I refresh an entity in this case if I get it from the database everytime that I load the page?

It’s just nonsense!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:56:45+00:00Added an answer on June 3, 2026 at 9:56 am

    From the code itself it doesn’t look like you’re doing any explicit caching yourself. @ViewScoped, @RequestScoped and isPostback are all not relevant here, and on the contrary, the purpose of those scopes is actually to do caching, instead of letting the backing bean call through to the service each and every time.

    That however is almost the opposite of your problem.

    In case you get stale entities from the entity manager, it’s almost always a case of an L2 cache. Did you configure any in persistence.xml? Which JPA implementation do you use?

    Also important, where and how do you update your data? The code as given doesn’t show it. You do mention this “even after changing it with MySql Workbench“

    In the case that a JPA Level 2 (L2) cache is used, JPA will get the entities from this cache. Without counter measures, it will track changes to those entities only if they are modified via JPA. If you update the underlying data yourself, either directly via JDBC or via some other external system (like MySql Workbench), JPA will not be aware of those changes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.