problème d'exécution des requêtes

problème d'exécution des requêtes - Java - Programmation

Marsh Posté le 06-12-2009 à 10:27:42    

Bonjour,
 
Je voulais faire des requêtes de manipulation d'une base des donnée sous Mysql en utilisant le framework de mappage Hibernate.
Il y a quelques requêtes qui ne s'exécutent pas.
Voici ces requêtes:

Code :
  1. List employe = session.createQuery("from Employe p where p.nom=?","ali", Hibernate.STRING).list();
  2.    
  3.      for (int i = 0; i < employe.size(); i++) {
  4. Employe eps = (Employe) employe.get(i);
  5. System.out.println("nom = " +  eps.getNom());


 
 

Code :
  1. Query query = session.createQuery("from Employe p where p.nom = :bassem" );
  2.      query.setString("bassem", "can" );
  3.       Iterator It = query.iterate();
  4.      while (It.hasNext()) {
  5.         Employe per = (Employe) It.next();
  6.         System.out.println("nom = " + per.getNom());
  7.       }


 
 

Code :
  1. int compteur = ( (Integer) session.createQuery("select count(*) from Employe" ).iterate().next() ).intValue();
  2.         System.out.println("compteur = " + compteur);


Que proposez vous ?
 
Merci.


Message édité par msedirim le 06-12-2009 à 10:30:44
Reply

Marsh Posté le 06-12-2009 à 10:27:42   

Reply

Marsh Posté le 08-12-2009 à 08:36:19    

Voici le résultat de l'exécution:

Citation :


run:
8 déc. 2009 08:10:43 org.hibernate.cfg.annotations.Version <clinit>
INFO: Hibernate Annotations 3.3.1.GA
8 déc. 2009 08:10:43 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.5
8 déc. 2009 08:10:43 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
8 déc. 2009 08:10:43 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
8 déc. 2009 08:10:43 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
8 déc. 2009 08:10:43 org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
8 déc. 2009 08:10:43 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
8 déc. 2009 08:10:43 org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : hibernateproject/Employe.hbm.xml
8 déc. 2009 08:10:43 org.hibernate.cfg.Configuration doConfigure
INFO: Configured SessionFactory: null
8 déc. 2009 08:10:43 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
INFO: Mapping class: hibernateproject.Employe -> employe
8 déc. 2009 08:10:43 org.hibernate.cfg.AnnotationConfiguration secondPassCompile
INFO: Hibernate Validator not found: ignoring
8 déc. 2009 08:10:44 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Using Hibernate built-in connection pool (not for production use!)
8 déc. 2009 08:10:44 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: Hibernate connection pool size: 20
8 déc. 2009 08:10:44 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: autocommit mode: false
8 déc. 2009 08:10:44 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/base_donnee
8 déc. 2009 08:10:44 org.hibernate.connection.DriverManagerConnectionProvider configure
INFO: connection properties: {user=root}
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: RDBMS: MySQL, version: 4.0.15-max-debug
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.0.4 ( $Date: 2006-10-19 17:47:48 +0200 (Thu, 19 Oct 2006) $, $Revision: 5908 $ )
8 déc. 2009 08:10:44 org.hibernate.dialect.Dialect <init>
INFO: Using dialect: org.hibernate.dialect.MySQLDialect
8 déc. 2009 08:10:44 org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
INFO: Using default transaction strategy (direct JDBC transactions)
8 déc. 2009 08:10:44 org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic flush during beforeCompletion(): disabled
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Automatic session close at end of transaction: disabled
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch size: 15
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC batch updates for versioned data: disabled
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Scrollable result sets: enabled
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JDBC3 getGeneratedKeys(): enabled
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Connection release mode: auto
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Maximum outer join fetch depth: 2
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default batch fetch size: 1
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Generate SQL with comments: disabled
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL updates by primary key: disabled
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Order SQL inserts for batching: disabled
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
8 déc. 2009 08:10:44 org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
INFO: Using ASTQueryTranslatorFactory
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query language substitutions: {}
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: JPA-QL strict compliance: disabled
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Second-level cache: enabled
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Query cache: disabled
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory createCacheProvider
INFO: Cache provider: org.hibernate.cache.NoCacheProvider
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Optimize cache for minimal puts: disabled
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Structured second-level cache entries: disabled
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Statistics: disabled
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Deleted entity synthetic identifier rollback: disabled
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Default entity-mode: pojo
8 déc. 2009 08:10:44 org.hibernate.cfg.SettingsFactory buildSettings
INFO: Named query checking : enabled
8 déc. 2009 08:10:44 org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
8 déc. 2009 08:10:44 org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured
nom = dd
********************
nom = aaa
nom = bbb
nom = cc
nom = dd
nom = bassem
nom = ha
nom = am
nom = ah
nom = far
nom = si
********************
********************
nom = aaa
nom = bbb
nom = cc
nom = dd
nom = bassem
nom = ha
nom = am
nom = ah
nom = far
nom = si
********************
Exception in thread "main" java.lang.ClassCastException: java.lang.Long
at hibernateproject.Main.main(Main.java:125)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)


 
 
 
 
Merci.


Message édité par msedirim le 08-12-2009 à 08:38:49
Reply

Marsh Posté le 08-12-2009 à 12:15:50    

Exception in thread "main" java.lang.ClassCastException: java.lang.Long  
at hibernateproject.Main.main(Main.java:125)
 
 
c'est plutôt là ton soucis.
 
à la ligne 125 de la classe Main, t'as un cast qui n'est pas bon.


---------------
HFR - Mes sujets pour Chrome - Firefox - vérifie les nouveaux posts des topics suivis/favoris
Reply

Marsh Posté le 08-12-2009 à 22:18:41    

Oui c'est la ligne 125.
 
Pour les 2 autres requêtes pourquoi elles ne s'exécutent pas : ?

Code :
  1. List employe = session.createQuery("from Employe p where p.nom=?","ali", Hibernate.STRING).list();
  2.      
  3.          for (int i = 0; i < employe.size(); i++) {
  4.     Employe eps = (Employe) employe.get(i);
  5.     System.out.println("nom = " +  eps.getNom());


 
 

Code :
  1. Query query = session.createQuery("from Employe p where p.nom = :bassem" );
  2.        query.setString("bassem", "can" );
  3.       Iterator It = query.iterate();
  4.  
  5.          while (It.hasNext()) {
  6.             Employe per = (Employe) It.next();
  7.             System.out.println("nom = " + per.getNom());
  8.           }


 
 
'can' va remplacer 'bassem' qui existe dans la table
 
 

Citation :


 
 
Après l'exécution,j'ai ceci:
 
    run:
    8 déc. 2009 21:29:08 org.hibernate.cfg.annotations.Version <clinit>
    INFO: Hibernate Annotations 3.3.1.GA
    8 déc. 2009 21:29:09 org.hibernate.cfg.Environment <clinit>
    INFO: Hibernate 3.2.5
    8 déc. 2009 21:29:09 org.hibernate.cfg.Environment <clinit>
    INFO: hibernate.properties not found
    8 déc. 2009 21:29:09 org.hibernate.cfg.Environment buildBytecodeProvider
    INFO: Bytecode provider name : cglib
    8 déc. 2009 21:29:09 org.hibernate.cfg.Environment <clinit>
    INFO: using JDK 1.4 java.sql.Timestamp handling
    8 déc. 2009 21:29:09 org.hibernate.cfg.Configuration configure
    INFO: configuring from resource: /hibernate.cfg.xml
    8 déc. 2009 21:29:09 org.hibernate.cfg.Configuration getConfigurationInputStream
    INFO: Configuration resource: /hibernate.cfg.xml
    8 déc. 2009 21:29:09 org.hibernate.cfg.Configuration addResource
    INFO: Reading mappings from resource : hibernateproject/Employe.hbm.xml
    8 déc. 2009 21:29:09 org.hibernate.cfg.Configuration doConfigure
    INFO: Configured SessionFactory: null
    8 déc. 2009 21:29:09 org.hibernate.cfg.HbmBinder bindRootPersistentClassCommonValues
    INFO: Mapping class: hibernateproject.Employe -> employe
    8 déc. 2009 21:29:09 org.hibernate.cfg.AnnotationConfiguration secondPassCompile
    INFO: Hibernate Validator not found: ignoring
    8 déc. 2009 21:29:09 org.hibernate.connection.DriverManagerConnectionProvider configure
    INFO: Using Hibernate built-in connection pool (not for production use!)
    8 déc. 2009 21:29:09 org.hibernate.connection.DriverManagerConnectionProvider configure
    INFO: Hibernate connection pool size: 20
    8 déc. 2009 21:29:09 org.hibernate.connection.DriverManagerConnectionProvider configure
    INFO: autocommit mode: false
    8 déc. 2009 21:29:09 org.hibernate.connection.DriverManagerConnectionProvider configure
    INFO: using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/base_donnee
    8 déc. 2009 21:29:09 org.hibernate.connection.DriverManagerConnectionProvider configure
    INFO: connection properties: {user=root}
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: RDBMS: MySQL, version: 4.0.15-max-debug
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: JDBC driver: MySQL-AB JDBC Driver, version: mysql-connector-java-5.0.4 ( $Date: 2006-10-19 17:47:48 +0200 (Thu, 19 Oct 2006) $, $Revision: 5908 $ )
    8 déc. 2009 21:29:09 org.hibernate.dialect.Dialect <init>
    INFO: Using dialect: org.hibernate.dialect.MySQLDialect
    8 déc. 2009 21:29:09 org.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
    INFO: Using default transaction strategy (direct JDBC transactions)
    8 déc. 2009 21:29:09 org.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
    INFO: No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Automatic flush during beforeCompletion(): disabled
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Automatic session close at end of transaction: disabled
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: JDBC batch size: 15
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: JDBC batch updates for versioned data: disabled
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Scrollable result sets: enabled
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: JDBC3 getGeneratedKeys(): enabled
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Connection release mode: auto
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Maximum outer join fetch depth: 2
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Default batch fetch size: 1
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Generate SQL with comments: disabled
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Order SQL updates by primary key: disabled
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Order SQL inserts for batching: disabled
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory createQueryTranslatorFactory
    INFO: Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
    8 déc. 2009 21:29:09 org.hibernate.hql.ast.ASTQueryTranslatorFactory <init>
    INFO: Using ASTQueryTranslatorFactory
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Query language substitutions: {}
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: JPA-QL strict compliance: disabled
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Second-level cache: enabled
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Query cache: disabled
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory createCacheProvider
    INFO: Cache provider: org.hibernate.cache.NoCacheProvider
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Optimize cache for minimal puts: disabled
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Structured second-level cache entries: disabled
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Statistics: disabled
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Deleted entity synthetic identifier rollback: disabled
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Default entity-mode: pojo
    8 déc. 2009 21:29:09 org.hibernate.cfg.SettingsFactory buildSettings
    INFO: Named query checking : enabled
    8 déc. 2009 21:29:10 org.hibernate.impl.SessionFactoryImpl <init>
    INFO: building session factory
    8 déc. 2009 21:29:10 org.hibernate.impl.SessionFactoryObjectFactory addInstance
    INFO: Not binding factory to JNDI, no JNDI name configured
    nom = sameh
    ********************
    nom = ali
    nom = mohamed
    nom = hanene
    nom = sameh
    nom = bassem
    nom = hassen
    nom = amal
    nom = ahmed
    nom = farah
    nom = sihem
    ********************
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
    at hibernateproject.Main.main(Main.java:105)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 7 seconds)

Reply

Marsh Posté le 09-12-2009 à 00:42:27    

Ben tu as écrit quoi à la ligne 105 ?

Reply

Marsh Posté le 09-12-2009 à 10:09:05    

tes requêtes ne s'exécutent pas parce que le code avant elles est tout vérolé.


---------------
HFR - Mes sujets pour Chrome - Firefox - vérifie les nouveaux posts des topics suivis/favoris
Reply

Sujets relatifs:

Leave a Replay

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