Spring JPA: no transaction is in progress

2020-03-30 01:26发布

问题:

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 em.flush() causes this exception:

2015-12-19 18:01:40 DEBUG DispatcherServlet:976 - Could not complete request org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress

This is my configuration:

@Configuration
@EnableWebMvc
@ComponentScan
@Import({ SecurityConfig.class })
@PropertySource("classpath:application.properties")
@EnableJpaRepositories
@EnableTransactionManagement
public class ApplicationContext extends WebMvcConfigurerAdapter {

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource datasource = new DriverManagerDataSource();
        datasource.setDriverClassName("org.postgresql.Driver");
        datasource.setUrl("jdbc:postgresql://localhost/expert");
        datasource.setUsername("expert");
        datasource.setPassword("expert");
        return datasource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource());
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean factoryBean
                = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(dataSource());
        factoryBean.setPackagesToScan(new String[ ] { "test.model" });
        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        factoryBean.setJpaVendorAdapter(vendorAdapter);
        factoryBean.setJpaProperties(this.additionalProperties());
        return factoryBean;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
        return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.show_sql", "true");
        properties.setProperty("hibernate.format_sql", "false");
        properties.setProperty("hibernate.hbm2ddl.auto", "update");
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
        return properties;
    }

}

My service:

@Service
@Transactional
public class ApiProjectServiceImpl implements ApiProjectService {

    @Autowired
    private ApiProjectRepository apiProjectRepository;

    public Project getProject(Integer id) {
        return apiProjectRepository.getProject(id);
    }

    public Project save(Project project) { return apiProjectRepository.save(project); }

}

And the repository:

@Repository
public class ApiProjectRepositoryImpl implements ApiProjectRepository {

    @PersistenceContext
    private EntityManager em;

    public Project getProject(Integer id) {
        Project project = em.createQuery("select p from Project p where id = " + id, Project.class)
                .getSingleResult();
        return project;
    }

    public Project save(Project project) {
        project = em.merge(project);
        em.flush();
        return project;
    }

}

Stacktrace when trying save():

2015-12-19 18:15:05 DEBUG FilterSecurityInterceptor:215 - Authorization successful
2015-12-19 18:15:05 DEBUG FilterSecurityInterceptor:227 - RunAsManager did not change Authentication object
2015-12-19 18:15:05 DEBUG FilterChainProxy:323 - /api/project reached end of additional filter chain; proceeding with original chain
2015-12-19 18:15:05 DEBUG OpenEntityManagerInViewFilter:160 - Opening JPA EntityManager in OpenEntityManagerInViewFilter
2015-12-19 18:15:05 DEBUG DispatcherServlet:823 - DispatcherServlet with name 'dispatcher' processing POST request for [/api/project]
2015-12-19 18:15:05 DEBUG RequestMappingHandlerMapping:229 - Looking up handler method for path /api/project
2015-12-19 18:15:05 DEBUG RequestMappingHandlerMapping:234 - Returning handler method [public test.model.Project test.api.ApiController.saveProject(test.model.Project)]
2015-12-19 18:15:05 DEBUG DefaultListableBeanFactory:246 - Returning cached instance of singleton bean 'apiController'
2015-12-19 18:15:05 DEBUG RequestResponseBodyMethodProcessor:140 - Reading [class test.model.Project] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@71528048]
2015-12-19 18:15:05 DEBUG Loader:2136 - Loading entity: [test.model.Project#16623]
2015-12-19 18:15:05 DEBUG SQL:109 - select project0_.id as id1_6_1_, project0_.awarded as awarded2_6_1_, project0_.broker as broker3_6_1_, project0_.brokerEmail as brokerEm4_6_1_, project0_.brokerPhone as [...]
2015-12-19 18:15:05 DEBUG LogicalConnectionImpl:226 - Obtaining JDBC connection
2015-12-19 18:15:05 DEBUG DriverManagerDataSource:162 - Creating new JDBC DriverManager Connection to [jdbc:postgresql://localhost/expert]
2015-12-19 18:15:05 DEBUG LogicalConnectionImpl:232 - Obtained JDBC connection
2015-12-19 18:15:05 DEBUG Loader:951 - Result set row: 0
2015-12-19 18:15:05 DEBUG Loader:1485 - Result row: null, EntityKey[test.model.Project#16623]
2015-12-19 18:15:05 DEBUG Loader:1336 - Result set contains (possibly empty) collection: [test.model.Project.assignedExperts#16623]
2015-12-19 18:15:05 DEBUG TwoPhaseLoad:160 - Resolving associations for [test.model.Project#16623]
2015-12-19 18:15:05 DEBUG Loader:2136 - Loading entity: [test.model.Company#3]
2015-12-19 18:15:05 DEBUG SQL:109 - select company0_.id as id1_1_0_, company0_.email as email2_1_0_, company0_.name as name3_1_0_ from Company company0_ where company0_.id=?
2015-12-19 18:15:05 DEBUG Loader:951 - Result set row: 0
2015-12-19 18:15:05 DEBUG Loader:1485 - Result row: EntityKey[test.model.Company#3]
2015-12-19 18:15:05 DEBUG TwoPhaseLoad:160 - Resolving associations for [test.model.Company#3]
2015-12-19 18:15:05 DEBUG TwoPhaseLoad:286 - Done materializing entity [test.model.Company#3]
2015-12-19 18:15:05 DEBUG Loader:2160 - Done entity load
2015-12-19 18:15:05 DEBUG TwoPhaseLoad:286 - Done materializing entity [test.model.Project#16623]
2015-12-19 18:15:05 DEBUG CollectionLoadContext:232 - 1 collections were found in result set for role: test.model.Project.assignedExperts
2015-12-19 18:15:05 DEBUG CollectionLoadContext:280 - Collection fully initialized: [test.model.Project.assignedExperts#16623]
2015-12-19 18:15:05 DEBUG CollectionLoadContext:240 - 1 collections initialized for role: test.model.Project.assignedExperts
2015-12-19 18:15:05 DEBUG Loader:2160 - Done entity load
2015-12-19 18:15:05 DEBUG Loader:2136 - Loading entity: [test.model.Logging#35672]
2015-12-19 18:15:05 DEBUG SQL:109 - select logging0_.id as id1_4_0_, logging0_.date as date2_4_0_, logging0_.message as message3_4_0_, logging0_.project_id as project_5_4_0_, logging0_.username as username4_4_0_ from [...]
2015-12-19 18:15:05 DEBUG Loader:951 - Result set row: 0
2015-12-19 18:15:05 DEBUG Loader:1485 - Result row: EntityKey[test.model.Logging#35672]
2015-12-19 18:15:05 DEBUG TwoPhaseLoad:160 - Resolving associations for [test.model.Logging#35672]
2015-12-19 18:15:05 DEBUG TwoPhaseLoad:286 - Done materializing entity [test.model.Logging#35672]
2015-12-19 18:15:05 DEBUG Loader:2160 - Done entity load
2015-12-19 18:15:05 DEBUG Loader:2136 - Loading entity: [test.model.Logging#35673]
2015-12-19 18:15:05 DEBUG SQL:109 - select logging0_.id as id1_4_0_, logging0_.date as date2_4_0_, logging0_.message as message3_4_0_, logging0_.project_id as project_5_4_0_, logging0_.username as username4_4_0_ from [...]
2015-12-19 18:15:05 DEBUG Loader:951 - Result set row: 0
2015-12-19 18:15:05 DEBUG Loader:1485 - Result row: EntityKey[test.model.Logging#35673]
2015-12-19 18:15:05 DEBUG TwoPhaseLoad:160 - Resolving associations for [test.model.Logging#35673]
2015-12-19 18:15:05 DEBUG TwoPhaseLoad:286 - Done materializing entity [test.model.Logging#35673]
2015-12-19 18:15:05 DEBUG Loader:2160 - Done entity load
2015-12-19 18:15:05 DEBUG Loader:2136 - Loading entity: [test.model.Party#4903]
2015-12-19 18:15:05 DEBUG SQL:109 - select party0_.id as id1_5_0_, party0_.addressCity as addressC2_5_0_, party0_.addressStreet as addressS3_5_0_, party0_.addressZip as addressZ4_5_0_, party0_.email as email5_5_0_, party0_.expert as [...]
2015-12-19 18:15:05 DEBUG Loader:951 - Result set row: 0
2015-12-19 18:15:05 DEBUG Loader:1485 - Result row: EntityKey[test.model.Party#4903]
2015-12-19 18:15:05 DEBUG TwoPhaseLoad:160 - Resolving associations for [test.model.Party#4903]
2015-12-19 18:15:05 DEBUG TwoPhaseLoad:286 - Done materializing entity [test.model.Party#4903]
2015-12-19 18:15:05 DEBUG Loader:2160 - Done entity load
2015-12-19 18:15:05 DEBUG ExceptionHandlerExceptionResolver:132 - Resolving exception from handler [public test.model.Project test.api.ApiController.saveProject(test.model.Project)]: org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress
2015-12-19 18:15:05 DEBUG ResponseStatusExceptionResolver:132 - Resolving exception from handler [public test.model.Project test.api.ApiController.saveProject(test.model.Project)]: org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress
2015-12-19 18:15:05 DEBUG DefaultHandlerExceptionResolver:132 - Resolving exception from handler [public test.model.Project test.api.ApiController.saveProject(test.model.Project)]: org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress
2015-12-19 18:15:05 DEBUG DispatcherServlet:976 - Could not complete request
org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:413)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:157)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at com.sun.proxy.$Proxy48.save(Unknown Source)
    at test.api.ApiProjectServiceImpl.save(ApiProjectServiceImpl.java:24)
    at test.api.ApiController.saveProject(ApiController.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:177)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.persistence.TransactionRequiredException: no transaction is in progress
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.checkTransactionNeeded(AbstractEntityManagerImpl.java:1171)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1332)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:342)
    at com.sun.proxy.$Proxy39.flush(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:262)
    at com.sun.proxy.$Proxy39.flush(Unknown Source)
    at test.api.ApiProjectRepositoryImpl.save(ApiProjectRepositoryImpl.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
    ... 79 more
2015-12-19 18:15:05 DEBUG OpenEntityManagerInViewFilter:185 - Closing JPA EntityManager in OpenEntityManagerInViewFilter
2015-12-19 18:15:05 DEBUG EntityManagerFactoryUtils:435 - Closing JPA EntityManager
2015-12-19 18:15:05 DEBUG LogicalConnectionImpl:246 - Releasing JDBC connection
2015-12-19 18:15:05 DEBUG LogicalConnectionImpl:264 - Released JDBC connection
2015-12-19 18:15:05 DEBUG SecurityContextPersistenceFilter:97 - SecurityContextHolder now cleared, as request processing completed

Thanks!

Update 1 Apparently the problem has been solved by upgrading from Spring 3.2.8 to Spring 4.2.0. I didn't make any changes to the code, I'm aware that a transaction is needed but the service implementation is annotated with @Transactional. Thanks all.

回答1:

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.