JpaRepository 连接问题,无法通过登录找到用户
Posted
技术标签:
【中文标题】JpaRepository 连接问题,无法通过登录找到用户【英文标题】:Problem with JpaRepository connection, cannot find a user by login 【发布时间】:2021-01-29 03:15:11 【问题描述】:我做了一个简单的 crud 程序。 我使用过 Spring 审计 目前一切正常: 当我发布新的“库存”时没有问题,但是当我尝试更新它时,我收到了这个错误 我很久没见过 ***Error 问题是当我向我的数据库添加一个新对象时,它会通过登录到存储库中找到用户, 与班级:
public User findUserByLogin(String login)
return userRepository.findByLogin(login);
public User getThisUser()
return findUserByLogin(request.getUserPrincipal().getName());
存储库:
@Query("select u from User u where lower(u.login) = lower(?1) ")
User findByLogin(String login);
当我更新已经存在的对象时,它会抛出 null,并试图找到它卡住的东西。 我是不是忘记了什么? 我不添加 DTO,我认为该类在这里没有问题。 (我认为:))
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Inventory extends BaseEntity
@Embedded
private EntityDescription entityDescription;
private BigDecimal price;
public EntityDescription getEntityDescription()
return entityDescription;
public void setEntityDescription(EntityDescription entityDescription)
this.entityDescription = entityDescription;
public BigDecimal getPrice()
return price;
public void setPrice(BigDecimal price)
this.price = price;
基础实体:
@MappedSuperclass
public class BaseEntity extends AbstractAuditable<User, Long>
@Version
private Long version;
public Long getVersion()
return version;
public void setVersion(Long version)
this.version = version;
@Override
public String toString()
return "BaseEntity" +
"version=" + version +
'';
@Service
public class UserService
private final HttpServletRequest request;
private final UserRepository userRepository;
private final UserRoleRepository userRoleRepository;
public UserService(HttpServletRequest request,
UserRepository userRepository,
UserRoleRepository userRoleRepository)
this.request = request;
this.userRepository = userRepository;
this.userRoleRepository = userRoleRepository;
public List<User> findAll()
return userRepository.findAll();
public User findUserByLogin(String login)
return userRepository.findByLogin(login);
public User getThisUser()
return findUserByLogin(request.getUserPrincipal().getName());
@GetMapping("/inventory")
public List<InventoryDTO> allInventory()
return inventoryService.findAll();
@PostMapping("/inventory")
public InventoryDTO addInventory(@RequestBody InventoryDTO inventoryDTO)
return inventoryService.add(inventoryDTO);
@GetMapping("/inventory/id")
public InventoryDTO getInventoryById(@PathVariable Long id)
return inventoryService.findById(id);
@DeleteMapping("/inventory/id")
public List<InventoryDTO> deleteItem(@PathVariable Long id)
inventoryService.remove(id);
return inventoryService.findAll();
@PostMapping("/inventory/id")
public InventoryDTO updateInventory(@PathVariable Long id, @RequestBody InventoryDTO inventoryDTO)
return inventoryService.update(id, inventoryDTO);
还有错误:
java.lang.***Error: null
at java.base/java.net.URL.<init>(URL.java:605) ~[na:na]
at java.base/java.net.URL.<init>(URL.java:553) ~[na:na]
at java.base/jdk.internal.loader.URLClassPath$FileLoader.getResource(URLClassPath.java:1222) ~[na:na]
at java.base/jdk.internal.loader.URLClassPath.getResource(URLClassPath.java:317) ~[na:na]
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:718) ~[na:na]
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:644) ~[na:na]
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602) ~[na:na]
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na]
at java.base/java.lang.ClassLoader.defineClass1(Native Method) ~[na:na]
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016) ~[na:na]
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:151) ~[na:na]
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:823) ~[na:na]
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:721) ~[na:na]
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:644) ~[na:na]
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602) ~[na:na]
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na]
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1533) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.hibernate.query.internal.AbstractProducedQuery.getSingleResult(AbstractProducedQuery.java:1581) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:196) ~[spring-data-jpa-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:88) ~[spring-data-jpa-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:154) ~[spring-data-jpa-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:142) ~[spring-data-jpa-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor$QueryMethodInvoker.invoke(QueryExecutorMethodInterceptor.java:195) ~[spring-data-commons-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:152) ~[spring-data-commons-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130) ~[spring-data-commons-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80) ~[spring-data-commons-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367) ~[spring-tx-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118) ~[spring-tx-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:149) ~[spring-data-jpa-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.2.9.RELEASE.jar:5.2.9.RELEASE]
at com.sun.proxy.$Proxy102.findByLogin(Unknown Source) ~[na:na]
at com.teamcompetencymatrix.www.service.UserService.findUserByLogin(UserService.java:31) ~[classes/:na]
at com.teamcompetencymatrix.www.service.UserService.getThisUser(UserService.java:35) ~[classes/:na]
at com.teamcompetencymatrix.www.config.AuditorAwareImpl.getCurrentAuditor(AuditorAwareImpl.java:24) ~[classes/:na]
at org.springframework.data.auditing.AuditingHandler.lambda$touchAuditor$6(AuditingHandler.java:193) ~[spring-data-commons-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at java.base/java.util.Optional.map(Optional.java:258) ~[na:na]
at org.springframework.data.auditing.AuditingHandler.touchAuditor(AuditingHandler.java:191) ~[spring-data-commons-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.data.auditing.AuditingHandler.lambda$touch$0(AuditingHandler.java:165) ~[spring-data-commons-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at java.base/java.util.Optional.map(Optional.java:258) ~[na:na]
【问题讨论】:
【参考方案1】:解决方案在 UserRepository 中
我添加了@Transactional
注解
问题在于寻找审计师。 这是交易的问题,所以制作一个新的解决了这个问题。
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Query("select u from User u where lower(u.login) = lower(?1) ")
User findAuditorByLogin(String login);
这是一个可行的解决方案,但也许还有更好的解决方案。
【讨论】:
以上是关于JpaRepository 连接问题,无法通过登录找到用户的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 JpaRepository 在 SpringMVC 中解析 ResponseEntity
无法在我的 JpaRepository 代码中使用 findOne() [重复]
Facebook 登录问题 - iOS 应用程序的新用户无法通过 FB 连接登录