Edu*_*ard 5 java stack-overflow spring spring-data-mongodb spring-boot
调用 MongoDB 存储库接口的方法时出现 StackOverflowError:
public interface TermRepository extends MongoRepository<Term, String>, QuerydslPredicateExecutor<Term> {
// [...]
@Query("{$or:[{'apis' : {$in : ?0 } }, {$text:{$search:?1}}]}")
Page<Term> globalSearch(List<DBRef> apis, String searchKeyword, Pageable pageable);
}
Run Code Online (Sandbox Code Playgroud)
apis
是一个只有一个 DBRef 的列表: new DBRef("api", new ObjectId("5e3ad9faaafa595898b6a682"))
searchKeyword
等于“账户”
pageable
是Page request [number: 0, size 37, sort: UNSORTED]
。如果size
是 36,它不会抛出 StackOverflowError!{
$or: [{
'apis': {
$in: [{
'$ref': 'api',
'$id': ObjectId('5e3ad9faaafa595898b6a682')
}]
}
}, {
$text: {
$search: 'account'
}
}]
}
Run Code Online (Sandbox Code Playgroud)
有谁知道发生了什么?
期限:
public class Term {
@Id
private String id;
@NotBlank
@TextIndexed
@JsonProperty
private String name;
@NotBlank
@TextIndexed
@JsonProperty
private String objectType;
@Transient
@JsonProperty
private String snakeCase;
@NotEmpty
@JsonProperty
private List<String> functionalCategories;
@NotNull
@JsonProperty
private TermTypeEnum termType;
@NotEmpty
@JsonProperty
private Map<String, String> description;
@Setter
@NotNull
@JsonProperty
private TermStateEnum state;
@NotBlank
@TextIndexed
@JsonProperty
private String example;
@DBRef
@NotNull
@Indexed
@JsonProperty
private List<Api> apis;
@DBRef
@NotNull
@JsonProperty
private User contributor;
@NotBlank
@TextIndexed
@JsonProperty
private String version;
@DBRef
@JsonProperty
private Map<String, Term> attributes;
}
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪:https : //pastebin.com/y0XYt7p6
虽然可能存在循环引用,因为 Term 有一个属性 Map<String, Term>,但我认为它不是产生堆栈溢出的循环引用。
做一些调试,我看到发生堆栈溢出是因为最后 spring-data 只持续实例化这个 Term (不是其他的,所以我没有看到循环引用):
Term(id=5e3ad9faaafa595898b6a7ea, name=debitCurrency, objectType=string, snakeCase=debit_currency, functionalCategories=null, termType=BODY, description={"english"=Debit Currency.}, state=PROPOSED, example=null, apis=[Api(id=5e3ad9faaafa595898b6a67f, name=Payments-1.0.1, description=null, responsible=null)], contributor=null, version=null, attributes=null)
Run Code Online (Sandbox Code Playgroud)
这反过来又不引用其他术语。
这是另一个术语,仅供比较:
Term(id=5e3ad9faaafa595898b6a6c8, name=displayCardNumber, objectType=string, snakeCase=display_card_number, functionalCategories=null, termType=BODY, description={"english"=Related card number to the account.}, state=PROPOSED, example=null, apis=[Api(id=5e3ad9faaafa595898b6a682, name=Accounts-1.0.2, description=null, responsible=null)], contributor=null, version=null, attributes=null)
Run Code Online (Sandbox Code Playgroud)
我看不出有什么区别,但debitCurrency
会产生堆栈溢出,displayCardNumber
但不会。
我确实有一个循环引用(更准确地说是自我引用)。
我提出了一种解决方法,其中包括创建@DBRef
属性 ,lazy=true
并实现该类的自定义序列化器Term
,该类具有自引用(或循环引用)。实现自定义序列化器的原因是,我可以保存一组已经序列化的对象,如果我必须再次序列化一个对象,我就不会这样做。
归档时间: |
|
查看次数: |
257 次 |
最近记录: |