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 :

Probleme de lecture Sous Hibernate


Sujet :

Hibernate Java

  1. #1
    Invité
    Invité(e)
    Par défaut Probleme de lecture Sous Hibernate
    Salut a vous tous
    Voilà j’ai un problème avec la lecture depuis hibernate j’ai tous essayer mais ca marche pa je sai pa pk si vous avez un exemple de lecture complet vous pouvez me le donnez ca va bcp m’aidez voilà des exemple que j’ai essayer mais ca marche pa :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
      List list = session.find("from TContact where nom like '%t'");Iterator it = list.iterator();while(it.hasNext()){  TContact contact = (TContact)it.next();  System.out.println(contact.getNom());} HibernateUtil.closeSession();
    Ici c un prob dans la methode find que eclipse declare comme erreur

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
      Query q = sess.createQuery("from DomesticCat cat where cat.name = :name");q.setString("name", "Fritz");Iterator cats = q.iterate();
    Aussi ca marche pa

    Si vous avez un exemple qui peut m’aider ca sera sympa

    Modéré par zekey: suppression de la notion d'urgence dans le titre : voir les rêgles du forum en haut à gauche
    Dernière modification par zekey ; 24/04/2006 à 12h17.

  2. #2
    Membre expérimenté
    Avatar de zekey
    Profil pro
    Inscrit en
    Février 2005
    Messages
    1 036
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 1 036
    Points : 1 403
    Points
    1 403
    Par défaut
    Quelle version d'hibernate, du jdk stp
    Steve Hostettler
    est ton ami(e) et le tag aussi.

  3. #3
    Invité
    Invité(e)
    Par défaut
    Merci pour l'information je doit relire les regles je pense
    alors voila pour la version j'utilise Hibernate 3.1.3
    la jdk 1.5
    voici mon code
    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
    public static void main(String[] args) {
            Session session = HibernateUtil.currentSession();
            Query query = session.createSQLQuery("select nom from employer where nom=" + "5");
            List list = query.list(); 
            Iterator it = list.iterator();
     
            while(it.hasNext())
            {
              Employer contact = (Employer)it.next();
              System.out.println(contact.getNom());
            }
     
            HibernateUtil.closeSession();
        }
    }
    et voici l'erreur qui ce genere
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object;
        at test.Select.main(Select.java:34)
    Merci D'avance
    Dernière modification par Invité ; 24/04/2006 à 13h11.

  4. #4
    Membre expérimenté
    Avatar de zekey
    Profil pro
    Inscrit en
    Février 2005
    Messages
    1 036
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 1 036
    Points : 1 403
    Points
    1 403
    Par défaut
    Alors en fait ton erreur viens de la confusion entre la table et l'objet qui la représente:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
      Query query = session.createSQLQuery("select nom from employer where nom=" + "5");
    employer c'est la table et Employer l'objet.
    Hibernate utilise les objets normalement et pas les tables (même si il peut)
    Donc change par:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
      Query query = session.createSQLQuery("from Employer e where e.nom=" + "5");
    Steve Hostettler
    est ton ami(e) et le tag aussi.

  5. #5
    Invité
    Invité(e)
    Par défaut
    Merci bien de votre reponse
    voici le code apres modification
    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
    public static void main(String[] args) {
            Session session = HibernateUtil.currentSession();
     
            Query query = session.createSQLQuery("from Employer e where e.nom=" + "5");
     
             List list = query.list(); 
             Iterator it = list.iterator();
     
                while(it.hasNext())
                {
                  Employer contact = (Employer)it.next();
                  System.out.println(contact.getNom());
                }
     
            HibernateUtil.closeSession();
        }
    }
    j'ai modifier employer(la table) par Employer (L'objet), mais je pense que moi vue que j'utilise le SQLQuery c pa obligatoire c utilisable quand on fai Query directe mais je suis pa trop sur de ca
    donc voila les erreur qui ce genere apres la modification

    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
    Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute query
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
        at org.hibernate.loader.Loader.doList(Loader.java:2148)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
        at org.hibernate.loader.Loader.list(Loader.java:2024)
        at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:111)
        at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1655)
        at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
        at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:164)
        at test.Select.main(Select.java:31)
    Caused by: java.sql.SQLException: Erreur de syntaxe près de 'from Employer e where e.nom=5' à la ligne 1
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1666)
        at com.mysql.jdbc.Connection.execSQL(Connection.java:2994)
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:936)
        at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1030)
        at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:139)
        at org.hibernate.loader.Loader.getResultSet(Loader.java:1669)
        at org.hibernate.loader.Loader.doQuery(Loader.java:662)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
        at org.hibernate.loader.Loader.doList(Loader.java:2145)
        ... 7 more
    trop d'erreur
    d'apres ce rapport d'erreur je pense qu'il peut pa executer la requette
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    could not execute query
    un autre truc pk en met pa le select en met tjr le from directe ??
    si vous avez un programme qui marche ca serai sympa de le mettre ici sinon si qlq un connais d'ou vien l'erreur je suis tjr la
    Merci bien

  6. #6
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2005
    Messages
    274
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2005
    Messages : 274
    Points : 307
    Points
    307
    Par défaut
    Bonjour,

    il na faut pas confondre les deux méthodes suivantes :

    createSQLQuery pour des requêtes SQL natives et la récupération de resultsets.

    createQuery pour des requêtes avec une syntaxe HQL et la récupération d'objets JAVA

    Essaie avec :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Query query = session.createQuery("from Employer e where e.nom=" + "5");

  7. #7
    Membre expérimenté
    Avatar de zekey
    Profil pro
    Inscrit en
    Février 2005
    Messages
    1 036
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 1 036
    Points : 1 403
    Points
    1 403
    Par défaut
    Oui mes excuses je n'avais pas vu que tu faisais appel à createSQLQuery, mais je pense que ce n'est pas ce que tu veux faire en réalité puisque après tu fais:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Employer contact = (Employer)it.next();
    Donc tu t'attend à recevoir un objet en retour non ?
    Donc effectivement avec SQLQuery tu dois recevoir une liste de Map contenant les champs, ce qui ne peut être casté en objet Employer.
    Steve Hostettler
    est ton ami(e) et le tag aussi.

  8. #8
    Invité
    Invité(e)
    Par défaut
    dabord je voulais vous remerciez pour vos reponse
    alors voila j'ai tous relie et j'ai modifier mon code de tel sorte que je selectionne tout les champ de ma table (select *)
    donc voila mon code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    public static void main(String[] args) {
            Session session = HibernateUtil.currentSession();
     
            Query query = session.createQuery("from Employer e");
            List list = query.list();
            System.out.println(list);
            HibernateUtil.closeSession();
     
        }
    je pense que j'ai commis une erreur vue le resultat
    sachant que 5 c le contenu de Matricule dans la table employer
    moi je pense que c pa juste vue que je fai un print sur list mais pour moi c un exploit que j'ai pa d'erreur
    mais le bon resultat doit etre l'affichage de tous les champ de la table employer donc si vous pouvez m'aider a completer ce code ca va me faire gagner bcp de temp dans mon projet
    Merci encore

  9. #9
    Membre expérimenté
    Avatar de zekey
    Profil pro
    Inscrit en
    Février 2005
    Messages
    1 036
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 1 036
    Points : 1 403
    Points
    1 403
    Par défaut
    Non non ca marche mais le toString sur cet objet ne te donne pas le détail mais l'id de cet objet.
    Bref remplace ton
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    System.out.println(list);
    par
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    Iterator it = list.iterator();
     
    while(it.hasNext()){
      Employer contact = (Employer)it.next();
      System.out.println(contact.getNom());
    }
    Steve Hostettler
    est ton ami(e) et le tag aussi.

  10. #10
    Invité
    Invité(e)
    Par défaut
    Un grand merci a vous tous c'est bon j'ai reussie a recupere les nom avec le getNom
    je vous remercie encore ca va me faire gagner bcp de temp

  11. #11
    Membre à l'essai
    Inscrit en
    Décembre 2009
    Messages
    11
    Détails du profil
    Informations forums :
    Inscription : Décembre 2009
    Messages : 11
    Points : 13
    Points
    13
    Par défaut
    j'ai ce msg d'erreur


    24 mars 2010 10:07:52 net.sf.hibernate.cfg.Environment <clinit>
    INFO: Hibernate 2.1.6
    24 mars 2010 10:07:52 net.sf.hibernate.cfg.Environment <clinit>
    INFO: hibernate.properties not found
    24 mars 2010 10:07:52 net.sf.hibernate.cfg.Environment <clinit>
    INFO: using CGLIB reflection optimizer
    24 mars 2010 10:07:52 net.sf.hibernate.cfg.Configuration configure
    INFO: configuring from resource: /hibernate.cfg.xml
    24 mars 2010 10:07:52 net.sf.hibernate.cfg.Configuration getConfigurationInputStream
    INFO: Configuration resource: /hibernate.cfg.xml
    24 mars 2010 10:07:52 net.sf.hibernate.cfg.Configuration addResource
    INFO: Mapping resource: TContact.hbm
    24 mars 2010 10:07:52 net.sf.hibernate.cfg.Binder bindRootClass
    INFO: Mapping class: com.minosis.hibernate.TContact -> t_contact
    24 mars 2010 10:07:52 net.sf.hibernate.cfg.Configuration doConfigure
    INFO: Configured SessionFactory: null
    24 mars 2010 10:07:52 net.sf.hibernate.cfg.Configuration secondPassCompile
    INFO: processing one-to-many association mappings
    24 mars 2010 10:07:52 net.sf.hibernate.cfg.Configuration secondPassCompile
    INFO: processing one-to-one association property references
    24 mars 2010 10:07:52 net.sf.hibernate.cfg.Configuration secondPassCompile
    INFO: processing foreign key constraints
    24 mars 2010 10:07:52 net.sf.hibernate.dialect.Dialect <init>
    INFO: Using dialect: net.sf.hibernate.dialect.PostgreSQLDialect
    24 mars 2010 10:07:52 net.sf.hibernate.cfg.SettingsFactory buildSettings
    INFO: Use outer join fetching: true
    24 mars 2010 10:07:52 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
    INFO: Using Hibernate built-in connection pool (not for production use!)
    24 mars 2010 10:07:52 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
    INFO: Hibernate connection pool size: 20
    24 mars 2010 10:07:52 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
    INFO: using driver: org.postgresql.Driver at URL: jdbc:postgresql://127.0.0.1:5432/test_bd
    24 mars 2010 10:07:52 net.sf.hibernate.connection.DriverManagerConnectionProvider configure
    INFO: connection properties: {user=postgres, password=postgres}
    24 mars 2010 10:07:52 net.sf.hibernate.transaction.TransactionFactoryFactory buildTransactionFactory
    INFO: Transaction strategy: net.sf.hibernate.transaction.JDBCTransactionFactory
    24 mars 2010 10:07:52 net.sf.hibernate.transaction.TransactionManagerLookupFactory getTransactionManagerLookup
    INFO: No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
    24 mars 2010 10:07:53 net.sf.hibernate.cfg.SettingsFactory buildSettings
    INFO: Use scrollable result sets: true
    24 mars 2010 10:07:53 net.sf.hibernate.cfg.SettingsFactory buildSettings
    INFO: Use JDBC3 getGeneratedKeys(): false
    24 mars 2010 10:07:53 net.sf.hibernate.cfg.SettingsFactory buildSettings
    INFO: Optimize cache for minimal puts: false
    24 mars 2010 10:07:53 net.sf.hibernate.cfg.SettingsFactory buildSettings
    INFO: Query language substitutions: {}
    24 mars 2010 10:07:53 net.sf.hibernate.cfg.SettingsFactory buildSettings
    INFO: cache provider: net.sf.hibernate.cache.EhCacheProvider
    24 mars 2010 10:07:53 net.sf.hibernate.cfg.Configuration configureCaches
    INFO: instantiating and configuring caches
    Exception in thread "main" java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(Z)V
    at net.sf.cglib.core.DebuggingClassWriter.<init>(DebuggingClassWriter.java:47)
    at net.sf.cglib.core.DefaultGeneratorStrategy.getClassWriter(DefaultGeneratorStrategy.java:30)
    at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:24)
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:215)
    at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:117)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
    at net.sf.hibernate.impl.SessionFactoryImpl.<clinit>(SessionFactoryImpl.java:236)
    at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:791)
    at com.minosis.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:10)
    at Test.main(Test.java:7)



    j'utilise eclipse+postgresql 8.3+hibernate synchronizer
    aider mois svp!

  12. #12
    Membre à l'essai
    Inscrit en
    Décembre 2009
    Messages
    11
    Détails du profil
    Informations forums :
    Inscription : Décembre 2009
    Messages : 11
    Points : 13
    Points
    13
    Par défaut
    j'ai ce fichier de cofiguration XML



    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

    <hibernate-configuration>
    <session-factory>
    <!-- local connection properties -->
    <property name="hibernate.connection.url">
    jdbc:postgresql://127.0.0.1:5432/test_bd
    </property>
    <property name="hibernate.connection.driver_class">
    org.postgresql.Driver
    </property>
    <property name="hibernate.connection.username">postgres</property>
    <property name="hibernate.connection.password">postgres</property>
    <!-- property name="hibernate.connection.pool_size"></property -->
    <!-- dialect for PostgreSQL -->
    <property name="dialect">

    org.hibernate.dialect.PostgreSQLDialect
    <!-- net.sf.hibernate.dialect.PostgreSQLDialect -->

    </property>
    <property name="hibernate.show_sql">false</property>
    <property name="hibernate.use_outer_join">true</property>
    <!--
    <property name="hibernate.transaction.factory_class">net.sf.hibernate.transaction.JTATransactionFactory</property>
    <property name="jta.UserTransaction">java:comp/UserTransaction</property>

    <property name="hibernate.transaction.factory_class">
    net.sf.hibernate.transaction.JDBCTransactionFactory
    </property> -->
    <mapping resource="TContact.hbm" />
    </session-factory>
    </hibernate-configuration>

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

Discussions similaires

  1. probleme d'execution appli sous HIBERNATE
    Par taouja dans le forum Hibernate
    Réponses: 0
    Dernier message: 24/03/2009, 03h07
  2. probleme de lecture de fichier sous Linux
    Par hbellahc dans le forum Entrée/Sortie
    Réponses: 2
    Dernier message: 04/07/2008, 15h08
  3. Probleme de lecture de fichier Excel sous Access
    Par tribaleur dans le forum VBA Access
    Réponses: 6
    Dernier message: 10/03/2008, 07h51
  4. [hibernate] probleme à la compilation sous eclipse
    Par ejaub dans le forum Eclipse Java
    Réponses: 1
    Dernier message: 23/11/2005, 10h45
  5. [Kylix] Probleme d'installation sous Mdk 9.1
    Par anderson2 dans le forum EDI
    Réponses: 4
    Dernier message: 25/05/2003, 18h27

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