How to display nested data OneToMany?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
13 views
Is the Entity class:
@Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private String method; private String url; @OneToMany private List queryParams;

The problem is that on the page given Json data without QueryParams, such as it is, but emptyE4qouxv.png
Controller:
@GetMapping("/find/{name}") public List find(@PathVariable String name) { return testService.findByName(name); } @GetMapping("get/{id}") public Optional gets(@PathVariable("id") Long id) { return testService.findById(id); }


Thus, if you run in debug Evaluate Expression, you can see that the data arrives:5D44GVc.png
What did I miss ?
by | 13 views

1 Answer

0 like 0 dislike
Hello.
For a complete understanding is not enough code to your service class and dao class (if available). In my opinion the problem is in the strategy for loading related entities (@OneToMany).
In Hibernate/JPA there are two types of strategies load related objects at once (FetchType=EAGER) or to do so only when they are accessed (FetchType=LAZY). In fact, in the case where the subject area has no complex structure of relationships between objects, the choice is already made – download pabazi for one object, as it would be if FetchType=EAGER, to put it mildly, unwise. Therefore lazy initialization in the case of collections is the most preferred strategy of initialization of related objects [https://habr.com/post/111911/]. The default connection is used OneToMany FetchType=LAZY.
So the problem with loading related objects from the database.

The code in your model, you must add annualy selecting the type of strategy download:
... @OneToMany(fetch = FetchType.LAZY) private List queryParams; ...


And in the class of service or DAO class before
... return testObject; ...
to put the line
Hibernate.initialize(testObject.getQueryParams);
In this case, Hibernate will load all related objects.
If you have any questions, please contact, ready to answer.
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked Mar 26, 2019 by palebluedot
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
35,392 users