Hibernate Duplicating Records

Asked

Viewed 324 times

0

I am new to the use of the Hibernate Framework and so I’m having trouble solving a question.

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, that is, if I do an Insert, write the same request twice and the same product in the database.

Here is the method of select and Insert:

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 researched extensively, I found numerous similar situations, but it did not solve my problem.

Please, if anyone can help.

Thank you very much

1 answer

1

It is because of its mapping in the list attribute. When you make the call to int idped = pedDAO.gravarPedidoMobile(pedido); he’s 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 documentation from @Onetomany and @Cascade

Browser other questions tagged

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