ollaollu
ollaollu

Reputation: 483

refresh lazy loaded Primefaces datatable when a method performs an update

When I update data in the database from a JDBC method, it is persisted but the primefaces datatable doesn't get refreshed. Refreshing the page doesn't show this change either but this change is seen only when the server is restarted and my init has been called again. Is there a way to make Primefaces refresh it's datatable content after I persist my data using this JDBC method?

Note: The JDBC method is different from the managed bean that does the add/update/delete for the database entity.

I hope my question is clear enough.

Here is my datatable:

<p:dataTable value="#{warehouseManagedBean.lazyModel}" 
var="showStock"
widgetVar="warehouseTable"
rows="10" 
rowsPerPageTemplate="20,30,50" 
paginator="true" 
lazy="true"
paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}">              
<p:column headerText="Raw Material" sortBy="#{showStock.rwcode}" filterBy="#{showStock.rwcode}" filterStyle="display:none; visibility:hidden;"> 
<h:outputText value="#{showStock.rwcode}"/>
</p:column>
<p:column headerText="Stock Balance">                                         
<h:outputText value="#{showStock.quantity}"/>
</p:column>
<p:column headerText="Cost">                                        
<h:outputText value="#{showStock.cost}"/>
</p:column>
<p:column headerText="Value">                                         
<h:outputText value="#{showStock.stockValue}"><f:convertNumber type="currency" currencySymbol="$"/></h:outputText>
</p:column>
<p:column rendered="false">                                         
<h:outputText value="#{showStock.stockid}"/>
</p:column>
</p:dataTable>

Here is my JDBC method(if needed)

public void updateWarehouse(double stockbalance, int id) throws SQLException{
conman = new PremierConnection();
String sql = "update warehouse set quantity = ? where stockid = ?";
Connection conn = null;
PreparedStatement stm = null;

try {
    conn = conman.getDBConnection();
    stm = conn.prepareStatement(sql);
    stm.setDouble(1, stockbalance);
    stm.setInt(2, id);
    stm.executeUpdate();            
} catch (SQLException e) {
        System.out.println(e.getMessage());
    } finally {
        if (stm != null) {
            stm.close();
        }
        if (conn != null) {
            conn.close();
        }
    }
}

UPDATE: Warehousemanagedbean

@Named(value = "warehouseManagedBean")
@ViewScoped
public class WarehouseManagedBean implements Serializable{

    private LazyDataModel<Warehouse> lazyModel;    
    private Warehouse stock = new Warehouse();

    @EJB
    private WarehouseEJB ejb;

    @PostConstruct
    public void init(){
        lazyModel = new LazyWarehouseDataModel(ejb);
    }    

    public String newStock(){
        try {
            stock = ejb.create(stock);
            JsfUtil.addSuccessMessage("Stock created successfully");
        } catch (EJBException e){
            JsfUtil.addErrorMessage("Raw material exists");
            return "AddStock.xhtml";
        } catch (Exception e) {
                Logger.getLogger(WarehouseManagedBean.class.getName()).log(Level.SEVERE, "Error creating new stock", e);            
        }
        return "Warehouse.xhtml";
    }

    public String newNextStock(){
        try {
            stock = ejb.create(stock);
            JsfUtil.addSuccessMessage("Stock created successfully");            
        } catch (EJBException e){
            JsfUtil.addErrorMessage("Raw material exists");
        } catch (Exception e) {
                Logger.getLogger(WarehouseManagedBean.class.getName()).log(Level.SEVERE, "Error creating new stock", e);            
        }
        return "AddStock.xhtml";
    }

    public String saveStock(){
        try {
            ejb.edit(stock);
            JsfUtil.addSuccessMessage("Stock updated successfully");
        } catch (EJBException e){
            JsfUtil.addErrorMessage("Error updating stock");
            return "EditStock.xhtml";
        } catch (Exception e) {
                Logger.getLogger(WarehouseManagedBean.class.getName()).log(Level.SEVERE, "Error updating stock", e);            
        }
        return "Warehouse.xhtml";
    }

    public void deleteStock(Warehouse stock){
        try {
            ejb.remove(stock);
            JsfUtil.addSuccessMessage("Stock deleted successfully");            
        } catch (EJBException e){
            JsfUtil.addErrorMessage("Error deleting stock");
        }catch (Exception e) {
                Logger.getLogger(WarehouseManagedBean.class.getName()).log(Level.SEVERE, "Error deleting stock", e);            
        }
    }

    public Warehouse getStock() {
        return stock;
    }

    public List<Warehouse> getWarehouseList() {
        return warehouseList;
    }

    public LazyDataModel<Warehouse> getLazyModel() {
        return lazyModel;
    }    

}

Upvotes: 4

Views: 4615

Answers (1)

Predrag Maric
Predrag Maric

Reputation: 24403

The problem is probably that lazyModel still has old data even after you update the database. Try calling init() again after you update stock (is that saveStock()?), and of course you should update <p:dataTable> component after that action is finished (if that is not already set up, add dataTable's id to update attribute of the button calling the saveStock()).

Upvotes: 1

Related Questions