Java: How to handle Json?

Good day!
The question is:
There
//Main class
 class Client {
 private String login;
 private Set<pet> pets;
}

//Processes the request and returns a JSON object 
@RequestMapping(value = "/ajax", method = RequestMethod.GET)
 public @ResponseBody String ajax() throws IOException {
 ObjectMapper mapper = new ObjectMapper();
 return mapper.writeValueAsString(clientDAO.getAll()); // then List<> (list all clients and each client Set animal)
}
//In the jsp describe the processing of data using ajax

$.ajax({
 url: '/ajax',
 type: 'GET',
 dataType: "json",
 contentType: "application/json ",
 success: function (data) {
 alert("Data is" + data)
},
 error: function () {
 alert("Error retrieving data!")
}
})</pet>


If you run the code I get the exception and refers to the Set pets, will prompt how correctly to implement an ajax request or I do not correctly send the data? If we substitute an arbitrary client object without the Set<> then everything works!
July 9th 19 at 13:46
3 answers
July 9th 19 at 13:48
Solution
As I wrote in comments, you have a circular dependency between Pet and Client. The output of two or remove a dependent or to determine the owner to Hibernate can correctly identify the rule deserialization. This can be done either in xml describing the rule of mapping parameter and define the inverse:

<hibernate-mapping>
 <class name="ru.misha.model.Client" table="clients" ...>
...
 <set name="pets" table="clients_pets" fetch="select" inverse="true">
<key>
 <column name="client_id" not-null="true">
</column></key>
 <one-to-many class="ru.misha.model.Pet">
</one-to-many></set>
...
</class>
...
</hibernate-mapping>


either define the direction of the relationship with antelami

@Entity
@Table(name = "clients")
public class Client extends Base {
@OneToMany(mappedBy="client")
@Column(name="clientId")
 private Set<pet> pets;
}</pet>


Something like that. Do not copy blindly, since I am writing from memory and could be wrong in the details. Read the documentation for Hibernate in a specified direction.
Thank you understand in which direction to look! - Sherman87 commented on July 9th 19 at 13:51
Found the solution:
Since Jackson 1.6 you can use two annotations to solve the infinite recursion problem without ignoring the getters/setters during serialization: @JsonManagedReference and @JsonBackReference

And added fetch="select" as you recommended, and that

still recommended to use a DTO, but with this we need to understand and do not really want to be honest!
Thank you! - Sherman87 commented on July 9th 19 at 13:54
July 9th 19 at 13:50
> Infinite recursion (StackOverflowError) (through reference chain: EN.misha.model.Pet["client"]->ru.misha.model.Client["pets"]
As I understand it, the program pulls the client in the client list of animals. The animal in the sky, the client, and follow the recursion again. Memory overflows and the app crashes. Try either to store only customer id in the animal, or add a OneToMany to list animals and in the animal remove the reference to customer
It is clear why this happens, but the solution you propose will not work. I have a OneToMany and ManyToOne, and if I removed the reference to the client connection via hibernate disturbed.
Perhaps there is some other solution? - Sherman87 commented on July 9th 19 at 13:53
So far I see 2 solutions: 1) as I wrote above, to pull only the id. Then the desired Client from the Dao shipping already id. 2) extrude the desired Client using the Dao, but passing petId. Both will have to get rid of cross-dependencies. - Sherman87 commented on July 9th 19 at 13:56
Here everything works fine without using ajax, and there is direct an ambush - Sherman87 commented on July 9th 19 at 13:59
Found the solution:
Since Jackson 1.6 you can use two annotations to solve the infinite recursion problem without ignoring the getters/setters during serialization: @JsonManagedReference and @JsonBackReference
In xml I added fetch="select", it worked.

Still recommended to use a DTO, but with this we need to understand and do not really want to be honest!
Thank you! - Luisa commented on July 9th 19 at 14:02
July 9th 19 at 13:52
You can look here , here, and on my answer on a similar question here here
Thank you, your answer to the toaster seen! - Sherman87 commented on July 9th 19 at 13:55

Find more questions by tags JavaHibernateSpring