Search code examples
javajpaobjectdb

Why is JPA-Query to delete Entities resulting in a com.objectdb TransactionRequiredException?


I'm trying to delete the objects in the database.

My first attempt was:

 public void removeAll(){
    TypedQuery<anObject> query = em.createQuery(
            "DELETE FROM tablName",
            anObject.class);
    query.executeUpdate();
}

this gave me an exception so I had a look at the example on the objected site and updated my code to resemble theirs:

  public int removeAll(){
        int deleted = em.createQuery(
                "DELETE FROM tableName").executeUpdate();
    }

I'm getting the same exception:

com.objectdb.o._TransactionRequiredException: Attempt to run update query when no transaction is active

anyone know what I can do to solve?


Solution

  • I've added an answer here just in case someone else stumbles across this and it might help.

    I had forgotten to add @Transactional notation.

    The final code snippet looks like:

       @Transactional
    public void removeAll(){
        TypedQuery<anObject> query = em.createQuery(
                "DELETE FROM tableName",
                anObject.class);
        query.executeUpdate();
    }