Santo
Santo

Reputation: 182

Spring JPA Hibernate ManyToMany

i'm using SpringBoot, with Spring JPA Hibernate, i'm trying to use the ManyToMany Annotation but it isn't working.

The join table is created, but never populated.

@Entity
@Table(name = "commande")
public class Commande {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer commandeID;
private String etat;
private String date_de_validation;
@ManyToOne(cascade = CascadeType.ALL)
private Client clientID;
@ManyToOne(cascade = CascadeType.ALL)
private Livreur livreurID;
@ManyToOne(cascade = CascadeType.ALL)
private Livraison livraisonID;
@ManyToMany
@JoinTable(name = "Join_Produit_To_Commande", joinColumns = { @JoinColumn(name = "commandeID") }, inverseJoinColumns = { @JoinColumn(name = "produitID") })
private Set<Produit> produits = new HashSet<Produit>();;

public Commande() {
}

public Commande(String etat, String date_de_validation, Client clientID,
        Livreur livreurID, Livraison livraisonID) {
    setEtat(etat);
    setDate_de_validation(date_de_validation);
    setClient(clientID);
    setLivreur(livreurID);
    setLivraison(livraisonID);
}

public Integer getCommandeID() {
    return this.commandeID;
}

public void setCommandeID(Integer commandeID) {
    this.commandeID = commandeID;
}

public String getEtat() {
    return this.etat;
}

public void setEtat(String etat) {
    this.etat = etat;
}

public String getDate_de_validation() {
    return this.date_de_validation;
}

public void setDate_de_validation(String date_de_validation) {
    this.date_de_validation = date_de_validation;
}

public Client getClient() {
    return this.clientID;
}

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

public Livreur getLivreur() {
    return this.livreurID;
}

public void setLivreur(Livreur livreurID) {
    this.livreurID = livreurID;
}

public Livraison getLivraison() {
    return this.livraisonID;
}

public void setLivraison(Livraison livraisonID) {
    this.livraisonID = livraisonID;
}

public Client getClientID() {
    return clientID;
}

public void setClientID(Client clientID) {
    this.clientID = clientID;
}

public Livreur getLivreurID() {
    return livreurID;
}

public void setLivreurID(Livreur livreurID) {
    this.livreurID = livreurID;
}

public Livraison getLivraisonID() {
    return livraisonID;
}

public void setLivraisonID(Livraison livraisonID) {
    this.livraisonID = livraisonID;
}

public Collection<Produit> getProduits() {
    return produits;
}

public void setProduits(Set<Produit> produits) {
    this.produits = produits;
}

public String toString() {
    return "commandeID=" + commandeID + " etat=" + etat
            + " date_de_validation=" + date_de_validation + " clientID="
            + clientID + " livreurID=" + livreurID + " livraisonID="
            + livraisonID;
}

}

This is my second class

@Entity
@Table(name = "produit")
public class Produit {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer produitID;
private String libelle;
private double poid;
private String type;
@ManyToMany(mappedBy = "produits")
private Set<Commande> commandes = new HashSet<Commande>();;

public Produit() {
}

public Produit(String libelle, double poid, String type) {
    setLibelle(libelle);
    setPoid(poid);
    setType(type);
}

public String getLibelle() {
    return this.libelle;
}

public void setLibelle(String Libelle) {
    this.libelle = Libelle;
}

public double getPoid() {
    return this.poid;
}

public void setPoid(double poid) {
    this.poid = poid;
}

public String getType() {
    return this.type;
}

public void setType(String type) {
    this.type = type;
}

public Set<Commande> getCommandes() {
    return commandes;
}

public void setCommandes(Set<Commande> commandes) {
    this.commandes = commandes;
}

public String toString() {
    return "Libelle=" + libelle + " poid=" + poid + " type=" + type
            + " produitID=" + produitID + " Commandes : " + commandes;
}

}

And here my Unit testing

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = PfeApplication.class)
@WebAppConfiguration
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class PfeApplicationTests {

@Autowired
private EntrepriseService service;
@Autowired
private PhoneService pService;
@Autowired
private LivreurService lService;
@Autowired
private CarService cService;
@Autowired
private ProduitService prService;
@Autowired
private ClientService clService;
@Autowired
private CommandeService cmdService;
@Autowired
private PositionLivreurService posService;
@Autowired
private LivraisonService livService;

@Test
public void contextLoads() {
}

@Test
@Transactional
public void manyToManyTest(){
    Produit produit1 = new Produit("Lavabo", 50, "Cassable");
    Livreur livreur1 = new Livreur("Adadi", "Soufiane", "03/03/1992", "0876543456");
    Client client1 = new Client("Ouali", "[email protected]", "0743453462", "Fes", "NoLogo");
    Livraison livraison1 = new Livraison("24/04/2015", "25/04/2015", false, false, livreur1);
    Commande commande1 = new Commande("Validé", "25/04/2015", client1, livreur1, livraison1);
    produit1.getCommandes().add(commande1);
    commande1.getProduits().add(produit1);
    cmdService.addCommande(commande1);
    prService.addProduit(produit1);
}
}

Upvotes: 1

Views: 541

Answers (2)

Santo
Santo

Reputation: 182

Resolved !

I had to add them both on the same Session on a transaction and then commit.

Upvotes: 1

YoYo
YoYo

Reputation: 9415

This problem similar to an earlier posting of mine. See My original posting. I solved it myself - it is basically a bug in the implementation, but if you just change Set<> into List<>, it could maybe work. Your case is different, you are using a spring implementation, but who knows ...

Upvotes: 1

Related Questions