views:

139

answers:

1

Hey there everyone ...

I´m using JPA with SPRING and mySQL and I´m having problems while removing an entity...

I am doing this:

@PersistenceContext
private EntityManager em;

... @Transactional

public void delete(Long id) {

em.flush();
OwnerEntity e = em.getReference(Entity.class, Long.valueOf(id));

if (e == null)
throw new Exception(Status.SERVER_ERROR);
em.remove(e);
em.flush();

}

+3  A: 

Well, the error is self explaining: you are supposed to run your JPA code inside a transaction and it looks like you aren't, hence the TransactionRequiredException. From its javadoc:

Thrown by the persistence provider when a transaction is required but is not active.

There are many ways to handle transactions with Spring, one of them is to annotate your service with @Transactional (assuming you have <tx:annotation-driven/> in your Spring configuration).

Since you didn't tell us much about the way you use Spring for that, I suggest to check the Chapter 9. Transaction management for more details.

Pascal Thivent
Thanks... but now I´m getting another error: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1 .....Do you now what can be that?
Alucard
@Alucard: Can't answer that from what you're showing. You should update your question to 1. show more code. 2. reflect the changes you made 3. provide the whole stacktrace.
Pascal Thivent