<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[The way how to solve the issue related with Infinite Recursive fetching of data from relationships between Entity classes (Spring Boot JPA Hibernate)]]></title><description><![CDATA[<p dir="auto">This article is all about how to avoid recursive fetching of data from relationships designed in Database. I’ll explain everything with an example so that you will all understand clearly. Here I’m working with Intellij IDEA and MySQL databases.</p>
<p dir="auto"><strong>1.1 Brief about the tables and the relationship used</strong><br />
Here in my example, I have created two Entity classes and they are connected with a OnetoMany relationship.<br />
<img src="/assets/uploads/files/1566052238951-ba5448b6-b4a2-46cf-8311-07cf97a5ee95-image-resized.png" alt="0_1566052237730_ba5448b6-b4a2-46cf-8311-07cf97a5ee95-image.png" class=" img-responsive img-markdown" /></p>
<p dir="auto">One student can have more than one email and this is the scenario taken. StudentId in ContactModel refers to the id field in StudentModel and This is a OnetoMany relationship. StudentModel is the “One” side and ContactModel is the “Many” side.</p>
<p dir="auto"><strong>1.2 The classes before resolving the error</strong></p>
<p dir="auto">How the Entity classes are designed in Spring boot is given below in images.</p>
<p dir="auto">StudentModel.java<br />
<img src="/assets/uploads/files/1566052273764-bac17b36-a2d4-48eb-94b7-9fb6a0fd11fd-image-resized.png" alt="0_1566052271116_bac17b36-a2d4-48eb-94b7-9fb6a0fd11fd-image.png" class=" img-responsive img-markdown" /></p>
<p dir="auto">ContactModel.java<br />
<img src="/assets/uploads/files/1566052295123-c7597d82-ec12-43c0-a8b8-b59afb55ce64-image-resized.png" alt="0_1566052291862_c7597d82-ec12-43c0-a8b8-b59afb55ce64-image.png" class=" img-responsive img-markdown" /></p>
<p dir="auto"><strong>1.3 Issue looks like this</strong></p>
<p dir="auto">Some data is included in the database and I have run some queries to fetch the data from the contact table.This is how it appears after fetching data,<br />
<img src="/assets/uploads/files/1566052338842-d615763e-e3c2-4d03-8d66-3bf6d4f4ef0e-image-resized.png" alt="0_1566052336520_d615763e-e3c2-4d03-8d66-3bf6d4f4ef0e-image.png" class=" img-responsive img-markdown" /></p>
<p dir="auto">This is an infinite recursion. This is the issue after designing the entity classes as above.</p>
<p dir="auto"><strong>1.4 How to solve</strong></p>
<p dir="auto">There are many annotations such as @JsonManagedReference, @JsonBackReference, @JsonIgnore etc, to avoid this infinite recursion.But, when used them creates some errors in fetching and inserting data from and to the entities.</p>
<p dir="auto">After searching a lot in internet and trying a lot, I came across the best way to solve this. It’s very simple.I used only to one annotation in the “One” side of the relationship.<br />
<em><strong>@JsonIgnoreProperties</strong></em></p>
<p dir="auto">This annotation ignores the fields from Jsonification. You need to use this in class level as below,<br />
<img src="/assets/uploads/files/1566052426165-1d78cb76-5c35-453f-a7bf-65462ff24f19-image-resized.png" alt="0_1566052423734_1d78cb76-5c35-453f-a7bf-65462ff24f19-image.png" class=" img-responsive img-markdown" /></p>
<p dir="auto">Actually when fetching data from StudentModel, you don’t need to fetch data from the field “contact”. So, I have asked to ignore that field . This annotation take the ignoring fields in a String <a href="http://array.So" target="_blank" rel="noopener noreferrer nofollow ugc">array.So</a> you need to give the field-names in an String array as above.</p>
<p dir="auto">[ <strong>Note</strong>:  You need to ignore two more fields, <em><strong>hibernateLazyInitializer, handler</strong></em>, otherwise it gives an error message like this when fetching data</p>
<pre><code>  “No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]&gt;com.project.inventoryManagement.Models.ContactModel[\”student\”&gt;com.project.inventoryManagement.Models.StudentModel$HibernateProxy$bYhYXmwK[\”hi bernateLazyInitializer\”])" 
</code></pre>
<p dir="auto">]</p>
<p dir="auto">Happy Coding without bugs!<br />
Thank you.</p>
]]></description><link>https://lankadevelopers.lk/topic/348/the-way-how-to-solve-the-issue-related-with-infinite-recursive-fetching-of-data-from-relationships-between-entity-classes-spring-boot-jpa-hibernate</link><generator>RSS for Node</generator><lastBuildDate>Tue, 12 May 2026 19:22:13 GMT</lastBuildDate><atom:link href="https://lankadevelopers.lk/topic/348.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 17 Aug 2019 14:40:32 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to The way how to solve the issue related with Infinite Recursive fetching of data from relationships between Entity classes (Spring Boot JPA Hibernate) on Mon, 02 Sep 2019 05:48:56 GMT]]></title><description><![CDATA[<p dir="auto">thanks, good article</p>
]]></description><link>https://lankadevelopers.lk/post/2143</link><guid isPermaLink="true">https://lankadevelopers.lk/post/2143</guid><dc:creator><![CDATA[dev_lak]]></dc:creator><pubDate>Mon, 02 Sep 2019 05:48:56 GMT</pubDate></item><item><title><![CDATA[Reply to The way how to solve the issue related with Infinite Recursive fetching of data from relationships between Entity classes (Spring Boot JPA Hibernate) on Sun, 01 Sep 2019 18:51:54 GMT]]></title><description><![CDATA[<p dir="auto">Hi Udith..nice write up bro..</p>
]]></description><link>https://lankadevelopers.lk/post/2142</link><guid isPermaLink="true">https://lankadevelopers.lk/post/2142</guid><dc:creator><![CDATA[pavinduLakshan]]></dc:creator><pubDate>Sun, 01 Sep 2019 18:51:54 GMT</pubDate></item><item><title><![CDATA[Reply to The way how to solve the issue related with Infinite Recursive fetching of data from relationships between Entity classes (Spring Boot JPA Hibernate) on Mon, 19 Aug 2019 10:10:23 GMT]]></title><description><![CDATA[<p dir="auto">Nice artical Bro</p>
]]></description><link>https://lankadevelopers.lk/post/2089</link><guid isPermaLink="true">https://lankadevelopers.lk/post/2089</guid><dc:creator><![CDATA[b6]]></dc:creator><pubDate>Mon, 19 Aug 2019 10:10:23 GMT</pubDate></item><item><title><![CDATA[Reply to The way how to solve the issue related with Infinite Recursive fetching of data from relationships between Entity classes (Spring Boot JPA Hibernate) on Sun, 18 Aug 2019 04:10:48 GMT]]></title><description><![CDATA[<p dir="auto">Thanks good article , Happy coding</p>
]]></description><link>https://lankadevelopers.lk/post/2084</link><guid isPermaLink="true">https://lankadevelopers.lk/post/2084</guid><dc:creator><![CDATA[Nubelle]]></dc:creator><pubDate>Sun, 18 Aug 2019 04:10:48 GMT</pubDate></item><item><title><![CDATA[Reply to The way how to solve the issue related with Infinite Recursive fetching of data from relationships between Entity classes (Spring Boot JPA Hibernate) on Sat, 17 Aug 2019 18:59:10 GMT]]></title><description><![CDATA[<p dir="auto">Thanks bro, keep post these kind of articles.</p>
]]></description><link>https://lankadevelopers.lk/post/2081</link><guid isPermaLink="true">https://lankadevelopers.lk/post/2081</guid><dc:creator><![CDATA[root]]></dc:creator><pubDate>Sat, 17 Aug 2019 18:59:10 GMT</pubDate></item></channel></rss>