Estou tentando fazer uma inserção no banco de dados. Existe uma tabela pessoas e a mesma pode ter vários telefones, poré na hora que dou o persist, insere a pessoa, mas o list do telefone insere só o último valor.
Classe pessoa
package dominio;
import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "pessoa")
public class Pessoa implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id_pessoa")
private Integer idPessoa;
@Column(name = "nome")
private String nome;
@OneToMany(mappedBy = "idPessoa", cascade=CascadeType.ALL)
private List<Telefone> telefoneCollection ;
public Pessoa() {
}
public Pessoa(Integer idPessoa) {
this.idPessoa = idPessoa;
}
public Pessoa(Integer idPessoa, String nome) {
this.idPessoa = idPessoa;
this.nome = nome;
}
public Integer getIdPessoa() {
return idPessoa;
}
public void setIdPessoa(Integer idPessoa) {
this.idPessoa = idPessoa;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public List<Telefone> getTelefoneCollection() {
return telefoneCollection;
}
public void setTelefoneCollection(List<Telefone> telefoneCollection) {
this.telefoneCollection = telefoneCollection;
}
}
Classe telefone
package dominio;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "telefone")
public class Telefone implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id_telefone")
private Integer idTelefone;
@Column(name = "numero")
private String numero;
@JoinColumn(name = "id_pessoa", referencedColumnName = "id_pessoa")
@ManyToOne(cascade=CascadeType.ALL, targetEntity=Pessoa.class)
private Pessoa idPessoa;
public Telefone() {
}
public Telefone(Integer idTelefone) {
this.idTelefone = idTelefone;
}
public Telefone(Integer idTelefone, String numero) {
this.idTelefone = idTelefone;
this.numero = numero;
}
public Telefone(String numero) {
this.numero = numero;
}
public Integer getIdTelefone() {
return idTelefone;
}
public void setIdTelefone(Integer idTelefone) {
this.idTelefone = idTelefone;
}
public String getNumero() {
return numero;
}
public void setNumero(String numero) {
this.numero = numero;
}
public Pessoa getIdPessoa() {
return idPessoa;
}
public void setIdPessoa(Pessoa idPessoa) {
this.idPessoa = idPessoa;
}
}
Método salvar do Dao Genérico
public static <T> T salvar(T entity) throws Exception {
EntityManager em = getEntityManager();
Exception e = null;
T retornoEntity = null;
EntityTransaction t = em.getTransaction();
try {
t.begin();
em.persist(entity);
em.flush();
t.commit();
retornoEntity = entity;
} catch (Exception ex) {
if (t.isActive()) {
em.getTransaction().rollback();
}
e = ex;
}
if (e != null) {
throw e;
}
return retornoEntity;
}
Classe que estou fazendo o teste
package controle;
import dao.UtilsCrud;
import dominio.Pessoa;
import dominio.Telefone;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NewMain {
public static void main(String[] args) {
List lista = new ArrayList<Telefone>();
Pessoa pessoa = new Pessoa();
pessoa.setNome("teste list");
Telefone t = new Telefone();
t.setIdPessoa(pessoa);
t.setNumero("1111-1111");
lista.add(t);
t.setIdPessoa(pessoa);
t.setNumero("2222-2222");
lista.add(t);
pessoa.setTelefoneCollection(lista);
UtilsCrud crud = new UtilsCrud();
try {
pessoa = crud.salvar(pessoa);
} catch (Exception ex) {
Logger.getLogger(NewMain.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Obrigado desde já.