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 :

problème de requête jpql


Sujet :

JPA Java

  1. #1
    Membre à l'essai
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2010
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Avril 2010
    Messages : 25
    Points : 11
    Points
    11
    Par défaut problème de requête jpql
    Bonjour à tous, je suis en train de développer une application de CRUD avec JPA hybernate. J'ai une erreur sur une de mes requête en particulier.
    1ere question comment peut on tester les requêtes jpql ?
    2eme question, voila la requête qui pose probleme. Si jamais quelqu'un voit l'erreur je suis preneur.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
      public PointDePassage getByPosition(Long idPosition) throws Exception {
            Query query = em.createQuery("SELECT pdp FROM PointDePassage pdp WHERE pdp.position_id LIKE :"+idPosition);
            //query.setParameter("position_id", "%" + idPosition + "%");
            PointDePassage pdp = (PointDePassage) query.getSingleResult();
            return pdp;
        }
    Pour préciser, l'entity PointDePassage possede un objet Position. La table PointDePassage possède un champ position_id pour relier les 2 entitys.
    J'utilise netbeans 6.5.

  2. #2
    Membre du Club
    Inscrit en
    Mai 2009
    Messages
    76
    Détails du profil
    Informations forums :
    Inscription : Mai 2009
    Messages : 76
    Points : 49
    Points
    49
    Par défaut
    Tu as demandé a Wawa? ^^

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    50
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 50
    Points : 48
    Points
    48
    Par défaut
    Les ":" après ton like

    Si tu cherches des exemples sur le net :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    String ejbql = "SELECT i from Item i WHERE i.name LIKE :pattern ESCAPE
    :esc";
    Query query = em.createQuery(ejbql);
    query.setParameter("pattern", "\\_%");
    Les ":" signifient que c'est un paramètre.

    Du coup, ton code se transforme en :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    public PointDePassage getByPosition(Long idPosition) throws Exception {
    Query query = em.createQuery("SELECT pdp FROM PointDePassage pdp WHERE pdp.position_id LIKE :idPosition");
    query.setParameter("idPosition", "%" + idPosition + "%");
    PointDePassage pdp = (PointDePassage) query.getSingleResult();
    return pdp;
    }
    EDIT : Ceci dit, un Like sur un nombre...

  4. #4
    Membre à l'essai
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2010
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Avril 2010
    Messages : 25
    Points : 11
    Points
    11
    Par défaut
    Voila l'erreur générée par le code que tu m'a donné.

    java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: position_id of: entreprise.PointDePassage [SELECT pdp FROM entreprise.PointDePassage pdp WHERE pdp.position_id LIKE :idPosition]

  5. #5
    Membre à l'essai
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2010
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Avril 2010
    Messages : 25
    Points : 11
    Points
    11
    Par défaut
    Et voila l'erreur généré par mon code sans ta modification.

    java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: expecting IDENT, found '1' near line 1, column 75 [SELECT pdp FROM entreprise.PointDePassage pdp WHERE pdp.position_id LIKE :1]

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    50
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 50
    Points : 48
    Points
    48
    Par défaut
    Oups en effet...

    Dans la ligne "SELECT pdp FROM PointDePassage pdp WHERE pdp.position_id"

    Le nom du champ position_id doit être celui du champ coté Java et non côté SQL.
    Je supposerai donc que ça doit être positionId.

    Soit :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    public PointDePassage getByPosition(Long idPosition) throws Exception {
    Query query = em.createQuery("SELECT pdp FROM PointDePassage pdp WHERE pdp.positionId LIKE :idPosition");
    query.setParameter("idPosition", "%" + idPosition + "%");
    PointDePassage pdp = (PointDePassage) query.getSingleResult();
    return pdp;
    }

  7. #7
    Membre à l'essai
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2010
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Avril 2010
    Messages : 25
    Points : 11
    Points
    11
    Par défaut
    comment faire en sorte que idPosition soit pris en compte comme une variable dans la requete?
    En sql il faut mettre +nomVarible; par exemple.

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    50
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 50
    Points : 48
    Points
    48
    Par défaut
    Je ne comprends pas ta question.

  9. #9
    Membre à l'essai
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2010
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Avril 2010
    Messages : 25
    Points : 11
    Points
    11
    Par défaut
    pour insérer une variable dans une requete Sql on fait :
    "requettteeeeee"+variable+"requeettte";

    comment faut il faire en jpql.
    Je sais pas si c'est trés claire je vois pas comment le dire autrement.

  10. #10
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    50
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 50
    Points : 48
    Points
    48
    Par défaut
    Ok, je vois ce que tu veux dire!

    C'est justement ce que je t'explique plus haut!!

    Alors reprenons. Tu as ta requête :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Query query = em.createQuery("SELECT pdp FROM PointDePassage pdp WHERE pdp.positionId LIKE :idPosition");
    Le fait de mettre les ":" avant idPosition signifie justement que c'est un paramètre qui va être passé!

    Ensuite, tu vas justement setter ton paramètre dans la requête, grâce à la ligne suivante :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    query.setParameter("idPosition", "%" + idPosition + "%");
    En gros, elle dit : "Là où tu trouveras le paramètre idPosition, tu mettras la valeurs "%" + idPosition + "%" "

    Est-ce plus clair?

  11. #11
    Membre à l'essai
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2010
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Avril 2010
    Messages : 25
    Points : 11
    Points
    11
    Par défaut
    Ok, c'est maintenant claire. Merci.
    Par contre l'erreur persiste
    java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: positionId of: entreprise.PointDePassage [SELECT pdp FROM entreprise.PointDePassage
    Je vais continuer à chercher...

  12. #12
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    50
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 50
    Points : 48
    Points
    48
    Par défaut
    Ok, après tout dépend de comment tu as fait ton mapping.
    Pourrais-tu mettre ici ta classe PointDePassage?

  13. #13
    Membre à l'essai
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2010
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Avril 2010
    Messages : 25
    Points : 11
    Points
    11
    Par défaut
    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
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package entreprise;
     
    import java.io.Serializable;
    import javax.persistence.DiscriminatorColumn;
    import javax.persistence.DiscriminatorType;
    import javax.persistence.DiscriminatorValue;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.OneToOne;
     
    /**
     *
     * @author nicolas
     */
    @Entity
    @DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING, length = 1)
    @DiscriminatorValue("P")
    public class PointDePassage implements Serializable {
     
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
        private String nomRue;
        private int numeroRue;
        private String nom;
        @OneToOne
        protected  Position position;
     
        public PointDePassage() {
        }
     
        public PointDePassage(String nomRue, int numeroRue, Position position, String nom) {
            this.nomRue = nomRue;
            this.numeroRue = numeroRue;        
            this.position = position;
            this.nom = nom;
        }
     
        public Long getId() {
            return id;
        }
     
        public void setId(Long id) {
            this.id = id;
        }
     
        @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 PointDePassage)) {
                return false;
            }
            PointDePassage other = (PointDePassage) 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 "rue " + this.getNomRue() + " numero " + this.getNumeroRue();
        }
     
        /**
         * @return the nomRue
         */
        public String getNomRue() {
            return nomRue;
        }
     
        /**
         * @param nomRue the nomRue to set
         */
        public void setNomRue(String nomRue) {
            this.nomRue = nomRue;
        }
     
        /**
         * @return the numeroRue
         */
        public int getNumeroRue() {
            return numeroRue;
        }
     
        /**
         * @param numeroRue the numeroRue to set
         */
        public void setNumeroRue(int numeroRue) {
            this.numeroRue = numeroRue;
        }
     
        /**
         * @return the position
         */
        public Position getPosition() {
            return position;
        }
     
        /**
         * @param position the position to set
         */
        public void setPosition(Position position) {
            this.position = position;
        }
     
        /**
         * @return the nom
         */
        public String getNom() {
            return nom;
        }
     
        /**
         * @param nom the nom to set
         */
        public void setNom(String nom) {
            this.nom = nom;
        }
    }

  14. #14
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    50
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 50
    Points : 48
    Points
    48
    Par défaut
    Et la classe Position aussi

  15. #15
    Membre à l'essai
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2010
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Avril 2010
    Messages : 25
    Points : 11
    Points
    11
    Par défaut
    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
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package entreprise;
     
    import java.io.Serializable;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
     
    /**
     *
     * @author nicolas
     */
    @Entity
    public class Position implements Serializable {
     
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
        private float latitude;
        private float longitude;
        private char ptCardiLati;
        private char ptCardiLongi;
     
        public Position(){
     
    }
    public Position(float latitude, float longitude, char ptCardiLati, char ptCardiLongi){
       // this.id=id;
        this.latitude=latitude;
        this.longitude=longitude;
        this.ptCardiLati=ptCardiLati;
        this.ptCardiLongi=ptCardiLongi;
    }
     
        public Long getId() {
            return id;
        }
     
        public void setId(Long id) {
            this.id = id;
        }
     
        @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 Position)) {
                return false;
            }
            Position other = (Position) 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 String.valueOf(latitude) + ptCardiLati + String.valueOf(longitude) + getPtCardiLongi();
        }
     
        /**
         * @return the latitude
         */
        public float getLatitude() {
            return latitude;
        }
     
        /**
         * @param latitude the latitude to set
         */
        public void setLatitude(float latitude) {
            this.latitude = latitude;
        }
     
        /**
         * @return the longitude
         */
        public float getLongitude() {
            return longitude;
        }
     
        /**
         * @param longitude the longitude to set
         */
        public void setLongitude(float longitude) {
            this.longitude = longitude;
        }
     
        /**
         * @return the ptCardiLati
         */
        public char getPtCardiLati() {
            return ptCardiLati;
        }
     
        /**
         * @param ptCardiLati the ptCardiLati to set
         */
        public void setPtCardiLati(char ptCardiLati) {
            this.ptCardiLati = ptCardiLati;
        }
     
        /**
         * @return the ptCardiLongi
         */
        public char getPtCardiLongi() {
            return ptCardiLongi;
        }
     
        /**
         * @param ptCardiLongi the ptCardiLongi to set
         */
        public void setPtCardiLongi(char ptCardiLongi) {
            this.ptCardiLongi = ptCardiLongi;
        }
    }

  16. #16
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    50
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 50
    Points : 48
    Points
    48
    Par défaut
    Alors, là je pense que ça devrait être bon...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Query query = em.createQuery("SELECT pdp FROM PointDePassage pdp WHERE pdp.position.id LIKE :idPosition");

  17. #17
    Membre à l'essai
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2010
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Avril 2010
    Messages : 25
    Points : 11
    Points
    11
    Par défaut
    Ah il y a du changement. L'erreur est maintenant sur la ligne
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
       PointDePassage pdp = (PointDePassage) query.getSingleResult();
    elle n'est plus sur la ligne de la requête.

    Voila l'erreur générée:
    java.lang.IllegalArgumentException: org.hibernate.QueryException: Not all named parameters have been set: [idPosition] [SELECT pdp FROM PointDePassage pdp WHERE pdp.position.id LIKE :idPosition]

  18. #18
    Membre du Club
    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    50
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 50
    Points : 48
    Points
    48
    Par défaut
    Tu fais bien le :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    query.setParameter("idPosition", "%" + idPosition + "%");
    Sinon, y a un truc qui me turlupine, pourquoi fais-tu un "like" sur un id et non un "=" ??

  19. #19
    Membre à l'essai
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2010
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Avril 2010
    Messages : 25
    Points : 11
    Points
    11
    Par défaut
    Un nouveau problème viens d'apparaître. Mon AddStation() ne fonctionne pas alors que le addPointDePassage() fonctionne.
    Station hérite de pointDePassage.
    Voici la classe Station :
    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
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package entreprise;
     
    import java.io.Serializable;
    import javax.persistence.DiscriminatorValue;
    import javax.persistence.Entity;
     
    /**
     *
     * @author nicolas
     */
    @Entity
    @DiscriminatorValue("S")
    public class Station extends PointDePassage implements Serializable {
     
     
        public Station() {
        }
     
        public Station(String nomRue, int numeroRue, Position position, String nom) {
            super(nomRue, numeroRue, position, nom);
        }
    }

  20. #20
    Membre à l'essai
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2010
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Avril 2010
    Messages : 25
    Points : 11
    Points
    11
    Par défaut
    je met un = c'est vrai que le like n'a pas d'intérêt sur un id.

Discussions similaires

  1. Problème requête JPQL
    Par btssouma dans le forum JPA
    Réponses: 8
    Dernier message: 11/10/2016, 13h29
  2. Problème requête JPQL
    Par l_informaticien dans le forum JPA
    Réponses: 0
    Dernier message: 02/02/2013, 15h15
  3. Problème de requètes concurentes
    Par Emmanuel.G dans le forum XMLRAD
    Réponses: 3
    Dernier message: 08/08/2003, 16h51
  4. Réponses: 2
    Dernier message: 16/07/2003, 14h40
  5. Problème dans requête avec count()
    Par BadFox dans le forum Requêtes
    Réponses: 3
    Dernier message: 08/07/2003, 18h02

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