<?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" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by beyondtheclouds on Medium]]></title>
        <description><![CDATA[Stories by beyondtheclouds on Medium]]></description>
        <link>https://medium.com/@skusman1526?source=rss-1ef8473d53ad------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*PevIdESI1B2i83ezoNht6Q.png</url>
            <title>Stories by beyondtheclouds on Medium</title>
            <link>https://medium.com/@skusman1526?source=rss-1ef8473d53ad------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 13:38:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@skusman1526/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[One to Many Mapping | Spring Data | Post Json Data| Spring Boot]]></title>
            <link>https://medium.com/@skusman1526/one-to-many-mapping-spring-data-post-json-data-spring-boot-fa2fd586aaea?source=rss-1ef8473d53ad------2</link>
            <guid isPermaLink="false">https://medium.com/p/fa2fd586aaea</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[one-to-many-relationship]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[spring-data-jpa]]></category>
            <dc:creator><![CDATA[beyondtheclouds]]></dc:creator>
            <pubDate>Fri, 16 Jun 2023 11:56:29 GMT</pubDate>
            <atom:updated>2023-06-16T12:03:50.692Z</atom:updated>
            <content:encoded><![CDATA[<p>I had been recently given a task by a Senior architect for a project requirement. I was asked to implement one to many mapping using spring data jpa.</p><p>I thought on paper it is easy but implementing also done. But Actual labyrinth started when I was giving data as Json format using <strong>POST method.</strong></p><p>Lets see the Scenario given , there is a Department and Employee Entities One department can have multiple employees.</p><p>this is the One-to-many relationship and implementation in spring boot using spring data jpa.</p><p><strong>Department.java</strong></p><pre>@Entity<br>@Setter<br>@Getter<br>@NoArgsConstructor<br>@AllArgsConstructor<br>@JsonIgnoreProperties({&quot;hibernateLazyInitializer&quot;,&quot;referenceList&quot;})<br>public class Department {<br>    @Id<br>    private int departmentId;<br>    private String departmentName;<br>    @OneToMany(mappedBy = &quot;department&quot;,cascade = CascadeType.ALL)<br>    public List&lt;Employee&gt; employeeList=new ArrayList&lt;&gt;();<br>}</pre><pre>@Entity<br>@Setter<br>@Getter<br>@NoArgsConstructor<br>@AllArgsConstructor<br>public class Employee {<br>    @Id<br>    private int employeeId;<br>    private String employeeName;<br>    @ManyToOne(fetch = FetchType.LAZY)<br>    @JoinColumn(name = &quot;department_id&quot;)<br>   @JsonIgnoreProperties(&quot;employeeList&quot;)// It prevent from looping.in department.<br>    private Department department;<br><br>}</pre><p>Employee.java</p><p>Now We need to give data to the Department table. In department we have mapped employee table. By giving <strong>department_id as a foreign key</strong></p><p>this is tables ERD</p><figure><img alt="Table ERD" src="https://cdn-images-1.medium.com/max/680/1*TJYHJYAWIkzG3EPpTYKdMg.png" /></figure><p>DepartmentController.java</p><pre>@RestController<br>@RequestMapping(&quot;/department&quot;)<br>public class DepartmentController {<br>    @Autowired<br>    DepartmentImplementation departmentImplementation;<br>    @PostMapping(&quot;/save&quot;)<br>    public Department saveDepartment(@RequestBody Department department){<br>      return  departmentImplementation.addDepartment(department);<br>    }<br>    @GetMapping(&quot;/getAllDepartments&quot;)<br>    public List&lt;Department&gt; getAllDepartment(){<br>        return departmentImplementation.getAllDepartments();}<br>}</pre><p>DepartmentService.java</p><pre>public interface DepartmentService {<br>    public Department addDepartment(Department department);<br>    public List&lt;Department&gt;getDepartments)();<br>}<br></pre><p>DepartmentServiceImplementation.java</p><pre>@Service<br>public class DepartmentImplementation implements  DepartmentService {<br>    @Autowired<br>    DepartmentRepository departmentRepository;<br><br>    @Override<br>    public Department addDepartment(Department department) {<br>        return departmentRepository.save(department);<br>    }<br>    @Override<br>   public List&lt;Department&gt;getDepartments)(){<br>           return departmentRepository.findAll();<br>   }<br>}</pre><p>DepartmentRepository.java</p><pre>@Repository<br>public interface DepartmentRepository extends JpaRepository&lt;Department,Integer&gt; {<br>}</pre><p><strong>Giving Data through Postman using REST API</strong></p><p><strong>POST:</strong>localhost:9091/department/save</p><pre>{<br>    &quot;departmentId&quot;:8081,<br>    &quot;departmentName&quot;: &quot;Telugu&quot;,<br>    &quot;employeeList&quot;: [<br>        {<br>            &quot;employeeId&quot;: 1000,<br>            &quot;employeeName&quot;: &quot;Usman&quot;,<br>            &quot;department&quot;:{<br>                &quot;departmentId&quot;:10<br>            }<br>        },<br>         {<br>            &quot;employeeId&quot;: 1001,<br>            &quot;employeeName&quot;: &quot;Vandhana&quot;,<br>            &quot;department&quot;:{<br>                &quot;departmentId&quot;:8081<br>            }<br>        }<br>    ]<br>}</pre><p>giving departmentId, departName and employeeList.</p><p>Retrieving through <strong>GET</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/1*CVnEv9ymoqMkVb9IGPluCA.png" /></figure><pre><br>spring.datasource.url=jdbc:postgresql://localhost:5432/yout_database_name<br>spring.datasource.username=postgres<br>spring.datasource.password=password<br>spring.database.driverClassName=org.postgresql.Driver<br>spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true<br>spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect<br>spring.jpa.hibernate.ddl-auto=update<br>server.port=9091</pre><p>Hope you like this. Keep Learning.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fa2fd586aaea" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>