How to save a list using Hibernate?

Asked

Viewed 492 times

0

How to save an entire list using Hibernate? I’m trying to save a list this way:

classe.setMinhaLista(minhaLista);
classeDao.salvar(classe);

And so:

classe.setMinhaLista(new ArrayList<Objeto>());

for(Objeto obj : minhaLista) {
     classe.getMinhaLista().add(obj);
}

classeDao.salvar(classe);

But I’m not getting either way and the glassfish console returns no error.

Entidade Minhalista

@Entity
public class MinhaLista implements Serializable {

private static final long serialVersionUID = 1L;

private Long idMinhaLista;
private Classe classe;

public void setClasse(Classe classe) {
    this.classe = classe
}

@ManyToOne
public void getClasse() {
    return classe;
}

// Restante dos gets e setter omitidos
}

Entity Classe

@Entity
public class Classe implements Serializable {

private static final long serialVersionUID = 1L;

private Long idClasse;
private List<MinhaLista> minhaLista;

public void setMinhaLista(List<MinhaLista> minhaLista) {
    this.minhaLista = minhaLista;
}

@OneToMany(mappedBy = "classe",cascade={CascadeType.ALL})
public List<MinhaLista> getMinhaLista() {
    return minhaLista;
}

// Restante dos getters e setters omitidos
}
  • You tried to use the Cascade?

  • Already, I tried Cascade ALL, ta salvando a minhalista, but is saving the list loose, without linking to the class object

  • You could post the code of your entities ?

  • I edited the question with the code

2 answers

0

I believe the list is being saved because it is empty, if you fill in the objects you can link to the list, tbm it is good to add the class property to the list, so that the system prevents you from saving a list without class

public class Classe {
    private Long id;    
    @OneToMany(mappedBy = "classe",cascade = CascadeType.PERSIST)
    private List<Lista> minhaLista;
}

The PERSIST tab will save the list if it is in mode transient

public class Lista {
    @NotNull
    @ManyToOne(optional = false)
    private Classe classe;
}

The Optional=false will force the field to have a class, Notnull will add a validation and if you are using Hibernate validation, it will prevent you from saving the entire transaction.

And before saving, you must circulate the assignment

classe.getMinhaLista().forEach(lista->lista.setClasse(classe));
  • The list is not empty, if I put Cascade ALL or PERSIST it saves, but without linking to the class object

  • important points, it is saving the list without link because vc did not add the property that makes the link mandatory, if it is saving without link or vc did not pass the object to link or the object is null

0

I was able to save the list items, all at once, that way:

  1. Save each item from the drop-down list to the database and add it to my list

      objeto.setNome(nome);
      objetoDao.salvar(objeto);
      minhaLista.add(objeto);
    
  2. Soon after, I walk through the minhalista and update each object, linking it to the class

      for(Objeto obj : minhaLista) {
          obj.setClasse(classe);
          objetoDao.atualizar(obj);
      }
    

Browser other questions tagged

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