由CrudRepository保存方法

时间:2017-12-01 06:51:31

标签: exception spring-boot save constraints crud

我正在使用CrudRepository存储方法在我的Spring启动项目中持久化Profile Object,如下所示:

MyService2.java

public Profile getProfile(String profileId, String email){
    Profile profile = null;
    MyClass myObj=new MyClass();
    Profile profile = new Profile(profileId, email);
    return myRepository.save(profile);;
}

MyService1.java:

public Profile getProfile(String profileId, String email){
    try{
        return myService2.getProfile(profileId,email);
    }
    catch(Exception e){
    {
        throw new MyCustomException(errorMessage, errorCode);
    }

}

MyRepository.java

public interface MyRepository extends PagingAndSortingRepository<Profile, String> {

 ........
}

Profile.java:

@Entity
public class Profile extends BaseDomain {
   @Id
   private String id;
   private String username;

   public Profile() {
    //this is no argument constructor
   }

   public Profile(final String id,final String email) {
    this.id = id;
    this.username= email;
   }

   //Getter and Setter
 }

在Profile对象中,profileId是primaryKey,并且在Database表中应用了唯一约束电子邮件字段。

Unique constraint in DB

从另一个Service MyService1类调用getProfile方法,并从控制器调用该服务类方法。

现在发出我面临的问题即使我在数据库中已经提供的方法中传递了emailId,正在创建配置文件对象,并且MyService1中没有捕获异常。约束违例异常仅在Controller中被捕获,我们正在进一步使用创建的配置文件对象。

控制器中的异常消息如下:

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

StackTrace如下:

  

org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:278),org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244),org.springframework.orm .jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521),org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761),org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730 ),org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:483),org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:290),org.springframework.transaction.interceptor.TransactionInterceptor。 invoke(TransactionInterceptor.java:96),org.springframework.aop.framework flectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179),org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92),org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179),org .springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213),com.sun.proxy。$ Proxy147.createProfile(Unknown Source),com.cp.controllers.api.MyController.createAccount(MyController.java:827 ),com.cp.controllers.api.MyController $$ FastClassBySpringCGLIB $$ 5cafd35f.invoke(),org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204),org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720),org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157),org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62), org.springframework .aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179),org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62),org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java) :179),org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92),org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179),org.springframework.aop.framework。 CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:655),com.cp.controllers.api.MyController $$ EnhancerBySpringCGLIB $$ 1e2efb29.createAccount(),sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method),sun.reflect.NativeMethodAccessorImpl .invoke(NativeMethodAccessorImpl.java:62),sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43),java.lang.reflect.Method.invoke(Method.java:498),org.springfram ework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221),org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136),org.springframework.web.servlet.mvc。 method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114),org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827),org.springframework.web.servlet.mvc。 method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738),org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85),org.springframework.web.servlet.DispatcherServlet.doDispatch( DispatcherServlet.java:963),org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897),org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServ let.java:970),org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872),javax.servlet.http.HttpServlet.service(HttpServlet.java:648),org.springframework.web.servlet .FrameworkServlet.service(FrameworkServlet.java:846),javax.servlet.http.HttpServlet.service(HttpServlet.java:729),org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230),org。 apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165),org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52),org.apache.catalina.core.ApplicationFilterChain.internalDoFilter( ApplicationFilterChain.java:192),org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165),org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:208),org.springframework.security .web.FilterChainProxy.doFilter(FilterChainProxy.java:177),org.springframework.web.filter.DelegatingFilterProxy.invok eDelegate(DelegatingFilterProxy.java:346),org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262),org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192),org.apache .catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165),org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99),org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java) :107),org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192),org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165),org.springframework.web.filter。 HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:87),org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107),org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain。 java:192),org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165),org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77),org.springframework.web.filter .OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107),org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192),org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165), org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197),org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107),org.apache.catalina.core.ApplicationFilterChain.internalDoFilter( ApplicationFilterChain.java:192),org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165),org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198),org.apache.catalina .core.StandardCon textValve.invoke(StandardContextValve.java:108),org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:522),org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140),org .apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79),org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87),org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter) .java:349),org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:1110),org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66),org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:785),org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1425),org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase。 java:49),java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142),java.util.concurrent.ThreadPoolExecutor $ Worker.run(T hreadPoolExecutor.java:617),org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61),java.lang.Thread.run(Thread.java:745)

当我在getProfile方法中将profileId传递为null时,在MyService1 catch块中成功捕获了Exception。

任何人都可以解释一下CrudRepository Save方法的这种行为。

由于

0 个答案:

没有答案