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 :

Hibernate HQL query


Sujet :

Hibernate Java

  1. #1
    Membre du Club
    Inscrit en
    Septembre 2007
    Messages
    89
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 89
    Points : 61
    Points
    61
    Par défaut Hibernate HQL query
    Bonjour,
    je tente de selectionner les enregistrements d'une table d'association en passant en paramettre un objet commande. mais le resultat obtenu est différent de null et quand je test le size() il me renvoit 0.
    est ce quelq'un pourrait m'aider. Voic la requette que je lance.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    public List<LigneCommandeClient> getLigneCommandeClientBycmde(CommandeClient cmdeclient) {
    		Session session = getSession();
    		List<LigneCommandeClient> lignecmdeclient=(List<LigneCommandeClient>)session.createQuery("FROM LigneCommandeClient WHERE cmdeclient=:param")
    		.setParameter("param", cmdeclient).list();
    		session.flush();
    		return lignecmdeclient;
    	}

  2. #2
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 313
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 313
    Points : 9 529
    Points
    9 529
    Billets dans le blog
    1
    Par défaut
    Tu n'as probablement aucun enregistrement qui réponde au critère de sélection.
    Le fait que la liste soit vide et pas NULL est normal, la requête est valide mais ne renvoie rien (donc liste vide).

  3. #3
    Membre du Club
    Inscrit en
    Septembre 2007
    Messages
    89
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 89
    Points : 61
    Points
    61
    Par défaut
    Tu n'as probablement aucun enregistrement qui réponde au critère de sélection.
    Le fait que la liste soit vide et pas NULL est normal, la requête est valide mais ne renvoie rien (donc liste vide).
    Bonjour je vous remercie de votre intervention. Mais quand je regarde la base de donnée j'ai exactement un enregistrement.
    parce que quand j'ai effectué une commande j'ai enregistré la ligne de commande.

  4. #4
    Membre du Club
    Inscrit en
    Septembre 2007
    Messages
    89
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 89
    Points : 61
    Points
    61
    Par défaut
    Je suis toujour preneur pour les idées.

  5. #5
    Membre régulier Avatar de spoklo
    Profil pro
    Inscrit en
    Octobre 2008
    Messages
    67
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2008
    Messages : 67
    Points : 76
    Points
    76
    Par défaut
    Bonjour
    Il manque le SELECT dans la requête. (on a pas besoin, ).

  6. #6
    Inactif  
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    2 189
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2006
    Messages : 2 189
    Points : 2 336
    Points
    2 336
    Par défaut
    pas besoin du select s'il utilise createQuery seulement s il utilise createSqlQuery

  7. #7
    Membre du Club
    Inscrit en
    Septembre 2007
    Messages
    89
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 89
    Points : 61
    Points
    61
    Par défaut
    Oui j'ai pas besoin du select en fait j'ais pas d'erreur mais je commprends pas pourquoi j'ai pas les enregistrement alors que si je cherche physiquement dasn la base je trouve mes enregistrement.

  8. #8
    Membre du Club
    Inscrit en
    Septembre 2007
    Messages
    89
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 89
    Points : 61
    Points
    61
    Par défaut
    Comment hibernate gere les cash. Est qu'il a cherche cherché réellement dans la base

  9. #9
    Inactif  
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    2 189
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2006
    Messages : 2 189
    Points : 2 336
    Points
    2 336
    Par défaut
    essaye un session.close

  10. #10
    Modérateur
    Avatar de OButterlin
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    7 313
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 7 313
    Points : 9 529
    Points
    9 529
    Billets dans le blog
    1
    Par défaut
    Peux-tu montrer le mapping de la table ?
    De quel type est "param" pour ta requête ?

  11. #11
    Membre du Club
    Inscrit en
    Septembre 2007
    Messages
    89
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 89
    Points : 61
    Points
    61
    Par défaut
    voici le mapping dela table
    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
     
    @Entity
    public class LigneCommandeClient {
    	@Embeddable
    	public static class Id implements Serializable{
     
    		private static final long serialVersionUID = 1L;
    		@Column(name="product_id")
    		private Long productId;
    		@Column(name="cmde_id")
    		private Long cmdeId;
     
    		public Long getCmdeId() {
    			return cmdeId;
    		}
    		public void setCmdeId(Long cmdeId) {
    			this.cmdeId = cmdeId;
    		}
    		public Long getProductId() {
    			return productId;
    		}
    		public void setProductId(Long productId) {
    			this.productId = productId;
    		}
    	public Id(){}
    		public Id(Long productId,Long cmdeId){
    			this.productId=productId;
    			this.cmdeId=cmdeId;
    		}
    	public boolean equals(Object o){
    		if(o!=null && o instanceof Id){
    			Id that=(Id)o;
    			return this.productId.equals(that.productId)&& this.cmdeId.equals(that.cmdeId);
    		}
    		else{
    			return false;
    		}
    	}
    	public int hashCode(){
    		return productId.hashCode()+cmdeId.hashCode();
    	}
    	}
     
    	private Id id=new Id();
    	private Produit product;
    	private CommandeClient cmdeclient;
    	private long quantite;
    	public LigneCommandeClient(){}
    	public LigneCommandeClient(long quantite,Produit product,CommandeClient cmdeclient){
    		//set Fields
    		this.quantite=quantite;
    		this.product=product;
    		this.cmdeclient=cmdeclient;
    		//set identifier values
    		this.id.setCmdeId(cmdeclient.getId());
    		this.id.setProductId(product.getId());
    		//Guarentee referential integrity
    //		product.getLigneCmdeClients().add(this);
    //		cmdeclient.getLigneCmdeClients().add(this);
    	}
    	@ManyToOne
    	@JoinColumn(name="id",insertable=false,updatable=false)
    	public CommandeClient getCmdeclient() {
    		return cmdeclient;
    	}
    	public void setCmdeclient(CommandeClient cmdeclient) {
    		this.cmdeclient = cmdeclient;
    	}
    	@ManyToOne
    	@JoinColumn(name="id",insertable=false,updatable=false)
    	public Produit getProduct() {
    		return product;
    	}
    	public void setProduct(Produit product) {
    		this.product = product;
    	}
    	public long getQuantite() {
    		return quantite;
    	}
    	public void setQuantite(long quantite) {
    		this.quantite = quantite;
    	}
    	@EmbeddedId
    	public Id getId() {
    		return id;
    	}
    	public void setId(Id id) {
    		this.id = id;
    	}
    	@Override
    	public int hashCode() {
    		final int prime = 31;
    		int result = 1;
    		result = prime * result
    				+ ((cmdeclient == null) ? 0 : cmdeclient.hashCode());
    		result = prime * result + ((id == null) ? 0 : id.hashCode());
    		result = prime * result + ((product == null) ? 0 : product.hashCode());
    		result = prime * result + (int) (quantite ^ (quantite >>> 32));
    		return result;
    	}
    	@Override
    	public boolean equals(Object obj) {
    		if (this == obj)
    			return true;
    		if (obj == null)
    			return false;
    		if (getClass() != obj.getClass())
    			return false;
    		final LigneCommandeClient other = (LigneCommandeClient) obj;
    		if (cmdeclient == null) {
    			if (other.cmdeclient != null)
    				return false;
    		} else if (!cmdeclient.equals(other.cmdeclient))
    			return false;
    		if (id == null) {
    			if (other.id != null)
    				return false;
    		} else if (!id.equals(other.id))
    			return false;
    		if (product == null) {
    			if (other.product != null)
    				return false;
    		} else if (!product.equals(other.product))
    			return false;
    		if (quantite != other.quantite)
    			return false;
    		return true;
    	}
     
     
     
    }

  12. #12
    Membre du Club
    Inscrit en
    Septembre 2007
    Messages
    89
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 89
    Points : 61
    Points
    61
    Par défaut
    param c'est un objet c'est: cmdeclient. c'est bien mentionner ds la requette.

  13. #13
    Inactif  
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    2 189
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2006
    Messages : 2 189
    Points : 2 336
    Points
    2 336
    Par défaut
    D'après ce que je vois private CommandeClient cmdeclient est un objet essaye peut être de testé son id.


    "FROM LigneCommandeClient WHERE cmdeclient.id=:param

  14. #14
    Membre du Club
    Inscrit en
    Septembre 2007
    Messages
    89
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 89
    Points : 61
    Points
    61
    Par défaut
    J'ai déjà essayé mais c'est la meme chose

  15. #15
    Inactif  
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    2 189
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2006
    Messages : 2 189
    Points : 2 336
    Points
    2 336
    Par défaut
    tu as testé

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    "FROM LigneCommandeClient WHERE cmdeclient.id=:param "
    .setParameter("param", cmdeclien.getId())
    ?

  16. #16
    Membre du Club
    Inscrit en
    Septembre 2007
    Messages
    89
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 89
    Points : 61
    Points
    61
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
     
    "FROM LigneCommandeClient WHERE cmdeclient.id=:param "
    .setParameter("param", cmdeclien.getId())
    oui mais meme chose bon est c'est pas un problème lazy initialisation. En fait je ne comprends pas bien ce terme mais on dirrait que il n'a pas lier les objets

  17. #17
    Inactif  
    Profil pro
    Inscrit en
    Mai 2006
    Messages
    2 189
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : Suisse

    Informations forums :
    Inscription : Mai 2006
    Messages : 2 189
    Points : 2 336
    Points
    2 336
    Par défaut
    la lazy initialisation veut simplement dire que tes objets ne sont pas loadé lors du chargement de l'objet mais lors d'un appel explicite (accès aux propriétés) aux objets mis en relation

  18. #18
    Membre du Club
    Inscrit en
    Septembre 2007
    Messages
    89
    Détails du profil
    Informations forums :
    Inscription : Septembre 2007
    Messages : 89
    Points : 61
    Points
    61
    Par défaut
    Problème résolu. en fait dans l'entité Lignedecommde j'avais pas mis la bonne annotation.

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

Discussions similaires

  1. HQL query et SQL query
    Par *alexandre* dans le forum Hibernate
    Réponses: 3
    Dernier message: 20/09/2006, 21h05
  2. [HIBERNATE] HQL pbs
    Par _juel_ dans le forum Hibernate
    Réponses: 1
    Dernier message: 09/06/2006, 14h33
  3. org.hibernate.hql.ast.QuerySyntaxError: unexpected token
    Par oughlad dans le forum Hibernate
    Réponses: 9
    Dernier message: 26/05/2006, 14h20
  4. [hibernate] HQL concat
    Par Fr@ncky dans le forum Hibernate
    Réponses: 1
    Dernier message: 10/02/2006, 09h07
  5. [hibernate HQL] Sélection de l'année d'une date
    Par Fr@ncky dans le forum Hibernate
    Réponses: 2
    Dernier message: 24/01/2006, 12h53

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