I wanna make a primary key from 2 foreign keys using Hibernate entity annotation : **
I tried this code but it didnt work :
Class "Produit" :
public class Produit implements Serializable{
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "LigneCommande", catalog = "mkyongdb", joinColumns = {
@JoinColumn(name = "reference", nullable = false, updatable = false) },
inverseJoinColumns = { @JoinColumn(name = "commande_id",
nullable = false, updatable = false) })
private List<Commande> commande;
public List<Commande> getCommande() {
return commande;
}
public void setCommande(List<Commande> commande) {
this.commande = commande;
}
}
Class "Commande" :
@Entity
public class Commande implements Serializable{
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "commande")
private List<Produit> produit;
public List<Produit> getProduit() {
return produit;
}
public void setProduit(List<Produit> produit) {
this.produit = produit;
}
}
On top of that I have not any exception or error !!
This is the solution :
public class LigneCommande implements Serializable {
@EmbeddedId
protected LigneCommandePK ligneCommandePK;
@Column(name = "quantite")
private int quantite;
@Column(name = "status")
private String status;
@JoinColumn(name = "produit_id", referencedColumnName = "id", insertable = false, updatable = false)
@ManyToOne(optional = false)
private Produit produit;
@JoinColumn(name = "commande_id", referencedColumnName = "id", insertable = false, updatable = false)
@ManyToOne(optional = false)
private Commande commande;
}
This the Class "Produit" :
@Entity
@Table(name = "produit")
public class Produit implements Serializable {@OneToMany(cascade = CascadeType.ALL, mappedBy = "produit")
private Collection<LigneCommande> ligneCommandeCollection;
}
And this is the association class :
@Embeddable
public class LigneCommandePK implements Serializable {
@Basic(optional = false)
@Column(name = "commande_id")
private int commandeId;
@Basic(optional = false)
@Column(name = "produit_id")
private int produitId;
}
And it works, look at the picture :
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments