I have jCombobox
where all entidades
students are loaded.
It happens to have two entities with the same name (two students Rodrigo Silva for example). Both are loaded into combo
.
With gravar
data in banco
, it does not matter which of the two entidades
listed with the same name I choose, is always written to id
lower (in this case id
1) .
What am I doing wrong, how can I resolve this?
public T save(T t) throws Exception {
EntityManager em = getEm();
try {
em.getTransaction().begin();
if (t.getId() == null) {
em.persist(t);
} else {
if (em.contains(t)) {
if (em.find(t.getClass(), t.getId()) == null) {
throw new Exception("Erro");
}
}
t = em.merge(t);
}
em.getTransaction().commit();
} finally {
em.close();
}
return t;
}
ocorrencia.setAluno((Aluno) jCAluno.getSelectedItem());
OcorrenciaDao oc = new OcorrenciaDao();
oc.save(ocorrencia);
Method that populates combo
:
public void loadComboAluno() {
AutoCompleteDecorator.decorate(this.jCAluno);
AlunoDao alunoDao = new AlunoDao();
List<Aluno> listaAlunos = alunoDao.consultarAlunos();
for (Aluno set : listaAlunos) {
jCAluno.addItem(set);
}
}
Query method:
public List<Aluno> consultarAlunos() {
EntityManager em = getEm();
List<Aluno> listaAlunos;
try {
Query q = em.createNamedQuery("Aluno.findAll");
listaAlunos = q.getResultList();
} catch (Exception e) {
listaAlunos = new ArrayList<>();
} finally {
em.close();
}
return listaAlunos;
}
Student Class:
@Entity
@DynamicUpdate(value=true)
@NamedQueries({
@NamedQuery(name = "Aluno.findAll", query = "SELECT c FROM Aluno c"),
@NamedQuery(name = "Aluno.findByAlunoId", query = "SELECT c FROM Aluno c WHERE c.id = :id"),
@NamedQuery(name = "Aluno.findByAlunoNome", query = "SELECT c FROM Aluno c WHERE c.nome = :nome")})
public class Aluno implements EntidadeBase, Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "idAluno")
private Integer id;
@Column(name = "nomeAluno")
private String nome;