Problem on JPA: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
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();
}
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.
精彩评论