Why is JPA-Query removing Entities resulting in com.objectdb TransactionRequiredException error?
I am trying to delete objects in a database.
My first try:
public void removeAll(){
TypedQuery<anObject> query = em.createQuery(
"DELETE FROM tablName",
anObject.class);
query.executeUpdate();
}
this gave me an exception, so I took a look at the example on the object and updated my code to be similar to theirs:
public int removeAll(){
int deleted = em.createQuery(
"DELETE FROM tableName").executeUpdate();
}
I am getting the same exception:
com.objectdb.o._TransactionRequiredException: Attempt to run update query when no transaction is active
Does anyone know what I can do to resolve?
+3
source to share
1 answer
I've added an answer here in case anyone else stumbles on this and it might help.
I forgot to add the notation @Transactional
.
The final piece of code looks like this:
@Transactional
public void removeAll(){
TypedQuery<anObject> query = em.createQuery(
"DELETE FROM tableName",
anObject.class);
query.executeUpdate();
}
+1
source to share