Spring MVC: Vente :Entity,repository,ORM

Les étapes à suivre

  1. 1Créer l'entité Vente.java puis ajouter les annotations @Entity,@Id ,
    et le mapping objet relationnel de la relation Vente entre l'entité Utilisateur et l'entité Produit
    Un utilisateur peut acheter plusieurs produits :donc un utilisateur peut faire plusieurs Vente :Many Vente per One Utilisateur ; Donc @ManyToOne
    Un Produit peut etre vendu plusieur fois (en diminuant la quantité):donc un produit peut apparaitre dans plusieurs vente : ManyVente per One Produit Donc @ManyToOne
  2. 2Créer VenteRepository.java pour gérer la partie transaction dans la table vente

1.Créer l'entité Vente.java

package com.ecomerce.models;


import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

/*@Entity: La création de la table vente */
@Entity
public class Vente {
/*@Id int id :est primary key de la table vente */
@Id
/*la valeur de l'id est auto-increment*/
@GeneratedValue(strategy=GenerationType.IDENTITY)
//=>id int primary key auto_increment;
private int id;
private Date dateVente;
private float quantite;
/*Many Vente pour le meme produit
One :Produit peut etre vendu Many plusieurs fois(qte) */
@ManyToOne()
/* constraint foreign key(Produit_ID) references produits(id) */
@JoinColumn(name = "Produit_ID")
private Produit produit;
/*Many Vente peuvent etre faite par le meme ONE Client *
ONE Client peut faire Many plusieurs vente
*/
@ManyToOne()
/* constraint foreign key(USER_ID) references utilisateurs(id) */
@JoinColumn(name = "USER_ID")
private Utilisateur client;
public Vente() {}
public Vente(int id, Date dateVente, float quantite, Produit produit, Utilisateur client) {
super();
this.id = id;
this.dateVente = dateVente;
this.quantite = quantite;
this.produit = produit;
this.client = client;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getDateVente() {
return dateVente;
}
public void setDateVente(Date dateVente) {
this.dateVente = dateVente;
}
public float getQuantite() {
return quantite;
}
public void setQuantite(float quantite) {
this.quantite = quantite;
}
public Produit getProduit() {
return produit;
}
public void setProduit(Produit produit) {
this.produit = produit;
}
public Utilisateur getClient() {
return client;
}
public void setClient(Utilisateur client) {
this.client = client;
}
}

2.Créer VenteRepository.java

package com.ecomerce.DAO;
import org.springframework.data.repository.CrudRepository;
import com.ecomerce.models.Vente;
public interface VenteRepository extends CrudRepository<Vente,Integer> {
}









Cours et TPs