我使用Spring启动,JPA将数据存储在DB中,用于两个关系表。但我的外键是存储空值。以下是我的代码。
控制器:
@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;
}
}
两个Model类的存储库:
@Repository
public interface T1Repo extends JpaRepository<T1Entity, Integer> {
}
@Repository
public interface T2Repo extends JpaRepository<T2Entity,Integer> {
}
我的数据存储在表t1和t2中,但外键字段为空。请帮我解决问题。
答案 0 :(得分:1)
您需要在T1Entity
(子)实体中设置T2Entity
实体引用。所以下面将是您在控制器中的代码 -
@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());
}