2012-10-05 32 views
0

當我刷新包含數據表的頁面時,我失去了添加到數據表的新數據,我刷新了它近5次,或者我重新啓動了tomcat應用程序服務器來獲取我發佈的數據。即時通訊使用Primefaces3.4 Hibernate 3的JSF 2的Tomcat 7 這是ManagedBean的摘錄:當我刷新包含數據表的頁面時,我失去了添加的新數據Primefaces JSF

import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 
import java.util.TimeZone; 

import javax.enterprise.inject.New; 
import javax.faces.application.FacesMessage; 
import javax.faces.bean.ManagedProperty; 
import javax.faces.context.FacesContext; 
import javax.faces.event.ValueChangeEvent; 
import javax.faces.model.SelectItem; 

import org.primefaces.event.FlowEvent; 
import org.primefaces.event.RowEditEvent; 

import com.oxynord.dao.RetourDao; 
import com.oxynord.dao.BonbonneDao; 
import com.oxynord.dao.BonbonneDataModel; 
import com.oxynord.dao.ChargementDao; 
import com.oxynord.dao.ChauffeurDao; 
import com.oxynord.dao.EnvoiDao; 
import com.oxynord.dao.UtilisateurDao; 
import com.oxynord.dao.VenteDao; 
import com.oxynord.data.*; 
public class BonBonneManagedBean { 
//test test 
//private BonbonneDao BDAO=new BonbonneDao(); 
private Bonbonne bonbonne=new Bonbonne(); 
private Bonbonne selectedBonbonne; 
private Chauffeur chauffeur=new Chauffeur(); 
private Chargement chargement=new Chargement(); 
private Chargement selectedChargement; 
private Envoi envoi=new Envoi(); 
private Envoi selectedEnvoi; 
private Client client=new Client(); 
private Vente vente=new Vente(); 
private Retour retour=new Retour(); 
private Client selectedClient; 
private List<Client> listClients; 
private List<Historiquebonbonne> listhistorique; 

// Test Vente 
List<Bonbonne> bs=new ArrayList<Bonbonne>(); 
List<Bonbonne> bs2=new ArrayList<Bonbonne>(); 

// get user connecté from session 
private AuthenticationController authen = (AuthenticationController)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("authenticationController"); 


private Date date=new Date(); 

// -- Table à cocher des bonbonnes -- 
private BonbonneDataModel bonbonneDataModel; 
private Bonbonne[] selectedBonbonnes; 

boolean next=false; 

public boolean isNext() { 
    return next; 
} 

public void setNext(boolean next) { 
    this.next = next; 
} 


// -------------------------------- edit ----------------------------------------------- 

// modifier une bonbonne 
public void onEdit(RowEditEvent event) throws Exception { 
     BonbonneDao.ModifierBonbonne(((Bonbonne) event.getObject())); 
} 
// modifier un chauffeur 
public void onEditChauffeur(RowEditEvent event) throws Exception { 
    BonbonneDao.ModifierChauffeur(((Chauffeur) event.getObject())); 
} 

,這是BonbonneDAO:

import java.util.ArrayList; 
    import java.util.Date; 
    import java.util.List; 

    import javax.faces.application.FacesMessage; 
    import javax.faces.context.FacesContext; 

    import org.hibernate.Session; 

    import com.oxynord.data.Bonbonne; 
    import com.oxynord.data.Chauffeur; 
    import com.oxynord.data.Client; 
    import com.oxynord.data.Domaine; 
    import com.oxynord.data.Etatactuelle; 
    import com.oxynord.data.Historiquebonbonne; 
    import com.oxynord.data.Produit; 
    import com.oxynord.data.Stock; 
    import com.oxynord.data.Utilisateur; 
    import com.oxynord.data.Ville; 
    import com.oxynord.data.Volume; 
    import com.oxynord.utile.HibernateUtil; 

    public class BonbonneDao { 
Date date=new Date(); 

//----------List Historiquebonbonne-------------------------- 

    @SuppressWarnings("unchecked") 
    public static List<Historiquebonbonne> HistoriqueBonbonne(Bonbonne bonbonne) { 
     Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
     session.beginTransaction(); 
     List<Historiquebonbonne> listHistoriquebonbonne= new ArrayList<Historiquebonbonne>(); 
     listHistoriquebonbonne= session.createQuery("from Historiquebonbonne as h where h.bonbonne.idBonbonne='"+bonbonne.getIdBonbonne()+"' order by h.idHistorique desc ").list(); 
     return listHistoriquebonbonne; 
    } 

//----------List des bonbonnes qui ont dépasser la date de 3 mois-------------------------- 

@SuppressWarnings("unchecked") 
public static List<Bonbonne> BonbonnesVendu() { 
    Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
    session.beginTransaction(); 
    List<Bonbonne> listBonbonnesVente= new ArrayList<Bonbonne>(); 
    listBonbonnesVente = session.createQuery("from Etatactuelle where libelle= 'Vendu' ").list(); 
    return listBonbonnesVente; 
} 

//-------------------------------------------------------------------------- 

@SuppressWarnings("unchecked") 
public static List<Bonbonne> ListBonbonnesVide() { 
    try { 
    Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
    session.beginTransaction(); 
    List<Bonbonne> listBonbonnes= new ArrayList<Bonbonne>(); 
    listBonbonnes = session.createQuery("from Bonbonne where etatactuelle.idEtat=1 and stock.idStock=1").list(); 
    return listBonbonnes; 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
    return null; 
} 

@SuppressWarnings("unchecked") 
public static List<Bonbonne> AllBonbonnes() { 
    try { 
    Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
    session.beginTransaction(); 
    List<Bonbonne> listBonbonnes= new ArrayList<Bonbonne>(); 
    listBonbonnes = session.createQuery("from Bonbonne").list(); 
    return listBonbonnes; 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
    return null; 
} 

@SuppressWarnings("unchecked") 
public static List<Produit> AllProduits() { 
    try { 
    Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
    session.beginTransaction(); 
    List<Produit> listProduit= new ArrayList<Produit>(); 
    listProduit = session.createQuery("from Produit").list(); 
    return listProduit; 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
    return null; 
} 

@SuppressWarnings("unchecked") 
public static List<Ville> AllVille() { 
    try { 
    Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
    session.beginTransaction(); 
    List<Ville> listVille= new ArrayList<Ville>(); 
    listVille = session.createQuery("from Ville").list(); 
    return listVille; 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
    return null; 
} 

@SuppressWarnings("unchecked") 
public static List<Domaine> AllDomaine() { 
    try { 
    Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
    session.beginTransaction(); 
    List<Domaine> listDomaine= new ArrayList<Domaine>(); 
    listDomaine = session.createQuery("from Domaine").list(); 
    return listDomaine; 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
    return null; 
} 

@SuppressWarnings("unchecked") 
public static List<Volume> AllVolumes() { 
    try { 
    Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
    session.beginTransaction(); 
    List<Volume> listVolume= new ArrayList<Volume>(); 
    listVolume = session.createQuery("from Volume").list(); 
    return listVolume; 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
    return null; 
} 

@SuppressWarnings("unchecked") 
public static List<Etatactuelle> AllEtatsActuelleBonbonne() { 
    try { 
    Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
    session.beginTransaction(); 
    List<Etatactuelle> listEtatactuelle= new ArrayList<Etatactuelle>(); 
    listEtatactuelle = session.createQuery("from Etatactuelle").list(); 
    return listEtatactuelle; 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
    return null; 
} 

@SuppressWarnings("unchecked") 
public static List<Stock> AllStocks() { 
    try { 
    Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
    session.beginTransaction(); 
    List<Stock> listStock= new ArrayList<Stock>(); 
    listStock = session.createQuery("from Stock").list(); 
    return listStock; 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
    return null; 
} 

// ---------------------------- Ajouter Bonbonne ----------------------------------------------- 

@SuppressWarnings("unchecked") 
public static boolean addBonbonne(Bonbonne bonbonne) throws Exception { 
    try { 
    Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
    org.hibernate.Transaction tx = session.beginTransaction(); 
    List<Bonbonne> b=session.createQuery("from Bonbonne where codebare='"+bonbonne.getCodebare()+"' or numeroSerie='"+bonbonne.getNumeroSerie()+"'").list(); 

    /* Test si le bonbonne est deja enregistrer 
    si oui --> afficher que le bobonne est deja enregistrer 
    si non --> enregistrer le bonbonne */ 

    if(b.size()==0) { 
    //Utilisateur u=(Utilisateur) session.createQuery("from Utilisateur").list().get(0);  
    //bonbonne.setUtilisateur(u); 
    session.save(bonbonne); 
    tx.commit(); 
    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("une bonbonne a été enregister avec succes")); 
    return true; 
    } else { 
    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"Il existe déjà un Code barre:"+b.get(0).getCodebare()+" avec un numero de série:"+b.get(0).getNumeroSerie()+"", "Code barre ou numero de série déjà enregistrer")); 
    return false; 
    } 

    } catch (Exception e) { 
    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Erreur d'enregistrement", "Erreur d'enregistrement")); 
    System.out.println(e); 
    return false; 
    } 
} 
+0

先發布一些代碼,我們怎麼知道問題出在哪裏?但是從你寫的東西 - 你的ManagedBean ViewScoped? –

+0

不,它的要求 –

+0

因此,使它ViewScoped ;-) –

回答

1

託管bean的範圍更改爲:@ javax.faces .bean.ViewScoped

+0

仍然有同樣的問題,即使我把Managedbean ViewScoped –

0

您應該使用@ViewScoped以確保只要您保持在同一視圖(又名yourpage.xhtml)中數據將會持續存在。

如果您在同一頁面中執行操作,請確保您的方法的類型爲void,否則返回null。這會讓你保持一致。

在堅持DAO層後執行flush

這是基於您所描述的行爲的猜測。如果它有效,你應該調整你的Hibernate層flush/commit選項,因爲看起來這些項目正在被保存但未提交/刷新,然後不更新持久化上下文,從而不會在頁面中顯示。

我希望它有幫助。

+0

當我添加他們在數據庫中的數據,所以它被持久提交併發佈在我的頁面上,但當我刷新我的頁面時,我失去了它,當我更新幾次時,他們再次出現 –

+0

@Bob你如何加載數據?一些更多的代碼Bean構建和數據列表 – rbento

+0

我添加了DAO –

相關問題