I've got this DAO in my Spring App
import javax.transaction.Transactional; @Repository // or @Component @Transactional public class LanguageRepository { // Injected database connection: @PersistenceContext private EntityManager em; // Stores a new guest: @Transactional public Language persist(Language guest) { em.persist(guest); em.flush(); em.refresh(guest); return guest; } @Transactional public boolean update() { return em.createQuery("...").executeUpdate() > 0; } }
While the fetching methods work, there are two huge problems: the persist method does not create a new entity, nor it refreshes with its new id. And the update method fails because of this:
[ObjectDB 2.8.0] javax.persistence.TransactionRequiredException Attempt to run update query when no transaction is active [PMImpl] (error 611)
Here's how I set up the transactionManager:
@Bean public JpaTransactionManager transactionManager() throws ClassNotFoundException { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(entityManagerFactory().getNativeEntityManagerFactory()); transactionManager.afterPropertiesSet(); return transactionManager; }