spring 未在表Lignes Comandes中填充Id命令

dkqlctbz  于 2023-03-16  发布在  Spring
关注(0)|答案(1)|浏览(94)

我尝试实现一个方法来添加新的命令,所以我创建i实体命令和Ligne命令如下:
命令实体:

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;

命令实体名称:

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;
                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上的命令ID为空
如何修复Mapcommande_id?
表LigneCOmande上的commande_id不应为空结果:在表Commande中插入一个新行,在LignesCommande中插入2行,除commande_id为空外,所有字段均正确填充

6ju8rftf

6ju8rftf1#

我通过添加lignecomman.setCommande(comande)来修复它;

相关问题