Solution to Infinite Recursion Problem of Interrelated Entities in Java
- 2021-11-29 07:18:22
- OfStack
Java Infinite Recursion of Interrelated Entities
Today, an bug appeared during the test, and an error was reported in the process of serializing the associated entity and returning it, prompting
Caused by: java.lang.StackOverflowError: null
This is a stack overflow error. Looking up the error thread, we finally find that Column and Table entities are related to each other, that is,
There are Table attribute in Column entity and Column attribute in Table entity, which leads to infinite loop, infinite recursion and stack overflow error in the serialization process.
The solution prior to Jackson 2.0 was
Add on the associated property
@JsonBackReference
Or
@JsonIgnore
One of the annotations will do. However, starting with Jackson 2.0 and later, the @ JsonIdentityInfo annotation is provided to solve this problem, with annotations before entity classes
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
A good understanding of recursion in Java
The idea of recursion
Transform large-scale problems into small-scale similar sub-problems to solve them. When the function is implemented, because the method to solve the big problem and the method to solve the small problem are often the same method, the function calls itself. In addition, this problem solving function must have obvious ending conditions, so that there will be no infinite recursion.
1 sentence summary: Recursion is to call yourself.
Conditional elements of recursion
1. There are two important conditions for recursion
You can reduce the size of the problem by recursive calls, and the new problem has the same form as the original problem. (Self-invocation) There is a simple situation, which can make recursion exit in a simple situation. (Recursive exit)2. 3 Elements of Recursion
Try to simplify a problem to a smaller scale Parent problem and child problem cannot overlap 1 There must be 1 situation in which you can exit the program
Recursive algorithm structure
The common algorithm pseudo-code of recursion is as follows:
func( mode){
if(endCondition){ // Recursive exit
end;
}else{
func(mode_small) // Call itself, recursively
}
}
Examples of recursive actual combat
Recursion is still a little abstract. Let's look at the code directly
1. The recursive realization of Fibonacci number
The recurrence formula of Fibonacci sequence is Fib (n) = Fib (n-1) + Fib (n-2), and the sequence (1, 1, 2, 3, 5, 8...) is generated.
public static int fib(int n) throws Exception {
if (n < 0){
throw new Exception(" Please enter the correct parameters ");
} else if (n == 0 || n == 1){
return n;
} else {
return fib(n - 1) + fib(n - 2); // Call yourself
}
}
Recursive Implementation of 2, 99 Multiplication Table
public static void mul(int n){
if(n==1){
System.out.println("1*1=1");
}else {
mul(n -1);
for(int i=1;i<=n;i++){
System.out.println(i + "*" + n + "=" + i*n + " ");
}
}
}
Summary 1, please
Recursive algorithm is an algorithm that calls itself directly or indirectly. If a problem can be solved, it can be decomposed into the solutions of several sub-problems; This problem is different from the sub-problem after decomposition, except for the different data scale, and the solution idea is completely the same; And there are obvious recursive termination conditions. Then recursion will be a good choice.