【问题标题】:Foreign key storing null value存储空值的外键
【发布时间】:2018-05-04 11:10:36
【问题描述】:

我正在使用 Spring Boot,JPA 将数据存储在两个关系表的数据库中。但是我的外键正在存储空值。下面是我的代码。

控制器:

@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());
    }
}

模型类 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;
    }

}

模型类 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
public interface T1Repo extends JpaRepository<T1Entity, Integer> {
}

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

我的数据同时存储在表 t1 和 t2 中,但外键字段为空。请帮我解决问题。

【问题讨论】:

    标签: hibernate spring-boot jpa spring-data-jpa


    【解决方案1】:

    您需要在T2Entity(子)实体中设置T1Entity 实体引用。所以下面将是你在控制器中的代码 -

    @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());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-26
      • 2016-08-20
      • 2019-11-17
      • 2019-11-13
      相关资源
      最近更新 更多