Bonjour,
J'ai un problème sur les alias dans un createNativeQuery.
Ca fait 5 jours que je suis la dessus...
Je m'explique:
J'ai utilisé createNativeQuery car je pense pas que ça soit possible de faire cette requête avec hql (je peux me tromper ) a cause de la partie en rouge.
Mais comme il y a plusieurs champs qui ont le même nom ("libelle", "mnemo"), et bien il écrit plusieurs fois la même chose.
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 sqlTxt = "select ticket.Id, ticket.sujet, ticket.delaiReel, ticket.remarqueIntervenant, " + "categorie.Libelle, type.Libelle, dem.mnemo, inter.mnemo, " + "demande.estUrgent, demande.delaiSouhaite, demande.delaiConfirme, demande.duree, " + "statutticket.Libelle, historique_statutticket.dateStatut " + "FROM ticket INNER JOIN " + "historique_statutticket ON ticket.Id = historique_statutticket.Ticket_Id INNER JOIN " + "statutticket ON historique_statutticket.StatutTicket_Id = statutticket.Id INNER JOIN " + "type ON ticket.type_id = type.id INNER JOIN " + "categorie ON ticket.categorie_id = categorie.id INNER JOIN " + "utilisateur dem ON ticket.demandeur = dem.id INNER JOIN " + "utilisateur inter ON ticket.intervenant = inter.id INNER JOIN " + "demande ON ticket.id = demande.ticket_id INNER JOIN " + " (SELECT historique_statutticket.Ticket_Id, Max(historique_statutticket.DateStatut) AS MaxDeDateStatut " + " FROM historique_statutticket " + " GROUP BY historique_statutticket.Ticket_Id " + " ORDER BY historique_statutticket.Ticket_Id) as RDateMaxStatut " + "ON (historique_statutticket.DateStatut = RDateMaxStatut.MaxDeDateStatut) AND (historique_statutticket.Ticket_Id = RDateMaxStatut.Ticket_Id) "; for (Object ti : em.createNativeQuery(sqlTxt).getResultList()) { Object tab[] = (Object[]) ti; System.out.println(tab[0] + " " + tab[1] + " " + tab[2] + " " + tab[3] + " " + tab[4] + " " + tab[5] + " " + tab[6] + " " + tab[7] + " " + tab[8] + " " + tab[9] + " " + tab[10] + " " + tab[11] + " " + tab[12] + " " + tab[13]); }
Alors je me suis dis qu'il manquait les alias, mais quand je mets les alias (en rouge):
Et bien le serveur m'envoi l'erreur :
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 sqlTxt = "select ticket.Id, ticket.sujet, ticket.delaiReel, ticket.remarqueIntervenant, " + "categorie.Libelle as catlibelle, type.Libelle as typelibelle, dem.mnemo as demmnemo, inter.mnemo as intmnemo, " + "demande.estUrgent, demande.delaiSouhaite, demande.delaiConfirme, demande.duree, " + "statutticket.Libelle as statutlibelle, historique_statutticket.dateStatut " + "FROM ticket INNER JOIN " + "historique_statutticket ON ticket.Id = historique_statutticket.Ticket_Id INNER JOIN " + "statutticket ON historique_statutticket.StatutTicket_Id = statutticket.Id INNER JOIN " + "type ON ticket.type_id = type.id INNER JOIN " + "categorie ON ticket.categorie_id = categorie.id INNER JOIN " + "utilisateur dem ON ticket.demandeur = dem.id INNER JOIN " + "utilisateur inter ON ticket.intervenant = inter.id INNER JOIN " + "demande ON ticket.id = demande.ticket_id INNER JOIN " + " (SELECT historique_statutticket.Ticket_Id, Max(historique_statutticket.DateStatut) AS MaxDeDateStatut " + " FROM historique_statutticket " + " GROUP BY historique_statutticket.Ticket_Id " + " ORDER BY historique_statutticket.Ticket_Id) as RDateMaxStatut " + "ON (historique_statutticket.DateStatut = RDateMaxStatut.MaxDeDateStatut) AND (historique_statutticket.Ticket_Id = RDateMaxStatut.Ticket_Id) ";
En fait il trouve plus les champs où il y a les alias.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 could not read column value from result set: Libelle; Column 'Libelle' not found. SQL Error: 0, SQLState: S0022 Column 'Libelle' not found.
Sur un site en anglais j'ai trouvé qu'on pouvait rajouter ça:
Mais ça fonctionne pas non plus.....
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 @SqlResultSetMappings({ @SqlResultSetMapping(name = "tickets", columns = { @ColumnResult(name = "demmnemo"), @ColumnResult(name = "intmnemo"), @ColumnResult(name = "typelibelle"), @ColumnResult(name = "catlibelle"), @ColumnResult(name = "statutlibelle") })})
enfin bref, j en ai marre, si quelqu'un pouvait m'aider ça me sauverait vraiment...
Merci d'avance
Partager