Hibernate returns many queries and overwrites my changes

Asked

Viewed 76 times

0

Good afternoon guys, I am trying to make a change in a datatable, when clicking on rowEditor, the value is sent correctly, however, I do not know why, It is being held many queries with Hibernate. I don’t know if I could explain myself, but I’m posting the code and the exit with the explanations.... thank you very much :)

listProdut.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);


        }

Productive

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

Console return by clicking on rowEditor v (note that before the 4th select from bottom to top, System.out.println( "get " + product list), 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_

1 answer

1


Friend,

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

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

  1. NONE: Does nothing with the object (default)
  2. MERGE: Update your kids when you update your father
  3. PERSISTS: Save your child when you save your father
  4. REFRESH: Save the father and keep the son unchanged
  5. REMOVE: Remove the child when removing the parent or the other way around
  6. ALL: Propagate all the 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) resolve.


Regarding the second problem (give 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, i.e., your children are not loaded (Ex: Brings list of products without items). If you explicitly use @Manytoone(fetch = Fetchtype.EAGER) all fields in the table will be brought in.

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

More information:

  • Thanks, but my problem was in the pBean get, I did not specify that when the product listing was different from null, do not try to carry out the filling of the list. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.