Hibernate Duplicating Records

0

I'm new to using the Hibernate Framework and so I'm having a hard time resolving an issue.

I'm using a relationship between objects as follows:

@Entity
@Table(name = "PEDIDO")
public class Pedido implements Serializable {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer idped;

        @OneToMany(mappedBy = "pedido", targetEntity = PedidoItem.class, fetch = FetchType.EAGER)
        @Cascade(org.hibernate.annotations.CascadeType.ALL)
        private List<PedidoItem> listaItens;

        //Getters and Setters...
    }


@Entity
@Table(name = "PEDIDO_ITEM")
public class PedidoItem implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer idpit;

    @ManyToOne
    private Pedido pedido;

    //Getters and Setters...
}

When I make a select or insert using these objects, Hibernate is duplicating the data, ie if I make an insert, it writes the same request and even the same product twice in the database.

Here's the select and insert method:

for (Pedido pedido : lista) {
   List<Pedido> ped = pedDAO.verificaPedidoExistente(pedido.getCodrep(), pedido.getIdemp(), pedido.getCodeqp(), pedido.getNupdmb());

                    if (ped.isEmpty()) {
                        int idped = pedDAO.gravarPedidoMobile(pedido);

                        List<PedidoItem> listaItens = pedido.getListaItens();
                        new PedidoItemDAO().gravarItensPedidoMobile(listaItens, idped);

                    }
                }


public List<Pedido> verificaPedidoExistente(Integer codrep, Integer idemp, String codeqp, Integer nupdmb) {
        List<Pedido> pedido = new ArrayList<>();
        String hql = "FROM Pedido WHERE codrep = :codrep AND idemp = :idemp AND codeqp = :codeqp AND nupdmb = :nupdmb";

        Session session = HibernateUtil.getFactory().openSession();
        try {
            pedido = session.createQuery(hql)
                    .setParameter("codrep", codrep)
                    .setParameter("idemp", idemp)
                    .setParameter("codeqp", codeqp)
                    .setParameter("nupdmb", nupdmb)
                    .list();

        } catch (RuntimeException erro) {
            System.err.println("Erro = " + erro.getMessage());
            throw erro;
        } finally {
            session.close();
        }
        return pedido;
    }


 public Integer novoPedido(Pedido pedido) {
        int idped = 0;
        Session session = HibernateUtil.getFactory().openSession();
        Transaction transaction = null;
        System.err.println("Grava novo pedido!");

        try {
            transaction = session.beginTransaction();
            session.save(pedido);
            idped = pedido.getIdped();
            transaction.commit();

        } catch (RuntimeException erro) {
            if (transaction != null) {
                transaction.rollback();
            }
            throw erro;
        } finally {
            session.close();
        }
        return idped;
    }

If I debug the project in NetBeans, duplication does not happen, but in production, duplication happens.

I searched extensively, found numerous similar situations, but it did not solve my problem.

Please, if anyone can help.

Thank you very much

    
asked by anonymous 19.12.2017 / 18:10

1 answer

0

It's because of its mapping in the ListItems attribute. When you call the int idped = pedDAO.gravarPedidoMobile(pedido); it is already creating the item because of Cascade.ALL In this case, you would not need the separate item creation call. Take a look at the @OneToMany and @Cascade documentation

    
21.12.2017 / 05:46