Skip to content

Implementação da Classe - PreexistingEntityException #175

@iancl100

Description

@iancl100

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();
            }
        }
    }

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions