Merge using Hibernate without erasing other Java data

Asked

Viewed 432 times

0

I use Hibernate in my Java application for database interaction using a controller class created even by Netbeans, but the problem I have is this: when making an edit in some field and only pass this value in the method I will call and merge all the others that I did not pass are set as null feeding only the field I passed information.

Below is the method of the controller class that updates data.

Productodao.java.

 public void edit(Produto produto) throws NonexistentEntityException, Exception {
    EntityManager em = null;
    try {
        em = getEntityManager();
        em.getTransaction().begin();
        produto = em.merge(produto);
        em.getTransaction().commit();
    } catch (Exception ex) {
        String msg = ex.getLocalizedMessage();
        if (msg == null || msg.length() == 0) {
            Integer id = produto.getCodproduto();
            if (findProduto(id) == null) {
                throw new NonexistentEntityException("The produto with id " + id + " no longer exists.");
            }
        }
        throw ex;
    } finally {
        if (em != null) {
            em.close();
        }
    }
}
  • Clayton, to better help you, it would be necessary that the Product class and the update of the same be in the body of the question, if not solved, adds there that we will be happy to help you.

2 answers

2


Entitymanager’s refresh method updates the database instance, overriding the specified properties and keeping the others.

The entity to be passed must be in the administered state (Managed).

Unfortunately the refresh method does not work in container managers like Spring and EJB.

In short, the refresh method works as a merge for the specified properties.

For example:

Produto p = em.find(Produto.class, 1);  
em.refresh(p);

// Depois do commit o objeto sera salvo no banco de dados
p.setNome("TV Full HD");

In your case it’ll look something like this:

public void edit(Produto p) {
    EntityManager em = null;

    try {
        em = getEntityManager();
        em.getTransaction().begin();

        Produto produto = em.find(Produto.class, p.getId());
        em.refresh(produto);

        if(p.getNome() != null) {
            produto.setNome(p.getNome());
        }

        // Outras propriedades que você queira sobrescrever

        em.getTransaction().commit();
    }catch (Exception ex) {
        String msg = ex.getLocalizedMessage();
        if (msg == null || msg.length() == 0) {
            Integer id = produto.getCodproduto();
            if (findProduto(id) == null) {
                throw new NonexistentEntityException("The produto with id " + id + " no longer exists.");
            }
        }
        throw ex;
    } finally {
        if (em != null) {
            em.close();
        }
    }
}
  • can you better detail in the example I proposed above? Thank you.

  • Okay, as I understand it, in case I have to make product.set for every field of mine that exists in the database?

  • Is the gets and sets are from your product jpa entity

  • Mark as the correct answer

0

If you haven’t solved it yet, check your persistence.xml settings..

See if it contains this property: name="Hibernate.hbm2ddl.auto" value="update"

If you prefer to use a tool I developed that makes working with Ibernate much easier.

link: https://github.com/KaynanCoelho/generic-repository

Up until.

Browser other questions tagged

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