sam
sam

Reputation: 2004

Foreign key storing null value

I am using Spring boot, JPA to store data in DB for two relational table. But my foreign key is storing null value. Below is my code.

Controller:

@Controller
public class PersistController {

    @Autowired
    private T1Repo t1Repo;

    @GetMapping(value = "/p-save-data")
    @ResponseBody
    public void saveData() {

        T1Entity t1Entity = new T1Entity();
        List<T2Entity> t2Entities = new ArrayList<>();

        T2Entity one = new T2Entity();
        T2Entity two = new T2Entity();
        T2Entity three = new T2Entity();

        one.setT2Name("abc " );
        two.setT2Name("xyz ");
        three.setT2Name("def ");

        t2Entities.add(one);
        t2Entities.add(two);
        t2Entities.add(three);

        t1Entity.setT1Category("jata ");
        t1Entity.setT2Entities(t2Entities);


        t1Repo.save(t1Entity);
        System.out.println("t1 saved " + t1Entity.getT1Id());
    }
}

Model class T1Entity:

@Entity
@Table(name = "t1", schema = "jata")
public class T1Entity implements Serializable {

    private int t1Id;
    private String t1Category;
    private List<T2Entity> t2Entities;

    public T1Entity() {
    }

    @OneToMany(targetEntity = T2Entity.class, mappedBy = "t1Entity",
            fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    public List<T2Entity> getT2Entities() {
        return t2Entities;
    }

    public void setT2Entities(List<T2Entity> t2Entities) {
        this.t2Entities = t2Entities;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "t1_id")
    public int getT1Id() {
        return t1Id;
    }

    public void setT1Id(int t1Id) {
        this.t1Id = t1Id;
    }

    @Basic
    @Column(name = "t1_category")
    public String getT1Category() {
        return t1Category;
    }

    public void setT1Category(String t1Category) {
        this.t1Category = t1Category;
    }

}

Model class T2Entity:

@Entity
@Table(name = "t2", schema = "jata")
public class T2Entity implements Serializable {

    private int t2Id;
    private String t2Name;
    private T1Entity t1Entity;

    public T2Entity() {
    }

    @ManyToOne
    @JoinColumn(name = "t2_fk_t1_id", referencedColumnName = "t1_id")
    public T1Entity getT1Entity() {
        return t1Entity;
    }

    public void setT1Entity(T1Entity t1Entity) {
        this.t1Entity = t1Entity;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "t2_id")
    public int getT2Id() {
        return t2Id;
    }

    public void setT2Id(int t2Id) {
        this.t2Id = t2Id;
    }

    @Basic
    @Column(name = "t2_name", length = 50)
    public String getT2Name() {
        return t2Name;
    }

    public void setT2Name(String t2Name) {
        this.t2Name = t2Name;
    }


}

Repository for both Model class:

@Repository
public interface T1Repo extends JpaRepository<T1Entity, Integer> {
}

@Repository
public interface T2Repo extends JpaRepository<T2Entity,Integer> {
}

My data is getting stored in both the table t1 and t2, but the foreign key field is null. Please help me solving the problem.

Upvotes: 0

Views: 325

Answers (1)

user8826113
user8826113

Reputation: 129

You need to set T1Entity entity reference in T2Entity (child) entity. So below will be your code in controller -

@Autowired
private T1Repo t1Repo;

@GetMapping(value = "/p-save-data")
@ResponseBody
public void saveData() {

    T1Entity t1Entity = new T1Entity();
    List<T2Entity> t2Entities = new ArrayList<>();

    T2Entity one = new T2Entity();
    T2Entity two = new T2Entity();
    T2Entity three = new T2Entity();

    one.setT2Name("abc " );
    one.setT1Entity(t1Entity ); //newly added line
    two.setT2Name("xyz ");
    two.setT1Entity(t1Entity ); //newly added line
    three.setT2Name("def ");
    three.setT1Entity(t1Entity ); //newly added line

    t2Entities.add(one);
    t2Entities.add(two);
    t2Entities.add(three);

    t1Entity.setT1Category("jata ");
    t1Entity.setT2Entities(t2Entities);


    t1Repo.save(t1Entity);
    System.out.println("t1 saved " + t1Entity.getT1Id());
}

Upvotes: 1

Related Questions