Hibernate returns many queries and overwrites my changes

0

Good afternoon, I'm trying to make a change in a datatable, when I click on rowEditor, the value is sent correctly, but I do not know why, many queries are being performed with hibernate. I do not know if I could explain myself correctly, but I am posting the code and the output with the explanations .... thank you very much:)

Productlist.xhtml

            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">



    <ui:composition template= "template.xhtml">

    <ui:define name="conteudo" >

    <h:form id="form">



                <p:growl id="msgs" showDetail="true"/>

    <p:dataTable  var="prod" value="#{pBean.listaproduto}" editable="true" style="margin-bottom:20px" id="listaproduto" >


            <p:ajax event="rowEdit" listener="#{pBean.onRowEdit}" update=":form:listaproduto" />


        <p:column headerText="ID">
            <p:cellEditor>
                <f:facet name="output"><h:outputText value="#{prod.nome}"/></f:facet>
                <f:facet name="input"><p:inputText id="idProd" value="#{prod.id}" style="width:100%"/></f:facet>
            </p:cellEditor>
        </p:column>

        <p:column headerText="Nome">
            <p:cellEditor>
                <f:facet name="output"><h:outputText value="#{prod.nome}"/></f:facet>
                <f:facet name="input"><p:inputText id="idNome" value="#{prod.nome}" style="width:100%"/></f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="Preço">
            <p:cellEditor>
                <f:facet name="output"><h:outputText value="#{prod.preco}"/></f:facet>
                <f:facet name="input"><p:inputText id="idPreco" value="#{prod.preco}" style="width:100%"/></f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="Fornecedor">
            <p:cellEditor>
                <f:facet name="output"><h:outputText value="#{prod.fornecedor}"/></f:facet>
                <f:facet name="input"><p:inputText id="idFornecedor" value="#{prod.fornecedor}" style="width:100%"/></f:facet>
            </p:cellEditor>
        </p:column>
        <p:column headerText="Categoria">
      <p:cellEditor>
          <f:facet name="output"><h:outputText value="#{prod.categoria}"/></f:facet>
          <f:facet name="input"><p:inputText  id="idCategoria" value="#{prod.categoria}" style="width:100%"/></f:facet>
      </p:cellEditor>
</p:column>             



    <p:column style="width:32px">
        <p:rowEditor  />


    </p:column> 
</p:dataTable>

    </h:form>



</ui:define>

</ui:composition>

</html>

pBean

public List<Produto> getListaproduto() {
            try{

            System.out.println( "get " + listaproduto);

                listaproduto = new ProdutoDao().listar();



            }catch(Exception e){
                e.printStackTrace();
            }


            return listaproduto;
        }


        public void onRowEdit(RowEditEvent event) {

     System.out.println( "no onRowEdit " + listaproduto);


        }

Product

        public List<Produto> listar()throws Exception {
        session = HibernateUtil.getSessionFactory().openSession();
        query = session.createQuery("select p from Produto p");

        List<Produto> lista = query.list();
        session.close();

            return lista;
        }

Return from the Console when clicking the rowEditor v (note that before the 4th select from the bottom, System.out.println ("get" + productlist) returns the correct value I passed.)

        get null
    Hibernate: 
        select
            this_.id as id1_0_0_,
            this_.categoria as categori2_0_0_,
            this_.fornecedor as forneced3_0_0_,
            this_.nome as nome4_0_0_,
            this_.preco as preco5_0_0_ 
        from
            Produto this_
    get [Produto [id= 1, nome= tetse, preco= 534534.0, fornecedor= teste, categoria= teste], Produto [id= 2, nome= 123, preco= 123.0, fornecedor= 123, categoria= 123]]
    Hibernate: 
        select
            this_.id as id1_0_0_,
            this_.categoria as categori2_0_0_,
            this_.fornecedor as forneced3_0_0_,
            this_.nome as nome4_0_0_,
            this_.preco as preco5_0_0_ 
        from
            Produto this_
    get [Produto [id= 1, nome= tetse, preco= 534534.0, fornecedor= teste, categoria= teste], Produto [id= 2, nome= 123, preco= 123.0, fornecedor= 123, categoria= 123]]
    Hibernate: 
        select
            this_.id as id1_0_0_,
            this_.categoria as categori2_0_0_,
            this_.fornecedor as forneced3_0_0_,
            this_.nome as nome4_0_0_,
            this_.preco as preco5_0_0_ 
        from
            Produto this_
    get [Produto [id= 1, nome= tetse, preco= 534534.0, fornecedor= teste, categoria= teste], Produto [id= 2, nome= 123, preco= 123.0, fornecedor= 123, categoria= 123]]
    Hibernate: 
        select
            this_.id as id1_0_0_,
            this_.categoria as categori2_0_0_,
            this_.fornecedor as forneced3_0_0_,
            this_.nome as nome4_0_0_,
            this_.preco as preco5_0_0_ 
        from
            Produto this_
    get [Produto [id= 1, nome= tetse, preco= 534534.0, fornecedor= teste, categoria= teste], Produto [id= 2, nome= 123, preco= 123.0, fornecedor= 123, categoria= 123]]
    Hibernate: 
        select
            this_.id as id1_0_0_,
            this_.categoria as categori2_0_0_,
            this_.fornecedor as forneced3_0_0_,
            this_.nome as nome4_0_0_,
            this_.preco as preco5_0_0_ 
        from
            Produto this_
    get [Produto [id= 1, nome= tetse, preco= 534534.0, fornecedor= teste, categoria= teste], Produto [id= 2, nome= 123, preco= 123.0, fornecedor= 123, categoria= 123]]
    Hibernate: 
        select
            this_.id as id1_0_0_,
            this_.categoria as categori2_0_0_,
            this_.fornecedor as forneced3_0_0_,
            this_.nome as nome4_0_0_,
            this_.preco as preco5_0_0_ 
        from
            Produto this_
    get [Produto [id= 1, nome= tetse, preco= 534534.0, fornecedor= teste, categoria= teste], Produto [id= 2, nome= 123, preco= 123.0, fornecedor= 123, categoria= 123]]
    Hibernate: 
        select
            this_.id as id1_0_0_,
            this_.categoria as categori2_0_0_,
            this_.fornecedor as forneced3_0_0_,
            this_.nome as nome4_0_0_,
            this_.preco as preco5_0_0_ 
        from
            Produto this_
    get [Produto [id= 1, nome= tetse, preco= 534534.0, fornecedor= teste, categoria= teste], Produto [id= 2, nome= 123, preco= 123.0, fornecedor= 123, categoria= 123]]
    Hibernate: 
        select
            this_.id as id1_0_0_,
            this_.categoria as categori2_0_0_,
            this_.fornecedor as forneced3_0_0_,
            this_.nome as nome4_0_0_,
            this_.preco as preco5_0_0_ 
        from
            Produto this_
    get [Produto [id= 1, nome= tetse, preco= 534534.0, fornecedor= teste, categoria= teste], Produto [id= 2, nome= 123, preco= 123.0, fornecedor= 123, categoria= 123]]
    Hibernate: 
        select
            this_.id as id1_0_0_,
            this_.categoria as categori2_0_0_,
            this_.fornecedor as forneced3_0_0_,
            this_.nome as nome4_0_0_,
            this_.preco as preco5_0_0_ 
        from
            Produto this_
    get [Produto [id= 1, nome= tetse, preco= 534534.0, fornecedor= teste, categoria= teste], Produto [id= 2, nome= 123, preco= 123.0, fornecedor= 123, categoria= 123]]
    Hibernate: 
        select
            this_.id as id1_0_0_,
            this_.categoria as categori2_0_0_,
            this_.fornecedor as forneced3_0_0_,
            this_.nome as nome4_0_0_,
            this_.preco as preco5_0_0_ 
        from
            Produto this_
    get [Produto [id= 1, nome= tetse, preco= 534534.0, fornecedor= teste, categoria= teste], Produto [id= 2, nome= 123, preco= 123.0, fornecedor= 123, categoria= 123]]
    Hibernate: 
        select
            this_.id as id1_0_0_,
            this_.categoria as categori2_0_0_,
            this_.fornecedor as forneced3_0_0_,
            this_.nome as nome4_0_0_,
            this_.preco as preco5_0_0_ 
        from
            Produto this_
    get [Produto [id= 1, nome= tetse, preco= 534534.0, fornecedor= teste, categoria= teste], Produto [id= 2, nome= 123, preco= 123.0, fornecedor= 123, categoria= 123]]
    Hibernate: 
        select
            this_.id as id1_0_0_,
            this_.categoria as categori2_0_0_,
            this_.fornecedor as forneced3_0_0_,
            this_.nome as nome4_0_0_,
            this_.preco as preco5_0_0_ 
        from
            Produto this_
    get [Produto [id= 1, nome= tetse, preco= 534534.0, fornecedor= teste, categoria= teste], Produto [id= 2, nome= 123, preco= 123.0, fornecedor= 123, categoria= 123]]
    Hibernate: 
        select
            this_.id as id1_0_0_,
            this_.categoria as categori2_0_0_,
            this_.fornecedor as forneced3_0_0_,
            this_.nome as nome4_0_0_,
            this_.preco as preco5_0_0_ 
        from
            Produto this_
    get [Produto [id= 1, nome= tetse, preco= 534534.0, fornecedor= teste, categoria= teste], Produto [id= 2, nome= 123, preco= 123.0, fornecedor= 123, categoria= 123]]
    Hibernate: 
        select
            this_.id as id1_0_0_,
            this_.categoria as categori2_0_0_,
            this_.fornecedor as forneced3_0_0_,
            this_.nome as nome4_0_0_,
            this_.preco as preco5_0_0_ 
        from
            Produto this_
    get [Produto [id= 1, nome= tetse, preco= 534534.0, fornecedor= teste, categoria= teste], Produto [id= 2, nome= 123, preco= 123.0, fornecedor= 123, categoria= 123]]
    Hibernate: 
        select
            this_.id as id1_0_0_,
            this_.categoria as categori2_0_0_,
            this_.fornecedor as forneced3_0_0_,
            this_.nome as nome4_0_0_,
            this_.preco as preco5_0_0_ 
        from
            Produto this_
    get [Produto [id= 1, nome= tetse, preco= 534534.0, fornecedor= teste, categoria= teste], Produto [id= 2, nome= 123, preco= 123.0, fornecedor= 123, categoria= 258]]
    Hibernate: 
        select
            this_.id as id1_0_0_,
            this_.categoria as categori2_0_0_,
            this_.fornecedor as forneced3_0_0_,
            this_.nome as nome4_0_0_,
            this_.preco as preco5_0_0_ 
        from
            Produto this_
    get [Produto [id= 1, nome= tetse, preco= 534534.0, fornecedor= teste, categoria= teste], Produto [id= 2, nome= 123, preco= 123.0, fornecedor= 123, categoria= 123]]
    Hibernate: 
        select
            this_.id as id1_0_0_,
            this_.categoria as categori2_0_0_,
            this_.fornecedor as forneced3_0_0_,
            this_.nome as nome4_0_0_,
            this_.preco as preco5_0_0_ 
        from
            Produto this_
    no onRowEdit [Produto [id= 1, nome= tetse, preco= 534534.0, fornecedor= teste, categoria= teste], Produto [id= 2, nome= 123, preco= 123.0, fornecedor= 123, categoria= 123]]
    get [Produto [id= 1, nome= tetse, preco= 534534.0, fornecedor= teste, categoria= teste], Produto [id= 2, nome= 123, preco= 123.0, fornecedor= 123, categoria= 123]]
    Hibernate: 
        select
            this_.id as id1_0_0_,
            this_.categoria as categori2_0_0_,
            this_.fornecedor as forneced3_0_0_,
            this_.nome as nome4_0_0_,
            this_.preco as preco5_0_0_ 
        from
            Produto this_
    get [Produto [id= 1, nome= tetse, preco= 534534.0, fornecedor= teste, categoria= teste], Produto [id= 2, nome= 123, preco= 123.0, fornecedor= 123, categoria= 123]]
    Hibernate: 
        select
            this_.id as id1_0_0_,
            this_.categoria as categori2_0_0_,
            this_.fornecedor as forneced3_0_0_,
            this_.nome as nome4_0_0_,
            this_.preco as preco5_0_0_ 
        from
            Produto this_
    
asked by anonymous 12.10.2016 / 20:10

1 answer

1

Friend,

It would be nice if you post the mapping data from the Product entity.

For the first problem, regarding overwriting data I suggest you read more about the @Cascade annotation, property used with mappedBy. This property is responsible for propagating the modifications, perhaps your project is with CascadeType.ALL so it is propagating the modifications. There are 5 main types you can use:

  • NONE: Do nothing with the object (default)
  • MERGE: Update children when parent update
  • PERSIST: Saves child when saving parent
  • REFRESH: Saves the parent and keeps the child unchanged
  • REMOVE: Removes child when parent is removed or vice versa
  • ALL: Spreads all changes
  • Examples:

     @ManyToMany(cascade=CascadeType.MERGE) // anotação direta na ligação
     @Cascade(cascade={CascadeType.PERSIST}) //anotação individual
     @Cascade(cascade={CascadeType.PERSIST,CascadeType.REFRESH}) //anotação com combinações
    

    In your case, maybe @OneToMany(cascade=CascadeType.PERSIST) resolves.

    Regarding the second problem (making a lot of data) you should study more about FETCH. By default when the relationship is annotated with @OneToMany or @ManyToMany it is loaded in Lazy mode, ie your children are not loaded (Ex: Brings product list without items). If you explicitly use @ManyToOne (fetch = FetchType.EAGER) all fields in the corresponding table will be brought in.

    In your case, maybe @ManyToOne(fetch = FetchType.LAZY) resolves.

    More information:

    13.10.2016 / 15:49