IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Hibernate Java Discussion :

Relation many to many avec attribut


Sujet :

Hibernate Java

  1. #1
    Membre confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2013
    Messages
    81
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loiret (Centre)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2013
    Messages : 81
    Par défaut Relation many to many avec attribut
    Bonjour j'ai un petit soucis étant débutant en hibernate je me demande comment mapper mes objets java : je m'explique :

    -J'ai une classe Carrelage qui référence différent type de carrelages identifié par un codeArticle unique

    -J'ai une classe Commande qui associe à un numéro de Commande différents codeArticle qui sont commandés

    Jusque ici je m'en sors seulement j'aimerais que dans cette association many to many entre Carrelage et Commande je puisse avoir la quantité de carrelages demandé

    Des idées ?

    Merci de votre aide
    Images attachées Images attachées  

  2. #2
    Membre expérimenté Avatar de hbennou
    Homme Profil pro
    Développeur Java
    Inscrit en
    Mai 2008
    Messages
    162
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2008
    Messages : 162
    Par défaut
    Bonjour,

    tu peux mettre un attribut Qte dans ta classe commande

  3. #3
    Membre confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2013
    Messages
    81
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loiret (Centre)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2013
    Messages : 81
    Par défaut merci
    Citation Envoyé par hbennou Voir le message
    Bonjour,

    tu peux mettre un attribut Qte dans ta classe commande
    Non parce qu'une commande peut concerner plusieurs articles en quantité différente

  4. #4
    Membre Expert
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 277
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 277
    Par défaut
    Tu peux découper ta relation n-n en deux relations 1-* *-1 avec une entité supplémentaire, qui portera autant d'attributs que tu le souhaites, notamment ta quantité.

  5. #5
    Membre confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2013
    Messages
    81
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loiret (Centre)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2013
    Messages : 81
    Par défaut
    Donc je vais avoir ma classe carrelage :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    @Entity
    @Table(name="carrelage")
    public class Carrelage {
    	@Id
    	@Column(name="codeArticle")
    	private String codeArticle;
    @OneToMany(mappedBy="carrelage")
    	private List<Commande_Carrelage> refCommande;
    ma classe Commande_Carrelage :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
     
    @Entity
    @Table(name="Commande_Carrelage")
    public class Commande_Carrelage {
     
    	@Id
    	@ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }
    			)
    	@JoinColumn(name="codeArticle")
    	private Carrelage carrelage;
     
    	@Id
    	@ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }
    			)
    	@JoinColumn(name="refCommande")
    	private Commande commande;
     
    	@Column(name="quantite")
    	private int quantite;
    et ma classe Commande :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    @Entity
    @Table(name="demandeEchantillon")
    public class Commande {
    	@Id
    	@GeneratedValue(strategy=GenerationType.AUTO)
    	@Column(name="refCommande")
    	private int refCommande;
           @OneToMany(mappedBy="commande")
    	private List<Commande_Carrelage> com;
    Si j'ai bien compris ?

  6. #6
    Membre Expert
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 277
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 277
    Par défaut
    Oui c'est l'idée.

  7. #7
    Membre confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2013
    Messages
    81
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loiret (Centre)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2013
    Messages : 81
    Par défaut A l'aiiiidddeeeeee
    J'ai des erreurs je ne comprends pas au secours je vous envoi le code :

    Ma classe Commande :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    @Entity
    @Table(name="demandeEchantillon")
    public class Commande {
    	@Id
    	@GeneratedValue(strategy=GenerationType.AUTO)
    	@Column(name="refDemande")
    	private int refDemande;
    	@Column(name="status", nullable=false)
    	private String status;
    	@Column(name="date", nullable=false)
    	private Date date;
    	@Column(name="prix", nullable=false)
    	private double prix;
    	@OneToOne(cascade={CascadeType.ALL})
    	@JoinColumn(
    			name="clientEchantillonID", 
    			referencedColumnName="id"
    	)
    	private ClientEchantillon clientEchantillon;
    	@OneToMany(
    			mappedBy = "carrelage",
    			targetEntity = Liaison.class
    	)
    	private List<Liaison> listeCarrelage;
     
    	public int getRefDemande() {
    		return refDemande;
    	}
    	public void setRefDemande(int refDemande) {
    		this.refDemande = refDemande;
    	}
    	public String getStatus() {
    		return status;
    	}
    	public void setStatus(String status) {
    		this.status = status;
    	}
    	public Date getDate() {
    		return date;
    	}
    	public void setDate(Date date) {
    		this.date = date;
    	}
    	public double getPrix() {
    		return prix;
    	}
    	public void setPrix(double prix) {
    		this.prix = prix;
    	}
    	public ClientEchantillon getClientEchantillon() {
    		return clientEchantillon;
    	}
    	public void setClientEchantillon(ClientEchantillon clientEchantillon) {
    		this.clientEchantillon = clientEchantillon;
    	}
    	public List<Liaison> getListeCarrelage() {
    		return listeCarrelage;
    	}
    	public void setListeCarrelage(List<Liaison> listeCarrelage) {
    		this.listeCarrelage = listeCarrelage;
    	}
    	public Commande(String status,
    			ClientEchantillon clientEchantillon,Double prix) {
    		super();
    		this.status = status;
    		this.clientEchantillon = clientEchantillon;
    		this.prix=prix;
    		this.date=new Date();
    	}
     
    	public Commande(){
    		this.date=new Date();
    	}
     
    	public boolean estFini(){
    		return(this.status.equals("Envoyé"));
    	}
     
    }
    Ma classe CarrelagesPub :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
     
    @Entity
    @Table(name="carrelagesPub")
    public class CarrelagesPub {
    	@Id
    	@Column(name="codeArticle")
    	private String codeArticle;
    	@Column(name="designation", nullable=false)
    	private String designation;
    	@Column(name="prixUnitaire", nullable=false)
    	private double prixUnitaire;
    	@Column(name="prixEchantillon", nullable=false)
    	private double prixEchantillon;
    	@Column(name="dimension", nullable=false)
    	private String dimension;
    	@Column(name="piece1", nullable=false)
    	private String piece1;
    	@Column(name="piece2")
    	private String piece2;
    	@Column(name="piece3")
    	private String piece3;
    	@Column(name="type1", nullable=false)
    	private String type1;
    	@Column(name="type2")
    	private String type2;
    	@Column(name="image", nullable=false)
    	private String image;
    	@OneToMany(
    			mappedBy = "carrelage",
    			targetEntity = Liaison.class
        )
    	private List<Liaison> demandeEchantillon;
     
    	public String getCodeArticle() {
    		return codeArticle;
    	}
    	public void setCodeArticle(String codeArticle) {
    		this.codeArticle = codeArticle;
    	}
    	public String getDesignation() {
    		return designation;
    	}
    	public void setDesignation(String designation) {
    		this.designation = designation;
    	}
    	public double getPrixUnitaire() {
    		return prixUnitaire;
    	}
    	public void setPrixUnitaire(double prixUnitaire) {
    		this.prixUnitaire = prixUnitaire;
    	}
    	public double getPrixEchantillon() {
    		return prixEchantillon;
    	}
    	public void setPrixEchantillon(double prixEchantillon) {
    		this.prixEchantillon = prixEchantillon;
    	}
    	public String getPiece1() {
    		return this.piece1;
    	}
    	public void setPiece1(String type) {
    		this.piece1 = type;
    	}
    	public String getPiece2() {
    		return piece2;
    	}
    	public void setPiece2(String piece2) {
    		this.piece2 = piece2;
    	}
    	public String getPiece3() {
    		return piece3;
    	}
    	public void setPiece3(String piece3) {
    		this.piece3 = piece3;
    	}
    	public String getType1() {
    		return type1;
    	}
    	public void setType1(String type1) {
    		this.type1 = type1;
    	}
    	public String getType2() {
    		return type2;
    	}
    	public void setType2(String type2) {
    		this.type2 = type2;
    	}
    	public String getImage() {
    		return image;
    	}
    	public void setImage(String image) {
    		this.image = image;
    	}
    	public List<Liaison> getDemandeEchantillon() {
    		return demandeEchantillon;
    	}
    	public void setDemandeEchantillon(List<Liaison> demEchantillon) {
    		this.demandeEchantillon = demEchantillon;
    	}
    	public String getDimension() {
    		return dimension;
    	}
    	public void setDimension(String dimension) {
    		this.dimension = dimension;
    	}
     
    	public CarrelagesPub(String codeArticle, String designation,
    			double prixUnitaire, double prixEchantillon, String dimension,
    			String piece1, String piece2, String piece3, String type1,
    			String type2, String image,
    			List<Liaison> demandeEchantillon) {
    		super();
    		this.codeArticle = codeArticle;
    		this.designation = designation;
    		this.prixUnitaire = prixUnitaire;
    		this.prixEchantillon = prixEchantillon;
    		this.dimension = dimension;
    		this.piece1 = piece1;
    		this.piece2 = piece2;
    		this.piece3 = piece3;
    		this.type1 = type1;
    		this.type2 = type2;
    		this.image = image;
    		this.demandeEchantillon = demandeEchantillon;
    	}
    	public CarrelagesPub(){
     
    	}
     
    }
    et ma classe Liaison qui est enfaite la relation avec paramètre :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    package modeleBase;
     
    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToMany;
    import javax.persistence.OneToOne;
    import javax.persistence.Table;
     
    @Entity
    @Table(name="liaison")
    public class Liaison {
     
    	@Id
    	@ManyToOne
    	@JoinColumn(
    			name="codeArticle", 
    			referencedColumnName="codeArticle"
    	)
    	private CarrelagesPub carrelage;
     
    	@Id
    	@ManyToOne
    	@JoinColumn(
    			name="refCommande", 
    			referencedColumnName="refCommande"
    	)
    	private Commande commande;
     
    	@Column(name="quantite")
    	private int quantite;
     
    	public CarrelagesPub getCarrelage() {
    		return carrelage;
    	}
     
    	public void setCarrelage(CarrelagesPub carrelage) {
    		this.carrelage = carrelage;
    	}
     
    	public Commande getCommande() {
    		return commande;
    	}
     
    	public void setCommande(Commande commande) {
    		this.commande = commande;
    	}
     
    	public int getQuantite() {
    		return quantite;
    	}
     
    	public void setQuantite(int quantite) {
    		this.quantite = quantite;
    	}
     
    	public Liaison(CarrelagesPub carrelage, Commande commande, int quantite) {
    		super();
    		this.carrelage = carrelage;
    		this.commande = commande;
    		this.quantite = quantite;
    	}
     
    	public Liaison() {
    		super();
    	}
     
     
    }
    ma classe de test :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    package modeleBaseTest;
     
    import java.util.ArrayList;
    import java.util.List;
     
    import org.hibernate.Session;
    import org.hibernate.Transaction;
     
    import modeleBase.CarrelagesPub;
    import modeleBase.ClientEchantillon;
    import modeleBase.Commande;
    import modeleBase.Liaison;
    import modeleBase.Utilisateur;
     
    public class TestCreationBDD {
     
    	/**
             * @param args
             */
    	public static void main(String[] args) {
    		CarrelagesPub c=new CarrelagesPub();
    		c.setCodeArticle("TT33");
    		c.setDesignation("carrelage 1");
    		c.setImage("exemple.jpg");
    		c.setPrixUnitaire(24.0);
    		c.setPrixEchantillon(19.0);
    		c.setPiece1("SDB");
    		c.setPiece2("terrasse");
    		c.setDimension("45x45");
    		c.setType1("SGF");
    		CarrelagesPub c2=new CarrelagesPub();
    		c2.setCodeArticle("TT34");
    		c2.setDesignation("carrelage 2");
    		c2.setImage("exemple.jpg");
    		c2.setPrixUnitaire(28.0);
    		c2.setPrixEchantillon(19.0);
    		c2.setPiece1("terrasse");
    		c2.setPiece2("SDB");
    		c2.setPiece3("sejour/salon/cuisine");
    		c2.setDimension("45x45");
    		c2.setType1("SGF");
    		CarrelagesPub c3=new CarrelagesPub();
    		c3.setCodeArticle("PMO1");
    		c3.setDesignation("produitmise en oeuvre colle");
    		c3.setImage("exemple.jpg");
    		c3.setPrixUnitaire(28.0);
    		c3.setPrixEchantillon(19.0);
    		c3.setPiece1("terrasse");
    		c3.setDimension("45x45");
    		c3.setType1("PMO");
     
    		ClientEchantillon client=new ClientEchantillon("M","Laluque","Florian",130,"rue du Pegas",null,45760,"Marigny","flomomo12@msn.com",238751757);
     
    		Commande demande = new Commande("attente paiement",client,28.0);
     
    		List<Liaison> listeLiaison = new ArrayList<Liaison>();
    		Liaison l1 = new Liaison(c,demande,12);
    		listeLiaison.add(l1);
     
    		demande.setListeCarrelage(listeLiaison);
    		c.setDemandeEchantillon(listeLiaison);
     
    		Utilisateur u=new Utilisateur("flomomo","azerty","root","root");
     
    		Session s = hibernateUtils.HibernateUtils.getSession();
     
    		// DÈbut de la transaction
    		Transaction t = s.beginTransaction();
     
    		// Enregistrement de l'event
    		s.save(c);
    		s.save(c2);
    		s.save(c3);
    		s.save(l1);
    		s.save(demande);
    		s.save(u);
     
    		// Fin de la transaction
    		t.commit();
     
    		// Fermeture de la session Hibernate
    		s.close();
    	}
     
    }

    et j'obtiens l'erreur suivante :


    Exception in thread "main" java.lang.ExceptionInInitializerError
    at modeleBaseTest.TestCreationBDD.main(TestCreationBDD.java:65)
    Caused by: java.lang.RuntimeException: ProblËme de configuration : Could not determine type for: modeleBase.CarrelagesPub, at table: liaison, for columns: [org.hibernate.mapping.Column(carrelage)]
    at hibernateUtils.HibernateUtils.<clinit>(HibernateUtils.java:16)
    ... 1 more
    Caused by: org.hibernate.MappingException: Could not determine type for: modeleBase.CarrelagesPub, at table: liaison, for columns: [org.hibernate.mapping.Column(carrelage)]
    at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:292)
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:276)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:216)
    at org.hibernate.cfg.Configuration.validate(Configuration.java:1135)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1320)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)
    at hibernateUtils.HibernateUtils.<clinit>(HibernateUtils.java:14)
    ... 1 more


    Quelqu'un voit le problème après deux jours moi je vois plus rien ....

    Merci

  8. #8
    Membre Expert
    Profil pro
    Inscrit en
    Août 2006
    Messages
    3 277
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 3 277
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    @Id
    	@ManyToOne
    	@JoinColumn(
    			name="refCommande", 
    			referencedColumnName="refCommande"
    	)
    	private Commande commande;
    Je pense que ta referencedColumnName n'a pas le bon nom, elle devrait plutot s'appeler refDemande, du nom de la colonne dans la table commande.

  9. #9
    Membre confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mai 2013
    Messages
    81
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loiret (Centre)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mai 2013
    Messages : 81
    Par défaut
    Citation Envoyé par fr1man Voir le message
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    @Id
    	@ManyToOne
    	@JoinColumn(
    			name="refCommande", 
    			referencedColumnName="refCommande"
    	)
    	private Commande commande;
    Je pense que ta referencedColumnName n'a pas le bon nom, elle devrait plutot s'appeler refDemande, du nom de la colonne dans la table commande.
    toujours le même message d'erreur :-(

Discussions similaires

  1. PGSQL-EclipseLink Relation One To Many et Many To One
    Par faitor1 dans le forum Persistance des données
    Réponses: 0
    Dernier message: 30/01/2015, 20h46
  2. Réponses: 5
    Dernier message: 10/08/2014, 18h25
  3. Réponses: 0
    Dernier message: 25/07/2011, 18h42
  4. [sqlalchemy] Création d'une relation Many to Many avec attributs
    Par joubu dans le forum Bibliothèques tierces
    Réponses: 6
    Dernier message: 05/08/2010, 21h13
  5. Un peu de mal a comprendre le concepte "one-to-many" et "many-to-many"
    Par chriscoolletoubibe dans le forum Hibernate
    Réponses: 4
    Dernier message: 29/03/2007, 19h50

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo