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 :

Problème de reverse hibernate many-to-many [Mapping]


Sujet :

Hibernate Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Femme Profil pro
    Inscrit en
    Juin 2012
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Juin 2012
    Messages : 11
    Par défaut Problème de reverse hibernate many-to-many
    Bonjour à tous,

    malgré mes recherches, je dois me résigner à vous appeler au secours.

    J'ai une base de donnée simple avec une table produit (id_prodit,name_produit) en relation many-to-many avec une table image (id_image, name_image).
    Lorsque j'utilise Hibernate tools sous éclipe, je prend soin de cocher la case "Detect many-to-many tables mais il me génère 4 beans.

    Ais-je fais une erreur quelque part? Dois-je (et alors, comment puis-je) configurer hibernate pour le forcer à détecter la relation ? Merci de votre aide !

    BDD :
    Table image :
    • id_image (auto_increment, primaire)
    • name_image


    Table produit :
    • id_produit (auto_increment, primaire)
    • name_produit


    Table produit_image :
    • id_produit (clé étrangère, relation à produit.id_produit)
    • id_image (clé étrangère, relation à image.id_image)



    Beans générés :

    Image :
    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
     
    public class Image implements java.io.Serializable {
     
    	private Integer idImage;
    	private String nameImage;
    	private Set<ImageProduit> imageProduits = new HashSet<ImageProduit>(0);
     
    	public Image() {
    	}
     
    	public Image(String nameImage) {
    		this.nameImage = nameImage;
    	}
     
    	public Image(String nameImage, Set<ImageProduit> imageProduits) {
    		this.nameImage = nameImage;
    		this.imageProduits = imageProduits;
    	}
     
    	@Id
    	@GeneratedValue(strategy = IDENTITY)
    	@Column(name = "id_image", unique = true, nullable = false)
    	public Integer getIdImage() {
    		return this.idImage;
    	}
     
    	public void setIdImage(Integer idImage) {
    		this.idImage = idImage;
    	}
     
    	@Column(name = "name_image", nullable = false, length = 25)
    	public String getNameImage() {
    		return this.nameImage;
    	}
     
    	public void setNameImage(String nameImage) {
    		this.nameImage = nameImage;
    	}
     
    	@OneToMany(fetch = FetchType.LAZY, mappedBy = "image")
    	public Set<ImageProduit> getImageProduits() {
    		return this.imageProduits;
    	}
     
    	public void setImageProduits(Set<ImageProduit> imageProduits) {
    		this.imageProduits = imageProduits;
    	}
     
    }
    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
     
    @Entity
    @Table(name = "produit", catalog = "test1")
    public class Produit implements java.io.Serializable {
     
    	private Integer idProduit;
    	private String nameProduit;
    	private Set<ImageProduit> imageProduits = new HashSet<ImageProduit>(0);
     
    	public Produit() {
    	}
     
    	public Produit(String nameProduit) {
    		this.nameProduit = nameProduit;
    	}
     
    	public Produit(String nameProduit, Set<ImageProduit> imageProduits) {
    		this.nameProduit = nameProduit;
    		this.imageProduits = imageProduits;
    	}
     
    	@Id
    	@GeneratedValue(strategy = IDENTITY)
    	@Column(name = "id_produit", unique = true, nullable = false)
    	public Integer getIdProduit() {
    		return this.idProduit;
    	}
     
    	public void setIdProduit(Integer idProduit) {
    		this.idProduit = idProduit;
    	}
     
    	@Column(name = "name_produit", nullable = false, length = 25)
    	public String getNameProduit() {
    		return this.nameProduit;
    	}
     
    	public void setNameProduit(String nameProduit) {
    		this.nameProduit = nameProduit;
    	}
     
    	@OneToMany(fetch = FetchType.LAZY, mappedBy = "produit")
    	public Set<ImageProduit> getImageProduits() {
    		return this.imageProduits;
    	}
     
    	public void setImageProduits(Set<ImageProduit> imageProduits) {
    		this.imageProduits = imageProduits;
    	}
     
    }
    ImageProduit :
    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
     
    @Entity
    @Table(name = "image_produit", catalog = "test1")
    public class ImageProduit implements java.io.Serializable {
     
    	private ImageProduitId id;
    	private Image image;
    	private Produit produit;
     
    	public ImageProduit() {
    	}
     
    	public ImageProduit(ImageProduitId id, Image image, Produit produit) {
    		this.id = id;
    		this.image = image;
    		this.produit = produit;
    	}
     
    	@EmbeddedId
    	@AttributeOverrides({
    			@AttributeOverride(name = "idProduit", column = @Column(name = "id_produit", nullable = false)),
    			@AttributeOverride(name = "idImage", column = @Column(name = "id_image", nullable = false)) })
    	public ImageProduitId getId() {
    		return this.id;
    	}
     
    	public void setId(ImageProduitId id) {
    		this.id = id;
    	}
     
    	@ManyToOne(fetch = FetchType.LAZY)
    	@JoinColumn(name = "id_image", nullable = false, insertable = false, updatable = false)
    	public Image getImage() {
    		return this.image;
    	}
     
    	public void setImage(Image image) {
    		this.image = image;
    	}
     
    	@ManyToOne(fetch = FetchType.LAZY)
    	@JoinColumn(name = "id_produit", nullable = false, insertable = false, updatable = false)
    	public Produit getProduit() {
    		return this.produit;
    	}
     
    	public void setProduit(Produit produit) {
    		this.produit = produit;
    	}
     
    }
    ImageProduitID :
    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
     
    @Embeddable
    public class ImageProduitId implements java.io.Serializable {
     
    	private int idProduit;
    	private int idImage;
     
    	public ImageProduitId() {
    	}
     
    	public ImageProduitId(int idProduit, int idImage) {
    		this.idProduit = idProduit;
    		this.idImage = idImage;
    	}
     
    	@Column(name = "id_produit", nullable = false)
    	public int getIdProduit() {
    		return this.idProduit;
    	}
     
    	public void setIdProduit(int idProduit) {
    		this.idProduit = idProduit;
    	}
     
    	@Column(name = "id_image", nullable = false)
    	public int getIdImage() {
    		return this.idImage;
    	}
     
    	public void setIdImage(int idImage) {
    		this.idImage = idImage;
    	}
     
    	public boolean equals(Object other) {
    		if ((this == other))
    			return true;
    		if ((other == null))
    			return false;
    		if (!(other instanceof ImageProduitId))
    			return false;
    		ImageProduitId castOther = (ImageProduitId) other;
     
    		return (this.getIdProduit() == castOther.getIdProduit())
    				&& (this.getIdImage() == castOther.getIdImage());
    	}
     
    	public int hashCode() {
    		int result = 17;
     
    		result = 37 * result + this.getIdProduit();
    		result = 37 * result + this.getIdImage();
    		return result;
    	}
     
    }

  2. #2
    Membre émérite Avatar de Gardyen
    Homme Profil pro
    Bio informaticien
    Inscrit en
    Août 2005
    Messages
    637
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Bio informaticien

    Informations forums :
    Inscription : Août 2005
    Messages : 637
    Par défaut
    te résigner ?

    en testant tes tables, hibernate tools m'a bien généré 2 beans avec un set de l'autre classe.

    Voilà les configurations que j'ai utilisées, si tu vois une différence (au niveau du basic typed composite key peut-être ?)...
    hibernate.eveng.xml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >
     
    <hibernate-reverse-engineering>
      <table-filter match-catalog="test" match-name="image"/>
      <table-filter match-catalog="test" match-name="produit"/>
      <table-filter match-catalog="test" match-name="produit_image"/>
    </hibernate-reverse-engineering>
    Hibernate Code Generation:
    cases cochées:

    reverse engineer from JDBC connection
    generate basic typed composite id
    detect optimistic lock columns
    detect many-to-many tables
    detect one-to-one association

  3. #3
    Membre averti
    Femme Profil pro
    Inscrit en
    Juin 2012
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Juin 2012
    Messages : 11
    Par défaut
    Salut Gardyen,

    merci de ta réponse !

    j'en suis à me demander si cela ne viendrais pas de ma base ...

    je joint le script, l'erreur est peut être là...

    En te remerciant,
    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
     
    -- phpMyAdmin SQL Dump
    -- version 3.4.9
    -- <a href="http://www.phpmyadmin.net" target="_blank">http://www.phpmyadmin.net</a>
    --
    -- Client: 127.0.0.1
    -- Généré le : Mer 13 Juin 2012 à 10:35
    -- Version du serveur: 5.5.20
    -- Version de PHP: 5.3.9
     
    SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
    SET time_zone = "+00:00";
     
     
    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8 */;
     
    --
    -- Base de données: `test1`
    --
     
    -- --------------------------------------------------------
     
    --
    -- Structure de la table `image`
    --
     
    CREATE TABLE IF NOT EXISTS `image` (
      `id_image` int(11) NOT NULL AUTO_INCREMENT,
      `name_image` varchar(25) NOT NULL,
      PRIMARY KEY (`id_image`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
     
    -- --------------------------------------------------------
     
    --
    -- Structure de la table `image_produit`
    --
     
    CREATE TABLE IF NOT EXISTS `image_produit` (
      `id_produit` int(11) NOT NULL,
      `id_image` int(11) NOT NULL,
      KEY `id_produit` (`id_produit`,`id_image`),
      KEY `id_image` (`id_image`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
     
    -- --------------------------------------------------------
     
    --
    -- Structure de la table `produit`
    --
     
    CREATE TABLE IF NOT EXISTS `produit` (
      `id_produit` int(11) NOT NULL AUTO_INCREMENT,
      `name_produit` varchar(25) NOT NULL,
      PRIMARY KEY (`id_produit`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
     
    --
    -- Contraintes pour les tables exportées
    --
     
    --
    -- Contraintes pour la table `image_produit`
    --
    ALTER TABLE `image_produit`
      ADD CONSTRAINT `image_produit_ibfk_2` FOREIGN KEY (`id_image`) REFERENCES `image` (`id_image`) ON DELETE CASCADE ON UPDATE CASCADE,
      ADD CONSTRAINT `image_produit_ibfk_1` FOREIGN KEY (`id_produit`) REFERENCES `produit` (`id_produit`) ON DELETE CASCADE ON UPDATE CASCADE;
     
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

  4. #4
    Membre émérite Avatar de Gardyen
    Homme Profil pro
    Bio informaticien
    Inscrit en
    Août 2005
    Messages
    637
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Bio informaticien

    Informations forums :
    Inscription : Août 2005
    Messages : 637
    Par défaut
    j'ai utilisé des tables identiques aux tiennes, cela vient donc de la configuration et/ou de la version utilisée...

    j'utilise hibernate tools 3.4.0 et hibernate 4.1.3, et pour la config on a la même je crois...

  5. #5
    Membre averti
    Femme Profil pro
    Inscrit en
    Juin 2012
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Juin 2012
    Messages : 11
    Par défaut
    Effectivement, on utilise les même versions...

    par contre, lorsque je lance Hibernate tools, mon fichier hibernate.reveng.xml reste le même, à savoir :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >
     
    <hibernate-reverse-engineering>
     
    </hibernate-reverse-engineering>

  6. #6
    Membre émérite Avatar de Gardyen
    Homme Profil pro
    Bio informaticien
    Inscrit en
    Août 2005
    Messages
    637
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Bio informaticien

    Informations forums :
    Inscription : Août 2005
    Messages : 637
    Par défaut
    autant pour moi !!

    tu n'as pas créé de clé primaire PRIMARY(id_produit, id_image) dans la table produit_image, du coup hibernate ne voit pas la relation !

    de plus pense à inclure les tables à mapper quand tu génères ton reveng.xml

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 1
    Dernier message: 11/07/2014, 02h22
  2. [hibernate]many-to-many petit problème
    Par Allensan dans le forum Hibernate
    Réponses: 4
    Dernier message: 12/12/2007, 09h25
  3. Hibernate Relations Reflexives Many-to-Many
    Par Shiingo dans le forum Hibernate
    Réponses: 6
    Dernier message: 14/09/2007, 15h57
  4. 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, 18h50
  5. [hibernate 3] mapping many-to-many
    Par darkyspirit dans le forum Hibernate
    Réponses: 4
    Dernier message: 29/12/2006, 19h37

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