我尝试实现一种添加新命令的方法,因此我创建了实体 Commande 和 Ligne Commande,如下所示:
指挥实体:
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
@Entity
public class Commande {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idCommande;
private Date dateCreation;
private Double prixTotal;
@ManyToOne
private CLient client;
@OneToMany(mappedBy = "commande", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference
@JsonIgnore
List<LigneComande> ligneComande;
Lignes commande 实体:
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import com.fasterxml.jackson.annotation.JsonBackReference;
@Entity
public class LigneComande {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idLigne;
private Double prixProduit;
private Double quantite;
private Date dateCreation;
@ManyToOne (fetch = FetchType.LAZY, cascade = javax.persistence.CascadeType.ALL)
@JoinColumn(name = "commande_id")
@JsonBackReference
private Commande commande;
@ManyToOne
private Produit produit;
//GETTERS and setters and COnstructors
控制器方法:
@PostConstruct
public void initRoleAndUser() {
java.util.Date datecreationuser= new java.util.Date();
long id =2L;
System.out.println("etape111111111111111111");
Produit prod = produitservice.getProduit(id);
CLient cleint = cLientservice.getCLient(id);
Commande comande = new Commande();
LigneComande lignecomman = new LigneComande();
List<LigneComande> lignecomman1 = new ArrayList<LigneComande>();
lignecomman.setDateCreation(new java.sql.Timestamp(datecreationuser.getTime()));
lignecomman.setPrixProduit((double) 22);
lignecomman.setQuantite((double) 22);
lignecomman.setProduit(prod);
lignecomman1.add(lignecomman);
lignecomman = new LigneComande();
lignecomman.setDateCreation(new java.sql.Timestamp(datecreationuser.getTime()));
lignecomman.setPrixProduit((double) 584);
lignecomman.setQuantite((double) 987);
lignecomman.setProduit(prod);
lignecomman1.add(lignecomman);
comande.setClient(cleint);
comande.setPrixTotal((double) 251);
comande.setLigneComande(lignecomman1);
comande.setDateCreation(new java.sql.Timestamp(datecreationuser.getTime()));
commandeService.saveCommande(comande);
}
服务实施
@Override
public Commande saveCommande(Commande p) {
return commandeRepository.save(p);
}
服务实施
@Override public Commande saveCommande(Commande p) { return commandeRepository.save(p); }
LigneCOmande 表上的 commande_id 为空
如何修复映射commande_id?
LigneCOmande 表上的 commande_id 不应该为空