[Hibernate SQL] Rownum et Order by

Rownum et Order by [Hibernate SQL] - SQL/NoSQL - Programmation

Marsh Posté le 06-07-2007 à 16:46:06    

Bonjour,
 
j'essaye de recuperer les x premiers lignes d'une table.
 
La requete devrait donc être du style :  

Code :
  1. from ( select * from com.neuf.mvno.persistence.entites.CommandeMarc as cmd where cmd.statut = :statut and cmd.ligneTelephone.idInterneTelephone=:idLigneTelephone order by cmd.id DESC ) where rownum <= 1


 
Mais j'obtiens l'exception suivante à l'execution :
 
 

Code :
  1. org.springframework.orm.hibernate.HibernateQueryException: in expected: select [from ( select * com.neuf.mvno.persistence.entites.CommandeMarc as cmd where cmd.statut = :statut and cmd.ligneTelephone.idInterneTelephone=:idLigneTelephone order by cmd.id DESC ) where rownum <= 1]; nested exception is net.sf.hibernate.QueryException: in expected: select [from ( select * com.neuf.mvno.persistence.entites.CommandeMarc as cmd where cmd.statut = :statut and cmd.ligneTelephone.idInterneTelephone=:idLigneTelephone order by cmd.id DESC ) where rownum <= 1]
  2. net.sf.hibernate.QueryException: in expected: select [from ( select * com.neuf.mvno.persistence.entites.CommandeMarc as cmd where cmd.statut = :statut and cmd.ligneTelephone.idInterneTelephone=:idLigneTelephone order by cmd.id DESC ) where rownum <= 1]
  3. at net.sf.hibernate.hql.FromParser.token(FromParser.java:102)
  4. at net.sf.hibernate.hql.ClauseParser.token(ClauseParser.java:87)
  5. at net.sf.hibernate.hql.PreprocessingParser.token(PreprocessingParser.java:123)
  6. at net.sf.hibernate.hql.ParserHelper.parse(ParserHelper.java:29)
  7. at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:149)
  8. at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:138)
  9. at net.sf.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:295)
  10. at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1572)
  11. at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1543)
  12. at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:49)


 
Je suis donc venu a testé la requete undirected sous SQuirreL et ca marche bien.
 
Ensuite j'ai  voulu testé la requete suivante :

Code :
  1. from com.neuf.mvno.persistence.entites.CommandeMarc as cmd where cmd.statut = :statut and cmd.ligneTelephone.idInterneTelephone=:idLigneTelephone and rownum <= 1 order by cmd.id DESC


 
Et à ma grande surprise, elle marche !!!!
J'ai appronfondi mes tests sous SQuirreL et conclusion : le rownum est valorisé APRES le group by !!!!
Y'a t-il eu une evolution ?? je suis sous oracle  9.2.0.6.0.
 

Code :
  1. select ROWNUM, id from Commande_Marc cmd where rownum <= 6 order by id DESC
  2. ===>
  3. 1 106
  4. 2 104
  5. 3 103
  6. 4 102
  7. 5 101
  8. 6 93


 

Code :
  1. select ROWNUM, id from Commande_Marc cmd where rownum <= 6
  2. ===>
  3. 1 2
  4. 2 3
  5. 3 4
  6. 4 5
  7. 5 6
  8. 6 7

Reply

Marsh Posté le 06-07-2007 à 16:46:06   

Reply

Marsh Posté le 07-07-2007 à 11:58:07    

si c'est pour faire de l'hibernate et pondre toi même les query, il n'y a alors aucun interet d'utiliser un OR mapping comme hibernate.
 
De plus, si tu laisse faire hibernate, il va te faire cela comme un grand.
Il existe par ailleurs une options dans hibernate pour ne récupérer que les x premières lignes.

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed