Update lazy loaded file Primary data when method does update

When I update data in the database using JDBC method, it persists, but there is no need to update data streams. Refreshing the page does not reflect this change either, but this change is only visible after restarting the server and calling init again. Is there a way to have Primefaces update its datatable content after I save my data using this JDBC method?

Note. The JDBC method is different from a managed bean, which does add / update / delete on a database object.

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;
    }    

}

      

+3


source to share


1 answer


Probably the problem is lazyModel

having old data even after updating the database. Retry the call init()

again after updating the stock (is that saveStock()

?). And of course you have to update the component <p:dataTable>

after this action is complete (if not already configured, add dataTable

id to the update

attribute of the button calling saveStock()

).



+1


source







All Articles