Search code examples
springspring-bootspring-data-jpamany-to-many

Spring Boot Jpa @ManyToOne : ForeignKey column in DB always populated to null


Employee.Java

`

import lombok.ToString;
    import javax.persistence.*;
    import java.util.ArrayList;
    import java.util.List;
    @ToString
    @Entity
    public class Employee {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private int empid;
        private String empname;
        private String empcontact;
        private String empemail;
        private String empphoto;
        @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL)
        private List<Skillset> skillset;

 public int getEmpid() {
        return empid;
    }

    public void setEmpid(int empid) {
        this.empid = empid;
    }

    public String getEmpname() {
        return empname;
    }

    public void setEmpname(String empname) {
        this.empname = empname;
    }

    public String getEmpcontact() {
        return empcontact;
    }

    public void setEmpcontact(String empcontact) {
        this.empcontact = empcontact;
    }

    public String getEmpemail() {
        return empemail;
    }

    public void setEmpemail(String empemail) {
        this.empemail = empemail;
    }

    public String getEmpphoto() {
        return empphoto;
    }

    public void setEmpphoto(String empphoto) {
        this.empphoto = empphoto;
    }

    public List<Skillset> getSkillset() {
        return skillset;
    }

    public void setSkillset(List<Skillset> skillset) {
        this.skillset = skillset;
    }`

SkillSet.Java

`

package aurozen.assign.aurozenassign.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.ToString;

import javax.persistence.*;

@Entity
public class Skillset {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int skillid;

    private String skillname;

    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "empId", nullable = false,updatable = false, insertable = true)
    private Employee employee;

    public int getSkillid() {
        return skillid;
    }

    public void setSkillid(int skillid) {
        this.skillid = skillid;
    }

    public String getSkillname() {
        return skillname;
    }

    public void setSkillname(String skillname) {
        this.skillname = skillname;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    @Override
    public String toString() {
        return "Skillset{" +
                "skillid='" + skillid + '\'' +
                ", skillname='" + skillname + '\'' +
                ", employee=" + employee +
                '}';
    }
}

`

EmployeeRepositry.java

package aurozen.assign.aurozenassign.repositry;


import aurozen.assign.aurozenassign.entity.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface EmployeeRepositry extends JpaRepository<Employee, Integer> {

    Optional<Employee> findByEmpcontact(String s);
}

SkillSetRepositry.java

package aurozen.assign.aurozenassign.repositry;


import aurozen.assign.aurozenassign.entity.Skillset;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SkillsetRepositry extends JpaRepository<Skillset, Integer> {
}

Controller

@PostMapping(value = "/signup",produces = {"application/json"})
    public Employee addEmployee(@RequestBody Employee employee) {
        empRepo.save(employee);
        return employee;
    }

Json Data

{
        "empname": "sandep",
        "empcontact": "9650114890",
        "empemail": "aidaih",
        "empphoto": "paidpaid",
        "skillset": [
            {
                "skillname": "jop"
            }
        ]
    }

I have attached Db Screenshot Database Screenshot with empid as foreign key in skillset table

foreign key(empid) in the skillset table always populating to null when ever I try to post the data through postman.other fields getting populating without any problem in both the table


Solution

  • The relationship between Skillset and Employee is owned by Skillset. This means JPA will persist the state the Skillset objects have.

    But via @RequestBody you are creating an Employee instance. While that references a Skillset instances, that instance does not reference the Employee. Therefor no relationship gets persisted.

    To fix this add code to setSkillset to set the employee property of it. Something like this should do:

    public void setSkillset(List<Skillset> skillset) {
        this.skillset = skillset;
        skillset.foreach(s -> s.setEmployee(this));
    }