我有一个@ViewScoped
bean,它调用一个@Stateless
bean,它执行一个简单的查询以从我的数据库中返回一些值。
这应该足以在我每次加载页面时进行查询,这应该导致我在每次页面加载时始终更新数据。
但这不起作用,我不知道如何解决它!
我的查询返回旧值,即使在使用 MySql Workbench 更改它之后也是如此。(在 Workbench 上进行查询会返回正确的数据!)
这是代码:
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 + " ]";
}
}
现在,我在使用其他实体和方法之前也遇到过这个问题,我通过刷新实体解决了这个问题,但是如果我每次加载页面时都从数据库中获取实体,为什么要在这种情况下刷新实体呢?
简直是胡说八道!