[java-2.4] JPA remove doesn't delete entity

162 views
Skip to first unread message

Johan Dahlberg

unread,
Aug 20, 2015, 3:23:47 PM8/20/15
to play-framework
I am using JPA and almost everything works except my delete method. What could cause this to not work? ClassType is the model class, for example User.class.

What could cause this?

public Boolean delete(Long id) {
    try {
        return jpaApi.withTransaction(() -> {
            try {
                CriteriaBuilder cb = JPA.em().getCriteriaBuilder();
                CriteriaQuery<T> cq = cb.createQuery(classType);
                Root<T> root = cq.from(classType);
                cq.where(cb.equal(root.get("id"), id));
                try {
                    T entity = JPA.em().createQuery(cq).getSingleResult();
                    JPA.em().remove(entity);
                    return true;
                } catch(NoResultException ex) {
                    return false;
                }
            } catch(Exception ex) {
                logger.error(ex.getMessage());
                return false;
            }
        });
    } catch (Throwable throwable) {
        return false;
    }
}

/Johan

Johan Andrén

unread,
Aug 21, 2015, 4:02:19 AM8/21/15
to play-framework
If you get an exception from the transaction commit you will swallow that exception and just return false.

Maybe you would get more info if you put a logger.error(throwable) there as well?

--
Johan Andrén
Typesafe -  Reactive apps on the JVM
Twitter: @apnylle

Johan Dahlberg

unread,
Aug 21, 2015, 5:03:17 AM8/21/15
to play-framework
Ah, I forgot to say that the method returns true.

/Johan
Reply all
Reply to author
Forward
0 new messages