Spring JPA:成功插入但更新失败(javax.persistence.PersistenceException:托管刷新期间出错)

时间:2016-04-27 03:29:05

标签: java spring hibernate spring-mvc jpa

我的代码有问题,

application.properties

server.port=8080
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/example
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
security.enable-csrf=False
spring.data.elasticsearch.repositories.enabled=false
spring.activemq.pool.enabled=false
spring.activemq.pool.configuration.create-connection-on-startup=false
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=DEBUG
logging.file=tigor.log

Bean(MsParameter.java)

package com.example.db.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "ms_parameter", uniqueConstraints = @UniqueConstraint(columnNames = "parameter_name"))
public class MsParameter /*extends _CoreBeans*/{
    @Id 
    @Column(name="parameter_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int parameter_id;
    private String parameter_name;
    private String parameter_value;


    public int getParameter_id() {
        return parameter_id;
    }
    public void setParameter_id(int parameter_id) {
        this.parameter_id = parameter_id;
    }
    @Column(name = "parameter_name", unique = true, nullable = false, length = 30)
    public String getParameter_name() {
        return parameter_name;
    }
    public void setParameter_name(String parameter_name) {
        this.parameter_name = parameter_name;
    }

    @Column(name = "parameter_value", nullable = false, length = 100)
    public String getParameter_value() {
        return parameter_value;
    }
    public void setParameter_value(String parameter_value) {
        this.parameter_value = parameter_value;
    }

}

存储库(MsParameterRepo.java)

package com.example.db.repository;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

import com.example.db.domain.MsParameter;

public interface MsParameterRepo extends CrudRepository<MsParameter, Integer>{
     @Query("SELECT p FROM MsParameter p WHERE p.parameter_id = :id ")
     MsParameter findById(@Param("id")int id);
}

服务接口(MsParameterService.java)

package com.example.db.service;

import java.util.Collection;

import com.btpns.hobit.db.domain.MsParameter;

public interface MsParameterServiceInterface {
    public MsParameter save(MsParameter p);
    public Boolean delete(Integer Id);
    public MsParameter edit(MsParameter param);
    public MsParameter find(Integer Id);
    public Collection<MsParameter> getAll(); 
}

服务接口实现(MsParameterServiceImpl.java)

package com.example.db.service;

import java.util.Collection;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;

import com.example.db.domain.MsParameter;
import com.example.db.repository.MsParameterRepo;

@Service
@Transactional
public class MsParameterServiceImpl implements MsParameterServiceInterface{

    @Autowired
    protected MsParameterRepo repo;

    @Override
    public MsParameter save(MsParameter p) {
        // TODO Auto-generated method stub
        return repo.save(p);
    }

    @Override
    public Boolean delete(Integer Id) {
        MsParameter p = repo.findOne(Id);

        if (p!=null){
            repo.delete(p);
            return true;
        }

        return false;
    }

    @Override
    public MsParameter edit(MsParameter param) {
        // TODO Auto-generated method stub
        return this.save(param);
    }

    @Override
    public MsParameter find(Integer Id) {
        // TODO Auto-generated method stub
        return repo.findOne(Id);
    }

    @Override
    public Collection<MsParameter> getAll() {
        Iterable<MsParameter> itr = repo.findAll();
        return (Collection<MsParameter>)itr;
    }

}

Controller(SettingParameterController.java)

package com.example.module.settings;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.app.core.CoreController;
import com.example.db.domain.MsParameter;
import com.example.db.domain.ResponseBean;
import com.example.db.repository.MsParameterRepo;
import com.example.db.service.MsParameterServiceInterface;

@RestController
@RequestMapping("/settingparameter")
@Transactional
public class SettingParameterController extends CoreController {
    @Autowired 
    MsParameterRepo msParam;

    @Autowired 
    MsParameterServiceInterface msParameterServiceInterface;
//  
    @RequestMapping(value="/save",method=RequestMethod.POST)
    public ResponseBean save(@RequestBody MsParameter param){
        try {       
            msParameterServiceInterface.save(param); //doesn't work
            return new ResponseBean("", true);
        } catch (Exception e) {
            return new ResponseBean("", false);
        }
    }

}

使用上面的代码, insert is success ,但是当更新错误时(在日志中,执行更新查询但是当flush是错误时)。这是日志错误:

2016-04-27 10:22:34.866 DEBUG 7776 --- [io-8081-exec-11] org.hibernate.loader.Loader              : Result set row: 0
2016-04-27 10:22:34.867 DEBUG 7776 --- [io-8081-exec-11] org.hibernate.loader.Loader              : Result row: EntityKey[com.example.db.domain.MsParameter#34]
2016-04-27 10:22:34.867 DEBUG 7776 --- [io-8081-exec-11] o.h.engine.internal.TwoPhaseLoad         : Resolving associations for [com.example.db.domain.MsParameter#34]
2016-04-27 10:22:34.867 DEBUG 7776 --- [io-8081-exec-11] o.h.engine.internal.TwoPhaseLoad         : Done materializing entity [com.example.db.domain.MsParameter#34]
2016-04-27 10:22:34.867 DEBUG 7776 --- [io-8081-exec-11] o.h.r.j.i.LogicalConnectionManagedImpl   : Initiating JDBC connection release from afterStatement
2016-04-27 10:22:34.868 DEBUG 7776 --- [io-8081-exec-11] org.hibernate.loader.Loader              : Done entity load
2016-04-27 10:22:34.869 DEBUG 7776 --- [io-8081-exec-11] o.h.e.i.AbstractFlushingEventListener    : Processing flush-time cascades
2016-04-27 10:22:34.869 DEBUG 7776 --- [io-8081-exec-11] o.h.e.i.AbstractFlushingEventListener    : Dirty checking collections
2016-04-27 10:22:34.869 DEBUG 7776 --- [io-8081-exec-11] o.h.e.i.AbstractFlushingEventListener    : Flushed: 0 insertions, 1 updates, 0 deletions to 1 objects
2016-04-27 10:22:34.869 DEBUG 7776 --- [io-8081-exec-11] o.h.e.i.AbstractFlushingEventListener    : Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2016-04-27 10:22:34.870 DEBUG 7776 --- [io-8081-exec-11] o.hibernate.internal.util.EntityPrinter  : Listing entities:
2016-04-27 10:22:34.870 DEBUG 7776 --- [io-8081-exec-11] o.hibernate.internal.util.EntityPrinter  : com.example.db.domain.MsParameter{parameter_value=thisisvalue, parameter_name=thisisname, parameter_id=34}
2016-04-27 10:22:34.871 DEBUG 7776 --- [io-8081-exec-11] org.hibernate.SQL                        : 
    update
        ms_parameter 
    set
        parameter_name=?,
        parameter_value=? 
    where
        parameter_id=?
Hibernate: 
    update
        ms_parameter 
    set
        parameter_name=?,
        parameter_value=? 
    where
        parameter_id=?
2016-04-27 10:22:34.872 DEBUG 7776 --- [io-8081-exec-11] o.h.r.j.i.LogicalConnectionManagedImpl   : Initiating JDBC connection release from afterStatement
2016-04-27 10:22:34.876  WARN 7776 --- [io-8081-exec-11] com.arjuna.ats.arjuna                    : ARJUNA012125: TwoPhaseCoordinator.beforeCompletion - failed for SynchronizationImple< 0:ffffc0a8000f:ca1e:57202b23:2f, org.hibernate.resource.transaction.backend.jta.internal.synchronization.RegisteredSynchronization@10f13e52 >

javax.persistence.PersistenceException: error during managed flush
    at org.hibernate.jpa.internal.EntityManagerImpl$CallbackExceptionMapperImpl.mapManagedFlushFailure(EntityManagerImpl.java:240) ~[hibernate-entitymanager-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3142) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2352) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl.beforeCompletion(JtaTransactionCoordinatorImpl.java:316) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.resource.transaction.backend.jta.internal.synchronization.SynchronizationCallbackCoordinatorNonTrackingImpl.beforeCompletion(SynchronizationCallbackCoordinatorNonTrackingImpl.java:47) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.resource.transaction.backend.jta.internal.synchronization.RegisteredSynchronization.beforeCompletion(RegisteredSynchronization.java:37) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:76) ~[jta-5.3.2.Final.jar:5.3.2.Final]
    at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:368) ~[arjuna-5.3.2.Final.jar:5.3.2.Final]
    at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:91) ~[arjuna-5.3.2.Final.jar:5.3.2.Final]
    at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:162) ~[arjuna-5.3.2.Final.jar:5.3.2.Final]
    at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1200) ~[jta-5.3.2.Final.jar:5.3.2.Final]
    at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:126) [jta-5.3.2.Final.jar:5.3.2.Final]
    at org.springframework.transaction.jta.JtaTransactionManager.doCommit(JtaTransactionManager.java:1021) [spring-tx-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761) [spring-tx-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730) [spring-tx-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:485) [spring-tx-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:291) [spring-tx-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) [spring-tx-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655) [spring-aop-4.3.0.RC1.jar:4.3.0.RC1]
    at com.example.db.service.MsParameterServiceImpl$$EnhancerBySpringCGLIB$$f4d2a421.save(<generated>) [classes/:0.0.1-SNAPSHOT]
    at com.example.module.settings.SettingParameterController.save(SettingParameterController.java:38) [classes/:0.0.1-SNAPSHOT]
    at com.example.module.settings.SettingParameterController$$FastClassBySpringCGLIB$$4161873e.invoke(<generated>) [classes/:0.0.1-SNAPSHOT]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) [spring-core-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:651) [spring-aop-4.3.0.RC1.jar:4.3.0.RC1]
    at com.example.module.settings.SettingParameterController$$EnhancerBySpringCGLIB$$e6030a84.save(<generated>) [classes/:0.0.1-SNAPSHOT]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_66]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_66]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_66]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_66]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) [spring-web-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) [spring-web-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114) [spring-webmvc-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) [spring-webmvc-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) [spring-webmvc-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) [spring-webmvc-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) [spring-webmvc-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) [spring-webmvc-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) [spring-webmvc-4.3.0.RC1.jar:4.3.0.RC1]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) [servlet-api.jar:na]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) [spring-webmvc-4.3.0.RC1.jar:4.3.0.RC1]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) [servlet-api.jar:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292) [catalina.jar:8.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [catalina.jar:8.0.33]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-websocket.jar:8.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [catalina.jar:8.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [catalina.jar:8.0.33]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) [spring-web-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.0.RC1.jar:4.3.0.RC1]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [catalina.jar:8.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [catalina.jar:8.0.33]
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:87) [spring-web-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.0.RC1.jar:4.3.0.RC1]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [catalina.jar:8.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [catalina.jar:8.0.33]
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77) [spring-web-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.0.RC1.jar:4.3.0.RC1]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [catalina.jar:8.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [catalina.jar:8.0.33]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121) [spring-web-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.0.RC1.jar:4.3.0.RC1]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [catalina.jar:8.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [catalina.jar:8.0.33]
    at org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:120) [spring-boot-1.4.0.M2.jar:1.4.0.M2]
    at org.springframework.boot.context.web.ErrorPageFilter.access$000(ErrorPageFilter.java:61) [spring-boot-1.4.0.M2.jar:1.4.0.M2]
    at org.springframework.boot.context.web.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:95) [spring-boot-1.4.0.M2.jar:1.4.0.M2]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) [spring-web-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.boot.context.web.ErrorPageFilter.doFilter(ErrorPageFilter.java:113) [spring-boot-1.4.0.M2.jar:1.4.0.M2]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [catalina.jar:8.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [catalina.jar:8.0.33]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) [catalina.jar:8.0.33]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) [catalina.jar:8.0.33]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) [catalina.jar:8.0.33]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) [catalina.jar:8.0.33]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [catalina.jar:8.0.33]
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) [catalina.jar:8.0.33]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) [catalina.jar:8.0.33]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522) [catalina.jar:8.0.33]
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095) [tomcat-coyote.jar:8.0.33]
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672) [tomcat-coyote.jar:8.0.33]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502) [tomcat-coyote.jar:8.0.33]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458) [tomcat-coyote.jar:8.0.33]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_66]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_66]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-util.jar:8.0.33]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_66]
Caused by: java.lang.NullPointerException: null
    at com.arjuna.ats.internal.jdbc.ConnectionManager.create(ConnectionManager.java:91) ~[jdbc-5.3.2.Final.jar:5.3.2.Final]
    at org.springframework.boot.jta.narayana.NarayanaDataSourceBean.getConnection(NarayanaDataSourceBean.java:59) ~[spring-boot-1.4.0.M2.jar:1.4.0.M2]
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:386) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.acquireConnectionIfNeeded(LogicalConnectionManagedImpl.java:84) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.resource.jdbc.internal.LogicalConnectionManagedImpl.getPhysicalConnection(LogicalConnectionManagedImpl.java:109) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.connection(StatementPreparerImpl.java:47) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$1.doPrepare(StatementPreparerImpl.java:87) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:172) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:78) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl.buildBatchStatement(AbstractBatchImpl.java:136) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl.getBatchStatement(AbstractBatchImpl.java:125) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3049) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2985) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3365) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:145) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:560) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:434) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1295) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:468) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3135) ~[hibernate-core-5.1.0.Final.jar:5.1.0.Final]
    ... 86 common frames omitted

2016-04-27 10:22:34.877 DEBUG 7776 --- [io-8081-exec-11] o.h.r.j.i.LogicalConnectionManagedImpl   : Initiating JDBC connection release from afterTransaction
2016-04-27 10:22:34.880 DEBUG 7776 --- [io-8081-exec-11] m.m.a.RequestResponseBodyMethodProcessor : Written [com.example.db.domain.ResponseBean@2044edf3] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@142a179a]
2016-04-27 10:22:34.880 DEBUG 7776 --- [io-8081-exec-11] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2016-04-27 10:22:34.880 DEBUG 7776 --- [io-8081-exec-11] o.h.e.jdbc.internal.JdbcCoordinatorImpl  : HHH000420: Closing un-released batch
2016-04-27 10:22:34.880 DEBUG 7776 --- [io-8081-exec-11] o.s.web.servlet.DispatcherServlet        : Successfully completed request

是否有任何错误配置或如何正确使用spring jpa?谢谢! :)

3 个答案:

答案 0 :(得分:1)

Arghhh,我的坏!删除依赖“narayana”方法后保存工作!

如何删除narayana依赖?打开pom.xml(如果使用maven)并删除以下行:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jta-narayana</artifactId>
</dependency>

谢谢! :)

答案 1 :(得分:0)

由于NullPointerException,更新失败。

    Caused by: java.lang.NullPointerException: null
    at com.arjuna.ats.internal.jdbc.ConnectionManager.create(ConnectionManager.java:91) ~[jdbc-5.3.2.Final.jar:5.3.2.Final]
    at org.springframework.boot.jta.narayana.NarayanaDataSourceBean.getConnection(NarayanaDataSourceBean.java:59) ~[spring-boot-1.4.0.M2.jar:1.4.0.M2]

我猜NarayanaDataSourceBean无法获得JDBC连接。检查您的配置和|或在此处发布NarayanaDataSourceBean.getConnection()方法

答案 2 :(得分:0)

在两次尝试持久/合并传入的MsParameter对象时,该对象始终是暂时的,根据您的评论,它没有id。进行更新时需要id值。

以下代码段可能会有所帮助。

class MsParameterServiceImpl ...{

    ...

    public MsParameter save(MsParameter incoming){
        MsParameter merged = null;
        if(incoming.getId()!=null){ //Going to update
            merged = repo.findOne(incoming);

        }else{//id param is null, then assume it is new
            merged = incoming;
            //Make sure database id column is auto_increment 
        }

        //Identifying the save/update is up to you


        return repo.save(merged);
    }
    ...

}

在您的控制器中,您不会返回已保存的对象。那么用户如何在下次更新特定实体/对象时不知道它id?用户应通过用户界面提供id或让数据库分配自动生成的值。

检查:http://docs.oracle.com/javaee/6/api/javax/persistence/GenerationType.html#IDENTITY

请注意:您的控制器和服务中也有@Transactional。这可能会导致嵌套的事务异常或类似的东西