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

Java EE Discussion :

[EJB3] Gestion des clés etrangères [EJBQL]


Sujet :

Java EE

Vue hybride

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

    Informations forums :
    Inscription : Juin 2008
    Messages : 19
    Par défaut [EJB3] Gestion des clés etrangères
    Bonjour,



    Voilà, j'ai deux beans entités(commande et Client) reliés par une clé étrangère(codeiata) dans commande.
    je souhaite obtenir la valeur de l'attribut nbavion contenu dans la table commande en fonction du label qui est un attribut de la table client.
    mais j'ai une erreur d

    javax.ejb.TransactionRolledbackLocalException: Exception thrown from bean; nested exception is: java.lang.IllegalArgumentException: You have attempted to set a value of type class java.lang.String for parameter codeiata with expected type of class erp.entity.Client from query string SELECT c FROM Commande c WHERE c.codeiata = :codeiata.

    Pourriez vous m'aider

    Ci dessous les codes :
    Pour Client

    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
    @Entity
    @Table(name = "CLIENT")
    @NamedQueries({@NamedQuery(name = "Client.findByCodeiata", query = "SELECT c FROM Client c WHERE c.codeiata = :codeiata"), 
    @NamedQuery(name = "Client.findByLabel", query = "SELECT c FROM Client c WHERE c.label = :label"), 
    @NamedQuery(name = "Client.findByPays", query = "SELECT c FROM Client c WHERE c.pays = :pays"),
    @NamedQuery(name = "Client.findByContinent", query = "SELECT c FROM Client c WHERE c.continent = :continent")})
    public class Client implements Serializable {
      private static final long serialVersionUID = 1L;
      @Id
      @Column(name = "CODEIATA", nullable = false)
      private String codeiata;
      @Column(name = "LABEL", nullable = false)
      private String label;
      @Column(name = "PAYS", nullable = false)
      private String pays;
      @Column(name = "CONTINENT", nullable = false)
      private String continent;
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "codeiata")
      private Collection<Commande> commandeCollection;
      @OneToMany(cascade = CascadeType.ALL, mappedBy = "codeiata")
      private Collection<Livraison> livraisonCollection;
     
      public Client() {
      }
     
      public Client(String codeiata) {
        this.codeiata = codeiata;
      }
     
      public Client(String codeiata, String label, String pays, String continent) {
        this.codeiata = codeiata;
        this.label = label;
        this.pays = pays;
        this.continent = continent;
      }
     
      public String getCodeiata() {
        return codeiata;
      }
     
      public void setCodeiata(String codeiata) {
        this.codeiata = codeiata;
      }
     
      public String getLabel() {
        return label;
      }
     
      public void setLabel(String label) {
        this.label = label;
      }
     
      public String getPays() {
        return pays;
      }
     
      public void setPays(String pays) {
        this.pays = pays;
      }
     
      public String getContinent() {
        return continent;
      }
     
      public void setContinent(String continent) {
        this.continent = continent;
      }
     
      public Collection<Commande> getCommandeCollection() {
        return commandeCollection;
      }
     
      public void setCommandeCollection(Collection<Commande> commandeCollection) {
        this.commandeCollection = commandeCollection;
      }
     
      public Collection<Livraison> getLivraisonCollection() {
        return livraisonCollection;
      }
     
      public void setLivraisonCollection(Collection<Livraison> livraisonCollection) {
        this.livraisonCollection = livraisonCollection;
      }
     
      @Override
      public int hashCode() {
        int hash = 0;
        hash += (codeiata != null ? codeiata.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 Client)) {
          return false;
        }
        Client other = (Client) object;
        if ((this.codeiata == null && other.codeiata != null) || (this.codeiata != null && !this.codeiata.equals(other.codeiata))) {
          return false;
        }
        return true;
      }
     
      @Override
      public String toString() {
        return "erp.entity.Client[codeiata=" + codeiata + "]";
      }
     
    }
    ClientFacade

    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
    @Stateless
    public class ClientFacade implements ClientFacadeLocal {
     
      @PersistenceContext
      private EntityManager em;
     
      public void create(Client client) {
        em.persist(client);
      }
     
      public void edit(Client client) {
        em.merge(client);
      }
     
      public void remove(Client client) {
        em.remove(em.merge(client));
      }
     
      public Client find(Object id) {
        return em.find(erp.entity.Client.class, id);
      }
     
      public List<Client> findAll() {
        return em.createQuery("select object(o) from Client as o").getResultList();
      }
     
      public Client dataByLabel(String label) {
        javax.persistence.Query data = em.createNamedQuery("Client.findByLabel").setParameter("label", label);
        return (Client) data.getSingleResult();
      }
    }
    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
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    @Entity
    @Table(name = "COMMANDE")
    @NamedQueries({@NamedQuery(name = "Commande.findByIdcommande", query = "SELECT c FROM Commande c WHERE c.idcommande = :idcommande"),
    @NamedQuery(name = "Commande.findNbAvionCmd", query = "SELECT c FROM Commande c WHERE c.codeiata = :codeiata"),
    @NamedQuery(name = "Commande.findByDatesignature", query = "SELECT c FROM Commande c WHERE c.datesignature = :datesignature"),
    @NamedQuery(name = "Commande.findByNbavion", query = "SELECT c FROM Commande c WHERE c.nbavion = :nbavion"),
     
    @NamedQuery(name = "Commande.nbAvionByCodeIata", query = "SELECT SUM(c.nbavion) FROM Commande c WHERE c.codeiata = :codeiata")
    })
    public class Commande implements Serializable {
     
      private static final long serialVersionUID = 1L;
      @Id
      @Column(name = "IDCOMMANDE", nullable = false)
      private Integer idcommande;
      @Column(name = "DATESIGNATURE")
      private String datesignature;
      @Column(name = "NBAVION")
      private Integer nbavion;
      @JoinColumn(name = "CODEIATA", referencedColumnName = "CODEIATA")
      @ManyToOne
      private Client codeiata;
      @JoinColumn(name = "IDMODELE", referencedColumnName = "IDMODELE")
      @ManyToOne
      private Modele idmodele;
     
      public Commande() {
      }
     
      public Commande(Integer idcommande) {
        this.idcommande = idcommande;
      }
     
      public Integer getIdcommande() {
        return idcommande;
      }
     
      public void setIdcommande(Integer idcommande) {
        this.idcommande = idcommande;
      }
     
      public String getDatesignature() {
        return datesignature;
      }
     
      public void setDatesignature(String datesignature) {
        this.datesignature = datesignature;
      }
     
      public Integer getNbavion() {
        return nbavion;
      }
     
      public void setNbavion(Integer nbavion) {
        this.nbavion = nbavion;
      }
     
      public Client getCodeiata() {
        return codeiata;
      }
     
      public void setCodeiata(Client codeiata) {
        this.codeiata = codeiata;
      }
     
      public Modele getIdmodele() {
        return idmodele;
      }
     
      public void setIdmodele(Modele idmodele) {
        this.idmodele = idmodele;
      }
     
      @Override
      public int hashCode() {
        int hash = 0;
        hash += (idcommande != null ? idcommande.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 Commande)) {
          return false;
        }
        Commande other = (Commande) object;
        if ((this.idcommande == null && other.idcommande != null) || (this.idcommande != null && !this.idcommande.equals(other.idcommande))) {
          return false;
        }
        return true;
      }
     
      @Override
      public String toString() {
        return "erp.entity.Commande[idcommande=" + idcommande + "]";
      }
    }
    CommandeFacade

    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
    @Stateless
    public class CommandeFacade implements CommandeFacadeLocal {
     
      @PersistenceContext
      private EntityManager em;
     
      public void create(Commande commande) {
        em.persist(commande);
      }
     
      public void edit(Commande commande) {
        em.merge(commande);
      }
     
      public void remove(Commande commande) {
        em.remove(em.merge(commande));
      }
     
      public Commande find(Object id) {
        return em.find(erp.entity.Commande.class, id);
      }
     
      public List<Commande> findAll() {
        return em.createQuery("select object(o) from Commande as o").getResultList();
      }
     
      public Integer nbAvionByCodeIata(Client codeiata) {
        javax.persistence.Query nb = em.createNamedQuery("Commande.nbAvionByCodeIata");
        nb.setParameter("codeiata", codeiata);
        return  (Integer) nb.getSingleResult();
      }
     
      public Commande nbAvionCmd(String codeiata) {
        Query cmd = em.createNamedQuery("Commande.findNbAvionCmd").setParameter("codeiata", codeiata);
        return (Commande)cmd.getSingleResult();
      }
    }
    Bean de session

    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
    @Stateless
    public class CompAerBean implements CompAerRemote {
     
      @EJB
      private CommandeFacadeLocal commandeFacade;
      @EJB
      private ClientFacadeLocal clientFacade;
     
      // Add business logic below. (Right-click in editor and choose
      // "EJB Methods > Add Business Method" or "Web Service > Add Operation")
      public void addComp(String codeiata, String label, String pays, String continent) {
     
        Client client = new Client();
     
        client.setCodeiata(codeiata);
     
        client.setLabel(label);
     
        client.setPays(pays);
     
        client.setContinent(continent);
     
        clientFacade.create(client);
      }
     
      public void donneeParam() {
        Client client = clientFacade.dataByLabel("airbus");
        System.out.println("Présenter les données d'une compagnie aérienne de label C" + "\n" +
                "label: " + client.getLabel() + "\n" +
                "pays: " + client.getPays() + "\n" +
                "Continent: " + client.getContinent());
      }
     
      public void nbAvion() {
        Client client = clientFacade.dataByLabel("airbus");
        Client client1 = new Client(client.getCodeiata(), client.getLabel(), client.getPays(), client.getContinent());
        Integer nb = commandeFacade.nbAvionByCodeIata(client1);
        System.out.println("Le nombre d'avion commandé par " + client.getLabel() + " est " + nb);
      /*Client client = clientFacade.dataByLabel("airbus");
      String codeiata = client.getCodeiata();
      //    Integer nb = commandeFacade.nbAvionByCodeIata(codeiata);
      Integer nb = commandeFacade.nbAvionByCodeIata("AB");
      System.out.println("nb" + nb);*/
     
      }
     
      public void afficheNbAvionCmd() {
        Client client = clientFacade.dataByLabel("airbus");
        Client client1 = new Client(client.getCodeiata(), client.getLabel(), client.getPays(), client.getContinent());
        Commande nb = commandeFacade.nbAvionCmd(client1.getCodeiata());
        System.out.println("Commande" + nb.getNbavion());
      }
    }

  2. #2
    Membre averti

    Profil pro
    Inscrit en
    Août 2005
    Messages
    39
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2005
    Messages : 39
    Par défaut
    SELECT c FROM Commande c WHERE c.codeiata = :codeiata ne peux pas fonctionner

    codeiata dans Commande est de type Client
    codeiata dans Client est de type String.

    en faisant c.codeiata=codeiata, tu compares un objet Client avec un objet String.

    donc

    SELECT c FROM Commande c WHERE c.codeiata.codeiata = :codeiata

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

Discussions similaires

  1. SQLite Release 3.6.19 : Gestion des clés étrangères
    Par Emmanuel Lecoester dans le forum SQLite
    Réponses: 22
    Dernier message: 19/11/2009, 15h06
  2. [EJB3 Entity] gestion des clés primaires avec EJB3 !
    Par magnum_cl9 dans le forum Java EE
    Réponses: 6
    Dernier message: 17/07/2009, 17h43
  3. Réponses: 3
    Dernier message: 29/06/2008, 18h56
  4. [MySQL] Insertions et gestion des clés étrangères
    Par b_e_n_n dans le forum PHP & Base de données
    Réponses: 3
    Dernier message: 15/06/2008, 16h14
  5. Gestion des clés étrangères
    Par Gonelle dans le forum HyperFileSQL
    Réponses: 1
    Dernier message: 06/07/2006, 10h48

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