I\'m having problems persisting entities in my Spring application. Just doing em.merge()
finishes without errors but does not update the database, and trying
Just like @Neil Stockton mentioned, some EntityManager
functions require a Transaction to exist. see the doc
Here is the doc
about flush
function:
void flush() Synchronize the persistence context to the underlying database. Throws: TransactionRequiredException - if there is no transaction or if the entity manager has not been joined to the current transaction PersistenceException - if the flush fails
The same goes for Merge
function as well:
T merge(T entity) Merge the state of the given entity into the current persistence context. Parameters: entity - entity instance Returns: the managed instance that the state was merged to Throws: IllegalArgumentException - if instance is not an entity or is a removed entity TransactionRequiredException - if there is no transaction when invoked on a container-managed entity manager of that is of type PersistenceContextType.TRANSACTION
So basically what you want is to create new transaction
or to use an exists
one. Since you are using @EnableTransactionManagement
annotation you can just annotate your method in order to do so
@Transactional(propagation=Propagation.REQUIRED)
public Project save(Project project) {
project = em.merge(project);
em.flush();
return project;
}
More about Spring
Transactions can be found at the spring docs. More the transactions
in java can be found at ibm which is highly recommended.
Add this to your datasource file/config
hibernate.allow_update_outside_transaction: true
It should work, but be sure of your production environment though