JpaRepository连接出现问题,无法通过登录找到用户



我制作了一个简单的crud程序
我使用了Spring审计
目前一切都很好:
当我发布一个新的"库存;没有问题,但当我尝试更新它时,我得到了这个错误
我很久没有看到StackOverFlowError
问题是,当我向数据库添加新对象时,它会通过登录存储库来找到用户,
使用类:

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.StackOverflowError: 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]

解决方案在UserRepository 中

我添加了一个@Transactional注释

问题出在找审计员上。这是一个交易问题,所以制作一个新的解决了这个问题。

@Transactional(propagation = Propagation.REQUIRES_NEW)
@Query("select u from User u where lower(u.login) = lower(?1) ")
User findAuditorByLogin(String login);

这是一个可行的解决方案,但也许还有更好的解决方案。

相关内容

  • 没有找到相关文章

最新更新