gpt4 book ai didi

java - 获取 InvalidDataAccessApiUsageException : Path '%s' from root %s must not span a cyclic property reference when using ExampleMatcher

转载 作者:行者123 更新时间:2023-12-03 20:47:56 38 4
gpt4 key购买 nike

客户端型号:

@Entity
public class Client {
@Id
private Integer id;

@OneToOne(mappedBy = "client", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.REMOVE})
private ClientLegalTerm clientLegalTerm;

...//Getters and Setters
}
ClientLegalTerm 模型:
@Entity
public class ClientLegalTerm {
@Id
private Integer id;

@OneToOne(optional = false, fetch = FetchType.LAZY)
@JoinColumn(foreignKey = @ForeignKey(name = "FK_client_legal_term_client_id"))
private Client client;

...//Getters and Setters
}
MyNewEntity 模型:
@Entity
public class MyNewEntity {
@Id
private Integer id;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
private Client client;

...//Getters and Setters

我正在使用 JpaRepository管理 MyNewEntity我想使用 QueryByExample 从 repo 中获取一个实例。
    ExampleMatcher matcher = ExampleMatcher.matching()
.withMatcher("client.id", match -> match.exact());
Example<MyNewEntity> example = Example.of(new MyNewEntity(client), matcher);

Optional<MyNewEntity> optionalEntity = investorClosedEndTransactionRepository.findOne(example);
但是我在 findOne() 上遇到了异常如果填充了 ClientLegalTerm,则调用。 org.springframework.dao.InvalidDataAccessApiUsageException: Path 'client.legalTerm.client' from root MyNewEntity must not span a cyclic property reference!我知道这是因为从客户端内部有一个循环引用返回到同一个客户端,但是因为 ClientLegalTerm 是一个一对一的表,这就是我正在处理的模型。有没有办法绕过这个异常?
也许有一种不同的方法可以仅通过其 ID 来匹配客户端?
完整的堆栈跟踪:
2020-11-04 12:28:17,588 ERROR [com.krfs.web.handler.ControllerExceptionHandler handleUncaughtException] - 127.0.0.1 8de1b613-6e6d-4d00-82b3-773934d9f09d An unhandled exception has occurred
org.springframework.dao.InvalidDataAccessApiUsageException: Path 'client.legalTerm.client' from root InvestorClosedEndTransaction must not span a cyclic property reference!
[{ com.alpsinc.live.loader.InvestorClosedEndTransaction@c0c64c52 }] -client-> [{ com.krfs.model.Client@86d }] -legalTerm-> [{ com.krfs.model.ClientLegalTerm@86d }] -client-> [{ com.krfs.model.Client@86d }]
at org.springframework.data.jpa.convert.QueryByExamplePredicateBuilder.getPredicates(QueryByExamplePredicateBuilder.java:163)
at org.springframework.data.jpa.convert.QueryByExamplePredicateBuilder.getPredicates(QueryByExamplePredicateBuilder.java:167)
at org.springframework.data.jpa.convert.QueryByExamplePredicateBuilder.getPredicates(QueryByExamplePredicateBuilder.java:167)
at org.springframework.data.jpa.convert.QueryByExamplePredicateBuilder.getPredicate(QueryByExamplePredicateBuilder.java:102)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository$ExampleSpecification.toPredicate(SimpleJpaRepository.java:886)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.applySpecificationToCriteria(SimpleJpaRepository.java:762)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.getQuery(SimpleJpaRepository.java:693)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findOne(SimpleJpaRepository.java:466)
at sun.reflect.GeneratedMethodAccessor6222.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45005)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:371)
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:204)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:657)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:621)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:605)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:178)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy2110.findOne(Unknown Source)
at sun.reflect.GeneratedMethodAccessor6220.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45005)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.zeroturnaround.javarebel.integration.util.ReloadingProxyFactory$ReloadingMethodHandler.invoke(SourceFile:74)
at com.sun.proxy.$Proxy2110.findOne(Unknown Source)
at com.alpsinc.live.loader.InvestorClosedEndTransactionService.validateEntityStatus(InvestorClosedEndTransactionService.java:78)

最佳答案

我有同样的错误:

org.springframework.dao.InvalidDataAccessApiUsageException: Path 'company.taxData.company' from root User must not span a cyclic property reference!
就我而言,是因为我有这些模型:
@Entity
@Table(name = "users")
public class User extends Item
{
@NotBlank
@Email
private String email;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(nullable = false)
@NotNull
private Company company;
}
@Entity
public class Company extends Item
{
@OneToMany(
cascade = CascadeType.ALL,
fetch = FetchType.EAGER,
orphanRemoval = true)
private Set<User> users;

@OneToMany(mappedBy = "company",
cascade = CascadeType.ALL,
fetch = FetchType.EAGER,
orphanRemoval = true)
private Set<Address> addresses;
}
@Entity
public class Address extends Item
{
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(nullable = false)
@NotNull
private Company company;
}
我试图以这种方式获得用户:
public Optional<User> findOneByEmailAndCompany(final String email, final Company company)
{
final User user = new User();
user.setCompany(company);
user.setEmail(email);
return userRepository.findOne(Example.of(user));
}
QueryByExamplePredicateBuilder JPA 类尝试以反射方式解析属性和类,但在 spansCycle 中函数检查 identityHex实际 valueidentityHexparent是相同的,并抛出 InvalidDataAccessApiUsageException避免无限递归:
boolean spansCycle() {

if (value == null) {
return false;
}

String identityHex = ObjectUtils.getIdentityHexString(value);
PathNode current = parent;

while (current != null) {

if (current.value != null && ObjectUtils.getIdentityHexString(current.value).equals(identityHex)) {
return true;
}
current = current.parent;
}

return false;
}
我通过在 UserRepository 中创建一个自定义查询来解决这个问题,在 Company 和 User 之间进行内部连接:
@Repository
public interface UserRepository extends JpaRepository<User, Long>
{
@Query("SELECT u FROM User u INNER JOIN u.company uc WHERE uc.id = :companyId AND u.email = :userEmail")
public Optional<User> findByEmailAndCompanyId(String userEmail, Long companyId);
}

关于java - 获取 InvalidDataAccessApiUsageException : Path '%s' from root %s must not span a cyclic property reference when using ExampleMatcher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64688374/

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