我的表product_commandes中的数据未插入

问题描述 投票:0回答:1

我的 PostgreSQL 数据库面临显示问题。 我是java初学者,所以我在这里寻求帮助,我希望得到一些解释来理解发生了什么。 为了解释这个问题,我有两个具有 @manytomany 关系的类(产品和订单)。

class Product (produit)
@ManyToMany(cascade = {CascadeType.ALL})
    private List<Commande> commande;

class Commande (literraly order)
@ManyToMany(mappedBy = "commande",targetEntity = Produit.class, cascade = { CascadeType.ALL },fetch=FetchType.EAGER)
    private List<Produit> listProduit;

在数据库中,我有一个中间表(product_order),它应该管理多对多关系。 当我插入新订单时,订单会添加到数据库的订单表中,但不会添加到 Product_order 表中,该表应包含订单的 ID 和相应的产品 ID。

我尝试更改注释关系,但得到了相同的结果

java postgresql hibernate object jpa
1个回答
0
投票
@Stultuske

This is the methode who add a order 

    public static void addCommande(Session session, Transaction transaction,Client client, List<Produit> listProduit){
        
        Commande newCommande = new Commande();
        
        
        newCommande.setClient(client);
        newCommande.setListProduit(listProduit);
        
        
        
        try {
            
            transaction = session.beginTransaction();
            session.save(newCommande);
            System.out.println("La commande a été ajouté avec succès");

        } catch (Exception e) {
            transaction.rollback();
           e.printStackTrace();
        }
        finally {
            session.close();
        }
        
    }

This is my main class 

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Session session = HibernateUtil.getOpenSession();
        Transaction transaction = null;
        
        
        Client client = ClientDAO.getSingleClientById(session,transaction,5l);
        
        Produit table = ProduitDAO.getSingleProduitById(session,transaction,2l);
        Produit chaise = ProduitDAO.getSingleProduitById(session,transaction,1l);
        
        List<Produit> nouvelleListProduit = new ArrayList<Produit>();
        

        nouvelleListProduit = CommandeDAO.addProduit(nouvelleListProduit, chaise);
        nouvelleListProduit = CommandeDAO.addProduit(nouvelleListProduit, table);
        
        CommandeDAO.addCommande(session,transaction,client, nouvelleListProduit);
        

    }

This is my class Order 

package Entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
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.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.SecondaryTable;
import javax.persistence.Table;

@Entity
@Table(name="commandes")
@SecondaryTable(name = "produit_commande")


public class Commande {
    
    @Id
    @Column(name="id_commande")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long idCommande;
    
    @ManyToOne
    @JoinColumn(name="id_client")
    private Client client;

    @ManyToMany(mappedBy = "commande",targetEntity = Produit.class, cascade = { CascadeType.ALL },fetch=FetchType.EAGER)
    private List<Produit> listProduit;

    public Long getIdCommande() {
        return idCommande;
    }

    public void setIdCommande(Long idCommande) {
        this.idCommande = idCommande;
    }

    public Client getClient() {
        return client;
    }

    public  void setClient(Client client) {
        this.client = client;
    }

    public List<Produit> getListProduit() {
        return listProduit;
    }

    public  void setListProduit(List<Produit> listProduit) {
        this.listProduit = listProduit;
    }


}

And this is my class Product

package Entity;

import java.time.LocalDate;
import java.util.ArrayList;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import javax.persistence.ManyToMany;

import javax.persistence.Table;


@Entity
@Table(name="produits")
public class Produit {
    
    @Id
    @Column(name="id_produit")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id_produit;
    

    @Column(name = "nom_produit")
    private String nom_produit;
    
    
    @Column(name = "is_dispo")
    private Boolean is_dispo;
    
    
    @Column(name="prix")
    private Double prix;
    
    
    @Column(name="created_at")
    private LocalDate created_at;
    
    
    @Column(name="quantite")
    private int quantite;
    

    @ManyToMany(cascade = {CascadeType.ALL})
    private List<Commande> commande;


    public Long getId_produit() {
        return id_produit;
    }


    public void setId_produit(Long id_produit) {
        this.id_produit = id_produit;
    }


    public String getNom_produit() {
        return nom_produit;
    }


    public void setNom_produit(String nom_produit) {
        this.nom_produit = nom_produit;
    }


    public Boolean getIs_dispo() {
        return is_dispo;
    }


    public void setIs_dispo(Boolean is_dispo) {
        this.is_dispo = is_dispo;
    }


    public Double getPrix() {
        return prix;
    }


    public void setPrix(Double prix) {
        this.prix = prix;
    }


    public LocalDate getCreated_at() {
        return created_at;
    }


    public void setCreated_at(LocalDate created_at) {
        this.created_at = created_at;
    }


    public int getQuantite() {
        return quantite;
    }


    public void setQuantite(int quantite) {
        this.quantite = quantite;
    }
    
    public List<Commande> getCommande() {
        return commande;
    }


    public void setCommande(List<Commande> commande) {
        this.commande = commande;
    }

}


 
© www.soinside.com 2019 - 2024. All rights reserved.