Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1536 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  [hibernate][RESOLU]insertion, update OK, lecture avec requete HQL KO

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[hibernate][RESOLU]insertion, update OK, lecture avec requete HQL KO

n°1749785
weed
Posté le 22-06-2008 à 12:09:34  profilanswer
 

Bonjour,
 
lorsque j'essaie d'inserer des données :
 

Code :
  1. public static void main(String[] args) throws HibernateException {
  2.  Session session = HibernateUtil.currentSession();
  3.  //Transaction tx = session.beginTransaction();
  4.  //Insertion  
  5.  User u = new User();
  6.  u.setNom("che" );
  7.  u.setPrenom("Sylvie" );
  8.  session.save(u);
  9.  tx.commit();
  10.  HibernateUtil.closeSession();
  11. }
  12. }


 
ou je fais une mise à jour :
 

Code :
  1. public static void main(String[] args) throws HibernateException {
  2.  Session session = HibernateUtil.currentSession();
  3.  Transaction tx = session.beginTransaction();
  4.  User u = (User) session.load(User.class, new Integer(1));
  5.  u.setPrenom("Jacques" );
  6.  session.save(u);
  7.  tx.commit();
  8.  HibernateUtil.closeSession();
  9. }
  10. }


 
ca passe nickel. Mais quand j'essaie de lire le contenu de ma table user (que j'ai renommé en tuser par la suite car je me suis dit que peut etre que c'est un mot reservé).
 
 
 

Code :
  1. Query query = session.createQuery("from tuser" );
  2.  List list = query.list();
  3.  //List list = session.find("from user" );
  4.  Iterator it = list.iterator();
  5.  while (it.hasNext()) {
  6.   User u = (User)it.next();
  7.   System.out.print(u.getPrenom());
  8.  }


 
 
j'ai a chaque fois une erreur com.mysql.jdbc.exceptions.MySQLSyntaxErrorException qui me dit que me requete n'est HQL n'est pas correct. Je ne comprends pas, comment peux t-on faire plus simple qu'un : from Matable
 
Ou me suis je trompé ?
 

Citation :

INFO: building session factory
22 juin 2008 11:29:12 net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured
22 juin 2008 11:29:12 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
ATTENTION: SQL Error: 1064, SQLState: 42000
22 juin 2008 11:29:12 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
GRAVE: Erreur de syntaxe près de 'from' à la ligne 1
22 juin 2008 11:29:12 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
ATTENTION: SQL Error: 1064, SQLState: 42000
22 juin 2008 11:29:12 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
GRAVE: Erreur de syntaxe près de 'from' à la ligne 1
22 juin 2008 11:29:12 net.sf.hibernate.JDBCException <init>
GRAVE: Could not execute query
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Erreur de syntaxe près de 'from' à la ligne 1
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
 at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
 at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
 at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
 at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1467)
 at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:87)
 at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:875)
 at net.sf.hibernate.loader.Loader.doQuery(Loader.java:269)
 at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
 at net.sf.hibernate.loader.Loader.doList(Loader.java:1033)
 at net.sf.hibernate.loader.Loader.list(Loader.java:1024)
 at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
 at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1544)
 at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
 at Test.main(Test.java:28)
Exception in thread "main" net.sf.hibernate.JDBCException: Could not execute query
 at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1547)
 at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
 at Test.main(Test.java:28)
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Erreur de syntaxe près de 'from' à la ligne 1
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
 at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
 at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
 at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
 at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1467)
 at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:87)
 at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:875)
 at net.sf.hibernate.loader.Loader.doQuery(Loader.java:269)
 at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
 at net.sf.hibernate.loader.Loader.doList(Loader.java:1033)
 at net.sf.hibernate.loader.Loader.list(Loader.java:1024)
 at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
 at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1544)
 ... 2 more


 
PS : je me suis inspiré du tuto : http://defaut.developpez.com/tutor [...] hibernate/


Message édité par weed le 22-06-2008 à 15:11:33
mood
Publicité
Posté le 22-06-2008 à 12:09:34  profilanswer
 

n°1749832
weed
Posté le 22-06-2008 à 15:10:25  profilanswer
 

erreur de ma part, on m'a dit qu'il ne faut pas mettre le nom de la table dans la requete HQL mais la classe.
 
La solution est donc pour ma part : "from User"


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  [hibernate][RESOLU]insertion, update OK, lecture avec requete HQL KO

 

Sujets relatifs
requete flux xml avec curl[resolu] probleme eregi()
Récupérer la valeur de IDENTITY avant insertion[Résolu] Requete Access
[RESOLU] Pb pour passer des arguments de ligne de commande(finalement pas résolu) Fonction de listage
[SQL] Requete possible ?[Resolu] Lancer un script PHP en mode invisible...
problème exécution de requête 
Plus de sujets relatifs à : [hibernate][RESOLU]insertion, update OK, lecture avec requete HQL KO


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR