springspring-bootentityone-to-manymany-to-one

Id Commande not populated on table Lignes Comandes


I tried to implement a method to add new commande so i create i entity Commande and Ligne Commande as below :

Commande Entity:

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 Entity :

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

The controller Method:

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


    }

The service Impl

@Override
    public Commande saveCommande(Commande p) {
        return commandeRepository.save(p);
    }

The service Impl

@Override public Commande saveCommande(Commande p) { return commandeRepository.save(p); }

the commande_id on the table LigneCOmande is null

How to fix the mapping commande_id?

the commande_id on the table LigneCOmande shouldn t be null THE RESULT : a new row inserted on table Commande and 2 rows inserted on LignesCommande with all fields populated corectly except commande_id is null


Solution

  • I fix it by adding lignecomman.setCommande(comande);