Bom dia estou com o seguinte problema:
- Tenho a tabela paciente e a tabela telefone
- 1 paciente tem uma lista de telefones
Segue as variáveis:
@Entity
Paciente
@OneToMany(mappedBy = "paciente", orphanRemoval = true, targetEntity = Telefone.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Telefone> telefones;
@Entity
Telefone
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "idPaciente")
private Paciente paciente;
Meu generic dao:
public void salvar(Entidade entidade) {
Session sessao = ConnectionFactory.getFabricaDeSessoes().openSession();
Transaction transacao = null;
try {
transacao = sessao.beginTransaction();
sessao.save(entidade);
transacao.commit();
} catch (RuntimeException erro) {
if (transacao != null) {
transacao.rollback();
}
throw erro;
} finally {
sessao.close();
}
}
Metodo que estou utilizando para testar persistencia:
Paciente paciente = new Paciente();
paciente.setNome("Henrique");
paciente.setCpf("186.200.191-23");
paciente.setDataNascimento(new Date(System.currentTimeMillis()));
paciente.setSexo(Sexo.MASCULINO);
paciente.setEmail("henrique@gmail.com");
paciente.setEndereco(new Endereco("88131-743", "Braulina Goulart", "48", "RioGrande", "", "SC"));
paciente.setTipoSanguineo("O+");
List<Telefone> telefones = new ArrayList();
telefones.add(new Telefone("(48)996850323", "Celular", "João", "Irmão", true));
telefones.add(new Telefone("(48)996850323", "Celular", "Henrique", "Pai", true));
paciente.setTelefones(telefones);
dao.salvar(paciente);
Do jeito q ta salva o telefone e o paciente, mais la na tabela de telefone o idPaciente fica null, ai quando busco por paciente n vem os telefones