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 ManyToMany Hibernate


Sujet :

Hibernate Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé Avatar de zemzoum89
    Profil pro
    Étudiant
    Inscrit en
    Janvier 2010
    Messages
    373
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2010
    Messages : 373
    Par défaut Relation ManyToMany Hibernate
    Bonjour voila j'ai créé une relation manytomany normalement elle me génère 4 tables les deux tables et la tables associatives et une quatrième table qu'il a créé de son propre gré, celle ci contient l'idtable1 idtable1 et idtable2 mais elle est toujours vide et en plus j'ai une duplication de clé aidez moi svp je bloque la merci beaucoup

  2. #2
    Membre Expert
    Avatar de Nesmontou
    Homme Profil pro
    Architecte logiciel
    Inscrit en
    Septembre 2004
    Messages
    1 612
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Architecte logiciel
    Secteur : Finance

    Informations forums :
    Inscription : Septembre 2004
    Messages : 1 612
    Par défaut
    Bonjour,

    Il va nous falloir un peu de code pour pouvoir t'aider

  3. #3
    Membre éclairé Avatar de zemzoum89
    Profil pro
    Étudiant
    Inscrit en
    Janvier 2010
    Messages
    373
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2010
    Messages : 373
    Par défaut
    la classe game:
    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
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package Games.modeles;
     
    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.Collection;
    import javax.persistence.CascadeType;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.ManyToMany;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToMany;
     
    /**
     *
     * @author ROOT
     */
    @Entity
    public class Game implements Serializable {
        private static final long serialVersionUID = 1L;
     
        public enum Age {A3, A7, A12, A16, A18}
     
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private int id;
        private String gameName;
        private String description;
        private Age catAge;
     
        // Gestion des relations
     
        @OneToMany(fetch=FetchType.EAGER)
        Collection<GameCategory> catList = new ArrayList<GameCategory>();
     
        @ManyToMany(fetch=FetchType.EAGER)
        Collection<Tag> tagList = new ArrayList<Tag>();
     
    //    @ManyToMany(cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
    //    Collection<EOrder> ordersList = new ArrayList<EOrder>();
     
        @OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.LAZY)
        Collection<CommentsAndMarks> commentAndMarksList = new ArrayList<CommentsAndMarks>();
     
        @ManyToOne(fetch=FetchType.EAGER)
        Distributor distributor;
     
         @OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
        Collection<Photo> photoList = new ArrayList<Photo>();
     
        public Game() {
        }
     
        public Game(String gameName,String Description, Age catAge, Distributor distributor) {
            this.gameName = gameName;
     
            this.description = Description;
            this.catAge = catAge;
            this.distributor = distributor;
     
        }
     
     
        public String getDescription() {
            return description;
        }
     
     
        public Age getCatAge() {
            return catAge;
        }
     
        public Collection<GameCategory> getCatList() {
            return catList;
        }
     
        public Collection<CommentsAndMarks> getCommentAndMarksList() {
            return commentAndMarksList;
        }
     
     
        public Distributor getDistributor() {
            return distributor;
        }
     
        public String getGameName() {
            return gameName;
        }
     
        public Collection<Tag> getTagList() {
            return tagList;
        }
     
        public void setDescription(String Description) {
            this.description = Description;
        }
     
     
        public void setCatAge(Age catAge) {
            this.catAge = catAge;
        }
     
        public void setCatList(Collection<GameCategory> catList) {
            this.catList = catList;
        }
     
        public void addCatToList(GameCategory cat) {
            this.catList.add(cat);
        }
     
        public void setCommentAndMarksList(Collection<CommentsAndMarks> commentAndMarksList) {
            this.commentAndMarksList = commentAndMarksList;
        }
     
        public void addCommentAndMarkToList(CommentsAndMarks com) {
            this.commentAndMarksList.add(com);
        }
        public void setDistributor(Distributor distributor) {
            this.distributor = distributor;
        }
     
        public void setGameName(String gameName) {
            this.gameName = gameName;
        }
     
        public void setTagList(Collection<Tag> tagList) {
            this.tagList = tagList;
        }
     
        public void addTagToList(Tag tag) {
            this.tagList.add(tag);
        }
     
        public void setPhotoList(Collection<Photo> photoList) {
            this.photoList = photoList;
        }
        public void addPhotoToList(Photo photo) {
            this.photoList.add(photo);
        }
     
        public Collection<Photo> getPhotoList() {
            return photoList;
        }
     
     
     
     
    //    public Collection<EOrder> getOrdersList() {
    //        return ordersList;
    //    }
    //
    //    public void setOrdersList(Collection<EOrder> ordersList) {
    //        this.ordersList = ordersList;
    //    }
    //
    //     public void addOrderToList(EOrder order) {
    //        this.ordersList.add(order);
    //    }
     
     
     
     
     
     
     
     
     
        public int getId() {
            return id;
        }
     
        public void setId(int id) {
            this.id = id;
        }
     
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (int) id;
            return hash;
        }
     
        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof Game)) {
                return false;
            }
            Game other = (Game) object;
            if (this.id != other.id) {
                return false;
            }
            return true;
        }
     
        @Override
        public String toString() {
            return "Games.modeles.Game[id=" + id + "]";
        }
     
    }

    La classe category:
    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
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package Games.modeles;
     
    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.Collection;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.ManyToMany;
    import javax.persistence.OneToMany;
     
    /**
     *
     * @author ROOT */
    @Entity
    public class Category implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private int id;
        private String catName;
     
        // Gestion des relations
        @OneToMany(fetch=FetchType.LAZY)
        Collection<GameCategory> gamesList = new ArrayList<GameCategory>();
     
        public Category() {
        }
     
        public Category(String catName) {
            this.catName = catName;
        }
     
     
     
        public int getId() {
            return id;
        }
     
        public void setId(int id) {
            this.id = id;
        }
     
        public String getCatName() {
            return catName;
        }
     
        public Collection<GameCategory> getGamesList() {
            return gamesList;
        }
     
        public void setCatName(String catName) {
            this.catName = catName;
        }
     
        public void setGamesList(Collection<GameCategory> gamesList) {
            this.gamesList = gamesList;
        }
     
        public void addGameToList(GameCategory game) {
            this.gamesList.add(game);
        }
     
     
     
        @Override
        public int hashCode() {
            int hash = 0;
            hash += (int) id;
            return hash;
        }
     
        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
            if (!(object instanceof Category)) {
                return false;
            }
            Category other = (Category) object;
            if (this.id != other.id) {
                return false;
            }
            return true;
        }
     
        @Override
        public String toString() {
            return "Games.modeles.Category[id=" + id + "]";
        }
     
    }

    La classe associative GameCategory:

    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
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package Games.modeles;
     
    import java.io.Serializable;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.Id;
    import javax.persistence.IdClass;
    import javax.persistence.ManyToOne;
    import javax.persistence.PrimaryKeyJoinColumn;
    import javax.persistence.Table;
     
     
    /**
     *
     * @author ROOT */
    @Entity
    @IdClass(GameCategoryId.class)
    public class GameCategory implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        private int gameId;
        @Id
        private int catId;
     
        private int stockQuantity;
     
        private double price;
     
     
      @ManyToOne(fetch=FetchType.EAGER)
      @PrimaryKeyJoinColumn(name="GAMEID", referencedColumnName="ID")
      private Game game;
     
      @ManyToOne(fetch=FetchType.EAGER)
      @PrimaryKeyJoinColumn(name="CATID", referencedColumnName="ID")
      private Category cat;
     
        public void setCat(Category cat) {
            this.cat = cat;
        }
     
        public void setCatId(int catId) {
            this.catId = catId;
        }
     
        public void setGame(Game game) {
            this.game = game;
        }
     
        public void setGameId(int gameId) {
            this.gameId = gameId;
        }
     
        public void setStockQuantity(int stockQuantity) {
            this.stockQuantity = stockQuantity;
        }
     
        public Category getCat() {
            return cat;
        }
     
        public int getCatId() {
            return catId;
        }
     
        public Game getGame() {
            return game;
        }
     
        public int getGameId() {
            return gameId;
        }
     
        public int getStockQuantity() {
            return stockQuantity;
        }
     
        public double getPrice() {
            return price;
        }
     
        public void setPrice(double price) {
            this.price = price;
        }
     
     
     
    @Override
        public int hashCode() {
            int hash = 0;
    //        hash += (id != null ? id.hashCode() : 0);
            return hash;
        }
     
        @Override
        public boolean equals(Object object) {
            // TODO: Warning - this method won't work in the case the id fields are not set
    //        if (!(object instanceof GameCategory)) {
    //            return false;
    //        }
    //        GameCategory other = (GameCategory) object;
    //        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
    //            return false;
    //        }
            return true;
        }
     
        @Override
        public String toString() {
           // return "Games.modeles.GameCategory[id=" + id + "]";
            return "à effacer";
        }
     
    }

    et celle la c'est la classe des IDs:
    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
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package Games.modeles;
     
    import java.io.Serializable;
     
    /**
     *
     * @author ROOT */
    public class GameCategoryId implements Serializable {
     
        private int gameId;
        private int catId;
     
        public int getCatId() {
            return catId;
        }
     
        public int getGameId() {
            return gameId;
        }
     
        public void setCatId(int catId) {
            this.catId = catId;
        }
     
        public void setGameId(int gameId) {
            this.gameId = gameId;
        }
     
     
        @Override
    public int hashCode() {
        return (int)(gameId + catId);
      }
     
        @Override
      public boolean equals(Object object) {
        if (object instanceof GameCategoryId) {
          GameCategoryId otherId = (GameCategoryId) object;
          return (otherId.gameId == this.gameId) && (otherId.catId == this.catId);
        }
        return false;
      }
    }
    Voila j'espère que vous allez pouvoir m'aider...

  4. #4
    Membre très actif

    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    486
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2009
    Messages : 486
    Billets dans le blog
    5
    Par défaut
    L'avantage d'Hibernate est de zapper la table de jointure. Soit dans la base de donnée la table A et B, ainsi que la table de jointure A_B.

    Dans le code Java, on a la classe A et la classe B.

    Dans la classe A, on aura:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    @ManyToMany(fetch=FetchType.LAZY)
    	@JoinTable(name="A_B",joinColumns=@JoinColumn(name="id_A"),inverseJoinColumns=@JoinColumn(name="id_B"))
    	private Set<B> bSet;
    Dans la classe B, on aura:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    @ManyToMany(fetch=FetchType.EAGER,mappedBy="bSet")
    	private Set<Service> bSet;
    A noter que dans le code Java, la classe A est propriétaire de la relation. Tu pourra effacer des relations à partir de Java que en modifiant principalement la classe A (voir ici, j'ai eu le problème).

  5. #5
    Membre très actif

    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    486
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2009
    Messages : 486
    Billets dans le blog
    5
    Par défaut
    Par contre, si tu cherche une relation ternaire, c'est plus compliqué (voir ici)

Discussions similaires

  1. Réponses: 0
    Dernier message: 19/04/2009, 20h50
  2. Réponses: 8
    Dernier message: 27/02/2009, 01h37
  3. EJB3 Relation ManyToMany
    Par cow_boy17 dans le forum JPA
    Réponses: 1
    Dernier message: 21/03/2008, 10h12
  4. [HQL] Pb avec relation ManyToMany
    Par jc63 dans le forum Hibernate
    Réponses: 1
    Dernier message: 26/07/2007, 14h35
  5. [NHibernate.Mapping.Attributes] Relation ManyToMany
    Par anthyme dans le forum NHibernate
    Réponses: 2
    Dernier message: 12/07/2007, 20h34

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