gpt4 book ai didi

java - Spring DATA JPA 以 Hibernate 作为 JPA 提供者

转载 作者:行者123 更新时间:2023-12-01 20:15:20 26 4
gpt4 key购买 nike

将 Spring boot 应用程序与 spring data jpa 一起使用,并使用 hibernate 作为 jpa 提供程序。

在一个扩展 JpaRepository 的接口(interface)中创建了一个默认方法。

请检查代码。

public interface UserDao extends JpaRepository<User, UUID> {

default void softDelete(UUID id) {
User existingUser = findOne(id);

if (existingUser != null)
existingUser.setActive(false);

throw new EntityNotFoundException();
}
}

现在,当我从服务层调用此softDelete()时,它不会返回现有用户。它返回为 NULL。

这是日志堆栈跟踪。

    2017-08-27 13:25:01 [http-nio-8080-exec-2] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaObjectRetrievalFailureException: nested exception is javax.persistence.EntityNotFoundException] with root cause
javax.persistence.EntityNotFoundException: null
at org.rout.projmgmt.dao.UserDao.softDelete(UserDao.java:31)
at java.lang.invoke.MethodHandle.invokeWithArguments(MethodHandle.java:627)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:62)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy107.softDelete(Unknown Source)
at org.rout.projmgmt.service.user.UserServiceImpl.delete(UserServiceImpl.java:53)
at org.rout.projmgmt.controller.UserController.delete(UserController.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doDelete(FrameworkServlet.java:894)

知道我缺少什么吗???

最佳答案

有几件事:

  1. 无论您是否获取记录,您总是会收到 EntityNotFoundException 异常,因为我假设您希望在找不到任何记录时发送该异常。
  2. 您可能也想返回用户,但您的返回类型为 void。我会将返回类型修改为 User,如下所示:

    default User softDelete(UUID id) {
    User existingUser = findOne(id);
    if (existingUser != null) {
    existingUser.setActive(false);
    } else {
    throw new EntityNotFoundException();
    }
    return existingUser;//you may want to save this user again with isActive flag now false
    }

关于java - Spring DATA JPA 以 Hibernate 作为 JPA 提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45904594/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com