In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to solve the Java interrelated entity infinite recursion problem, the article introduces in great detail, has a certain reference value, interested friends must read it!
Java interrelated entities infinitely recursive
Today, a bug appeared during the test, and an error was reported in the process of serializing the associated entity.
Caused by: java.lang.StackOverflowError: null
This is a stack overflow error. According to the error clues, it is found that Column and Table entities are related to each other, that is to say,
There is a Table attribute in the Column entity and a Column attribute in the Table entity, which leads to a dead loop, infinite recursion, and even stack overflow errors during serialization.
Before Jackson2.0, the solution was
Add on the associated attribute
@ JsonBackReference
Or
@ JsonIgnore
Just one of the comments. However, starting from later versions of Jackson2.0, @ JsonIdentityInfo annotations are provided to solve this problem, adding annotations before entity classes.
@ JsonIdentityInfo (generator=ObjectIdGenerators.IntSequenceGenerator.class, property= "@ id") understands the idea of recursion in Java.
Turn large-scale problems into small-scale similar sub-problems to solve. In the implementation of the function, because the method to solve the big problem and the method to solve the small problem are often the same method, so the function calls itself. In addition, the problem-solving function must have an obvious end condition, so that there is no infinite recursion.
To sum up: recursion is to call yourself.
Conditional elements of recursion
1. There are two important conditions for recursion
The scale of the problem can be reduced by recursive call, and the new problem has the same form as the original problem. (called by itself)
There is a simple situation in which recursion can exit in a simple situation. (recursive exit)
2. Three elements of recursion
Try to simplify a problem to a smaller scale
There can be no overlap between the parent problem and the child problem
There must be a situation where you can exit the program.
Recursive algorithm structure
The pseudocode of the common algorithm of recursion is as follows:
Func (mode) {if (endCondition) {/ / Recursive exit end;} else {func (mode_small) / / call itself, Recursive}} Recursive practical example
Recursion is still a little abstract, let's look at the code directly.
1. Recursive realization of Fibonacci number
The recurrence formula of Fibonacci sequence is: Fib (n) = Fib (n Mel 1) + Fib (N Mel 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}}
2. Recursive implementation of 99 multiplication table
Public static void mul (int n) {if (naphtha 1) {System.out.println ("1x 1x 1");} else {mul (n-1); for (int ionometeri)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.