JPA多对多关系:无法插入生成的表
Posted
技术标签:
【中文标题】JPA多对多关系:无法插入生成的表【英文标题】:JPA many to many relation: unable to insert into generated table 【发布时间】:2020-11-17 16:07:38 【问题描述】:我有 2 个实体“Entre”和“Emplacement”:
@Entity
@Table(name = "ENTREE")
public class Entree
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID_ENTREE", updatable = false, nullable = false)
private long idEntree;
@Column(name = "NUM_DECLARATION", insertable=true, updatable=true, nullable=true)
private String numDeclaration;
@Column(name = "DATE_ENTREE", insertable=true, updatable=true, nullable=true)
private String dateEntree;
@Column(name = "TYPE_ENTREE", insertable=true, updatable=true, nullable=true)
private String typeEntree;
@Column(name = "NOM_ARTICLE", insertable=true, updatable=true, nullable=true)
private String nomArticle;
@Column(name = "TYPE_ARTICLE", insertable=true, updatable=true, nullable=true)
private String typeArticle;
@Column(name = "QUANTITE_ENTREE", insertable=true, updatable=true, nullable=true)
private int quantiteEntree;
@ManyToOne
@JoinColumn(name="idDossier", nullable=false)
private Dossier dossier;
@ManyToMany( fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinTable(name = "entree_emplacement",
joinColumns =
@JoinColumn(name = "id_entree", referencedColumnName = "id_entree",
nullable = false, updatable = false),
inverseJoinColumns =
@JoinColumn(name = "id_emplacement", referencedColumnName = "id_emplacement",
nullable = false, updatable = false))
private Set<Emplacement> emplacement = new HashSet<>();
public Entree()
super();
public Entree( String numDeclaration, String dateEntree, String typeEntree, String nomArticle, String typeArticle, int quantiteEntree, boolean isDone)
super();
this.numDeclaration = numDeclaration;
this.dateEntree = dateEntree;
this.typeEntree = typeEntree;
this.nomArticle = nomArticle;
this.typeArticle = typeArticle;
this.quantiteEntree = quantiteEntree;
public long getIdEntree()
return idEntree;
public void setIdEntree(long idEntree)
this.idEntree = idEntree;
public String getNumDeclaration()
return numDeclaration;
public void setNumDeclaration(String numDeclaration)
this.numDeclaration = numDeclaration;
public String getDateEntree()
return dateEntree;
public void setDateEntree(String dateEntree)
this.dateEntree = dateEntree;
public String getTypeEntree()
return typeEntree;
public void setTypeEntree(String typeEntree)
this.typeEntree = typeEntree;
public String getNomArticle()
return nomArticle;
public void setNomArticle(String nomArticle)
this.nomArticle = nomArticle;
public String getTypeArticle()
return typeArticle;
public void setTypeArticle(String typeArticle)
this.typeArticle = typeArticle;
public int getQuantiteEntree()
return quantiteEntree;
public void setQuantiteEntree(int quantiteEntree)
this.quantiteEntree = quantiteEntree;
public Dossier getDossier()
return dossier;
public void setDossier(Dossier dossier)
this.dossier = dossier;
public Set<Emplacement> getEmplacements()
return emplacement;
public void addEmplacement(Emplacement emplacement)
this.emplacement.add(emplacement);
emplacement.getEntrees().add(this);
public void removeEmplacement(Emplacement emplacement)
this.emplacement.remove(emplacement);
emplacement.getEntrees().remove(this);
这里是第二个实体:
@Entity
@Table(name = "EMPLACEMENT")
public class Emplacement
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID_EMPLACEMENT", updatable = false, nullable = false)
private long idEmplacement;
@Column(name = "NUM_EMPLACEMENT", insertable=true, updatable=true, nullable=false)
private String numEmplacement;
@ManyToMany(mappedBy = "emplacement", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<Entree> entree = new HashSet<>();
public Emplacement()
public Emplacement( String numEmplacement)
this.numEmplacement = numEmplacement;
public long getIdEmplacement()
return idEmplacement;
public void setIdEmplacement(long idEmplacement)
this.idEmplacement = idEmplacement;
public String getNumEmplacement()
return numEmplacement;
public void setNumEmplacement(String numEmplacement)
this.numEmplacement = numEmplacement;
public Set<Entree> getEntrees()
return entree;
这是我的插入代码:
@PostMapping("/ajouterEntree")
public ResponseEntity<String> addEntree(@Valid Entree entree, BindingResult result,ModelMap modelMap, @RequestParam(name = "numDossier") String numDossier, @RequestParam(name = "emplacement") String liste_emplacements)
Emplacement e = new Emplacement(liste_emplacements);
entree.getEmplacements().add(e);
entreeService.saveEntree(entree);
return new ResponseEntity<String>("ok" + result, HttpStatus.OK);
我可以将数据插入到 Entree 和 Emplacement 表中,但是生成的第三个名为 entree-emplacement 的表是空的。
那么如何在@ManyToMany 关系中将数据插入到生成的表中? 谢谢
【问题讨论】:
【参考方案1】:好的,解决了。这是我的代码:
if(!liste_emplacements.equals(""))
List<String> list = new ArrayList<String>(Arrays.asList(liste_emplacements.split(",")));
Emplacement[] emp = new Emplacement[list.size()];
for (int i=0; i<list.size() ;i++)
emp[i] = new Emplacement(Long.parseLong(list.get(i)));
entree.getEmplacements().add(emp[i]);
emp[i].getEntrees().add(entree);
entreeService.saveEntree(entree);
return new ResponseEntity<String>("ok" + result, HttpStatus.OK);
【讨论】:
以上是关于JPA多对多关系:无法插入生成的表的主要内容,如果未能解决你的问题,请参考以下文章
Spring数据JPA-休眠多对多关系在链接实体表中插入null
孩子不会更新 Xamarin SQLite 扩展 - 多对多关系