A respectiva classe PreexistingEntityException, pelo que foi verificado, não há nenhuma função para ela em nenhuma outra classe existente. Então sugiro a seguinte função:
- Tratar a ocorrência de inserção repetida no banco de dados dos objetos manejados pelas classe do datamapper.
Ou seja, caso ocorra a tentativa de inserção repetida seja lançada a PreexistingEntityException.
Antes
public void create(Aula aula) {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(aula);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
Depois
public void create(Aula aula) throws PreexistingEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(aula);
em.getTransaction().commit();
} catch(Exception ex){
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
Long id = aula.getId();
if (findAula(id) != null) {
throw new PreexistingEntityException(aula.getClass(), aula, ex);
}
}
throw ex;
}finally {
if (em != null) {
em.close();
}
}
}
A respectiva classe
PreexistingEntityException, pelo que foi verificado, não há nenhuma função para ela em nenhuma outra classe existente. Então sugiro a seguinte função:Ou seja, caso ocorra a tentativa de inserção repetida seja lançada a PreexistingEntityException.
Antes
Depois