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

JPA Java Discussion :

Column XYZ is specified twice


Sujet :

JPA Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre Expert Avatar de Madfrix
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    2 326
    Détails du profil
    Informations personnelles :
    Localisation : France, Gironde (Aquitaine)

    Informations forums :
    Inscription : Juin 2007
    Messages : 2 326
    Par défaut Column XYZ is specified twice
    Bonjour,

    j'ai fais pour tester JPA (implémentation EclipseLink) un mapping n-n avec une table d'association (avec clé composite + champ supplémentaire).

    Voici le mapping :

    table abonnes :
    id
    nom
    prenom

    table livres:
    id
    libelle

    table emprunts:
    abonne_id
    livre_id
    test

    EclipseLink m'a généré les entités suivantes :

    Entité Abonne
    Code java : 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
     
    package com.entities;
     
    import java.io.Serializable;
    import javax.persistence.*;
    import java.util.List;
     
     
    /**
     * The persistent class for the abonnes database table.
     * 
     */
    @Entity
    @Table(name="abonnes")
    public class Abonne implements Serializable {
    	private static final long serialVersionUID = 1L;
     
    	@Id
    	@GeneratedValue(strategy=GenerationType.AUTO)
    	private int id;
     
    	private String nom;
     
    	private String prenom;
     
    	//bi-directional many-to-one association to Emprunt
    	@OneToMany(mappedBy="abonne")
    	private List<Emprunt> emprunts;
     
        public Abonne() {
        }
     
    	public int getId() {
    		return this.id;
    	}
     
    	public void setId(int id) {
    		this.id = id;
    	}
     
    	public String getNom() {
    		return this.nom;
    	}
     
    	public void setNom(String nom) {
    		this.nom = nom;
    	}
     
    	public String getPrenom() {
    		return this.prenom;
    	}
     
    	public void setPrenom(String prenom) {
    		this.prenom = prenom;
    	}
     
    	public List<Emprunt> getEmprunts() {
    		return this.emprunts;
    	}
     
    	public void setEmprunts(List<Emprunt> emprunts) {
    		this.emprunts = emprunts;
    	}
     
    }

    Entité Livre
    Code java : 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
     
    package com.entities;
     
    import java.io.Serializable;
    import javax.persistence.*;
    import java.util.List;
     
     
    /**
     * The persistent class for the livres database table.
     * 
     */
    @Entity
    @Table(name="livres")
    public class Livre implements Serializable {
    	private static final long serialVersionUID = 1L;
     
    	@Id
    	@GeneratedValue(strategy=GenerationType.AUTO)
    	private int id;
     
    	private String libelle;
     
    	//bi-directional many-to-one association to Emprunt
    	@OneToMany(mappedBy="livre")
    	private List<Emprunt> emprunts;
     
        public Livre() {
        }
     
    	public int getId() {
    		return this.id;
    	}
     
    	public void setId(int id) {
    		this.id = id;
    	}
     
    	public String getLibelle() {
    		return this.libelle;
    	}
     
    	public void setLibelle(String libelle) {
    		this.libelle = libelle;
    	}
     
    	public List<Emprunt> getEmprunts() {
    		return this.emprunts;
    	}
     
    	public void setEmprunts(List<Emprunt> emprunts) {
    		this.emprunts = emprunts;
    	}
     
    }

    Entité Emprunt
    Code java : 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
     
    package com.entities;
     
    import java.io.Serializable;
    import javax.persistence.*;
     
     
    /**
     * The persistent class for the emprunts database table.
     * 
     */
    @Entity
    @Table(name="emprunts")
    public class Emprunt implements Serializable {
    	private static final long serialVersionUID = 1L;
     
    	@EmbeddedId
    	private EmpruntPK id;
     
    	private String test;
     
    	//bi-directional many-to-one association to Abonne
        @ManyToOne
    	private Abonne abonne;
     
    	//bi-directional many-to-one association to Livre
        @ManyToOne
    	private Livre livre;
     
        public Emprunt() {
        }
     
    	public EmpruntPK getId() {
    		return this.id;
    	}
     
    	public void setId(EmpruntPK id) {
    		this.id = id;
    	}
     
    	public String getTest() {
    		return this.test;
    	}
     
    	public void setTest(String test) {
    		this.test = test;
    	}
     
    	public Abonne getAbonne() {
    		return this.abonne;
    	}
     
    	public void setAbonne(Abonne abonne) {
    		this.abonne = abonne;
    	}
     
    	public Livre getLivre() {
    		return this.livre;
    	}
     
    	public void setLivre(Livre livre) {
    		this.livre = livre;
    	}
     
    }

    EmbeddedId
    Code java : 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
     
    package com.entities;
     
    import java.io.Serializable;
    import javax.persistence.*;
     
    /**
     * The primary key class for the emprunts database table.
     * 
     */
    @Embeddable
    public class EmpruntPK implements Serializable {
    	//default serial version id, required for serializable classes.
    	private static final long serialVersionUID = 1L;
     
    	@Column(name="abonne_id")
    	private int abonneId;
     
    	@Column(name="livre_id")
    	private int livreId;
     
        public EmpruntPK() {
        }
    	public int getAbonneId() {
    		return this.abonneId;
    	}
    	public void setAbonneId(int abonneId) {
    		this.abonneId = abonneId;
    	}
    	public int getLivreId() {
    		return this.livreId;
    	}
    	public void setLivreId(int livreId) {
    		this.livreId = livreId;
    	}
     
    	public boolean equals(Object other) {
    		if (this == other) {
    			return true;
    		}
    		if (!(other instanceof EmpruntPK)) {
    			return false;
    		}
    		EmpruntPK castOther = (EmpruntPK)other;
    		return 
    			(this.abonneId == castOther.abonneId)
    			&& (this.livreId == castOther.livreId);
     
        }
     
    	public int hashCode() {
    		final int prime = 31;
    		int hash = 17;
    		hash = hash * prime + this.abonneId;
    		hash = hash * prime + this.livreId;
     
    		return hash;
        }
    }

    J'essaie d'insérer des valeurs dans ces 3 tables en procédant comme suit :

    Code java : 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
     
    Abonne abonne = new Abonne();
    Livre livre = new Livre();
    Emprunt emprunt = new Emprunt();
    EmpruntPK empruntPK = new EmpruntPK();
     
    abonne.setNom("Gates");
    abonne.setPrenom("Billouz");
     
    livre.setLibelle("l'info pour les nuls");					
     
    em.persist(abonne);
    em.persist(livre);
    em.flush();
     
    empruntPK.setAbonneId(abonne.getId());
    empruntPK.setLivreId(livre.getId());
    emprunt.setId(empruntPK);
    emprunt.setTest("un test");
     
    em.persist(emprunt);		
    em.flush();

    J'obtiens une exception MySQLSyntaxErrorException :

    Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Column 'abonne_id' specified twice
    Dans les logs, j'ai également ceci :

    Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DatabaseException
    Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Column 'abonne_id' specified twice
    Error Code: 1110
    Call: INSERT INTO emprunts (TEST, abonne_id, livre_id, LIVRE_ID, ABONNE_ID) VALUES (?, ?, ?, ?, ?)
    bind => [un test, 1251, 1252, null, null]
    Effectivement, abonne_id et livre_id apparaissent 2 fois dans l'insert généré. J'ai essayé beaucoup de méthodes d'insertions sans pour autant que l'insert généré change (seulement le binding change). Je ne sais pas si c'est ma méthode d'insertion qui est mauvaise ou bien si c'est mes entités qui sont mal construites...

    Voyez vous l'erreur ?


    Merci

  2. #2
    Membre Expert Avatar de Madfrix
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    2 326
    Détails du profil
    Informations personnelles :
    Localisation : France, Gironde (Aquitaine)

    Informations forums :
    Inscription : Juin 2007
    Messages : 2 326
    Par défaut
    Et comme c'est toujours quand on poste qu'on trouve la solution et bien j'ai trouvé

    En relisant certains tutos notamment celui ci, j'ai vu cette entité :

    Code java : 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
     
    @Entity
    @Table(name="PROJ_EMP")
    @IdClass(ProjectAssociationId.class)
    public class ProjectAssociation {
      @Id
      private long employeeId;
      @Id
      private long projectId;
      @Column("IS_PROJECT_LEAD")
      private boolean isProjectLead;
      @ManyToOne
      @PrimaryKeyJoinColumn(name="EMPLOYEEID", referencedColumnName="ID")
      /* if this JPA model doesn't create a table for the "PROJ_EMP" entity,
      *  please comment out the @PrimaryKeyJoinColumn, and use the ff:
      *  @JoinColumn(name = "employeeId", updatable = false, insertable = false)
      * or @JoinColumn(name = "employeeId", updatable = false, insertable = false, referencedColumnName = "id")
      */
      private Employee employee;
      @ManyToOne
      @PrimaryKeyJoinColumn(name="PROJECTID", referencedColumnName="ID")
      /* the same goes here:
      *  if this JPA model doesn't create a table for the "PROJ_EMP" entity,
      *  please comment out the @PrimaryKeyJoinColumn, and use the ff:
      *  @JoinColumn(name = "projectId", updatable = false, insertable = false)
      * or @JoinColumn(name = "projectId", updatable = false, insertable = false, referencedColumnName = "id")
      */
      private Project project;
      ...
    }

    L'ajout de @JoinColumn dans mon entité Emprunt a résolu mon problème. Bizarre quand même que EclipseLink m'a pas généré cette annotations non ?

Discussions similaires

  1. Réponses: 5
    Dernier message: 17/02/2009, 14h07
  2. Erreur : You have specified an invalid column ordinal
    Par anthony_rexis dans le forum Windows Forms
    Réponses: 0
    Dernier message: 03/02/2009, 15h23
  3. Réponses: 6
    Dernier message: 16/07/2008, 09h48
  4. Erreur lors de CREATE VIEW: must specify column name..
    Par nek_kro_kvlt dans le forum SQL
    Réponses: 1
    Dernier message: 29/09/2006, 10h39
  5. [MySQL] Column 'description' specified twice
    Par Spouine dans le forum PHP & Base de données
    Réponses: 4
    Dernier message: 01/10/2005, 09h38

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