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 :

Hibernate: erreur JNDI


Sujet :

Hibernate Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    43
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2006
    Messages : 43
    Par défaut Hibernate: erreur JNDI
    Bonjour,

    J'essaye de me former à hibernate mais je n'arrive pas à le faire fonctionner sans erreur. Cela fait des heures que j'essaye de le faire marcher sous eclipse + mysql comme bdd mais lorsque je compile, je rencontre l'erreur suivante:

    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
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    INFO: JNDI InitialContext properties:{}
    
    6 juil. 2006 18:02:32 net.sf.hibernate.impl.SessionFactoryObjectFactory addInstance
    
    ATTENTION: Could not bind factory to JNDI
    
    javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    
          at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
    
          at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    
          at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
    
          at javax.naming.InitialContext.getNameParser(Unknown Source)
    
          at net.sf.hibernate.util.NamingHelper.bind(NamingHelper.java:52)
    
          at net.sf.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:90)
    
          at net.sf.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:171)
    
          at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:791)
    
          at com.minosis.hibernate.HibernateUtil.<clinit>(HibernateUtil.java:15)
    
          at com.minosis.Test.main(Test.java:10)
    
    6 juil. 2006 18:02:32 net.sf.hibernate.transaction.JTATransaction begin
    
    GRAVE: Could not find UserTransaction in JNDI
    
    javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    
          at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
    
          at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    
          at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
    
          at javax.naming.InitialContext.lookup(Unknown Source)
    
          at net.sf.hibernate.transaction.JTATransaction.begin(JTATransaction.java:133)
    
          at net.sf.hibernate.transaction.JTATransactionFactory.beginTransaction(JTATransactionFactory.java:58)
    
          at net.sf.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:2231)
    
          at com.minosis.Test.main(Test.java:12)
    
    net.sf.hibernate.TransactionException: Could not find UserTransaction in JNDI:
    
          at net.sf.hibernate.transaction.JTATransaction.begin(JTATransaction.java:137)
    
          at net.sf.hibernate.transaction.JTATransactionFactory.beginTransaction(JTATransactionFactory.java:58)
    
          at net.sf.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:2231)
    
          at com.minosis.Test.main(Test.java:12)
    
    Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    
          at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
    
          at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    
          at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
    
          at javax.naming.InitialContext.lookup(Unknown Source)
    
          at net.sf.hibernate.transaction.JTATransaction.begin(JTATransaction.java:133)
    
          ... 3 more
    
    Exception in thread "main"
    Qu'est ce que ça signifie?
    Je vous remercie d'avance pour votre aide.

  2. #2
    Membre chevronné

    Inscrit en
    Avril 2005
    Messages
    317
    Détails du profil
    Informations forums :
    Inscription : Avril 2005
    Messages : 317
    Par défaut
    Si tu nous montres ton code, on pourra t'aider

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    43
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2006
    Messages : 43
    Par défaut
    Oui pardon le voila:

    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
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
     
    Test.java:
     
    package com.minosis;
     
    import net.sf.hibernate.*;
    import com.minosis.hibernate.*;
     
    public class Test {
     
     public static void main(String[] args) throws HibernateException {
     
    	 Session session = HibernateUtil.currentSession();
     
    	 Transaction tx = session.beginTransaction();
     
    	 TAuteur auteur = new TAuteur();
    	 auteur.setNom("Dupont");
    	 auteur.setPrenom("Jean");
    	 session.save(auteur);
     
    	 auteur = new TAuteur();
    	 auteur.setNom("Lambert");
    	 auteur.setPrenom("Julie");
    	 session.save(auteur);
     
    	 tx.commit();
     
    	 HibernateUtil.closeSession();	 
     
     }
    }
     
    HibernateUtil.java:
     
    package com.minosis.hibernate;
     
    import net.sf.hibernate.HibernateException;
    import net.sf.hibernate.Session;
    import net.sf.hibernate.SessionFactory;
    import net.sf.hibernate.cfg.Configuration;
     
    public class HibernateUtil {
     
     private static final SessionFactory sessionFactory;
     
     static {
       try {
       // Crée la SessionFactory
       sessionFactory = new Configuration().configure().buildSessionFactory();
       } catch (HibernateException ex) {
       throw new RuntimeException("Problème de configuration : "
       + ex.getMessage(), ex);
       }
    }
     
     public static final ThreadLocal session = new ThreadLocal();
     
     public static Session currentSession()
    		throws HibernateException {
       Session s = (Session) session.get();
       // Ouvre une nouvelle Session, si ce Thread n'en a aucune
       if (s == null) {
       s = sessionFactory.openSession();
       session.set(s);
       }
       return s;
       }
     
     public static void closeSession()
    		throws HibernateException {
       Session s = (Session) session.get();
       session.set(null);
       if (s != null)
       s.close();
       }
     }
     
    TAuteur.java:
     
    package com.minosis;
     
    public class TAuteur {
    	private int id;
    	private String nom;
    	private String prenom;
     
    	public void setId(int newId) {
    		this.id = newId;
    	}
     
    	public void setNom(String newNom) {
    		this.nom = newNom;
    	}
     
    	public void setPrenom(String newPrenom) {
    		this.prenom = newPrenom;
    	}
     
    	public int getId() {
    		return this.id;
    	}
     
    	public String getNom() {
    		return this.nom;
    	}
     
    	public String getPrenom() {
    		return this.prenom;
    	}
    }
     
    Merci d'avance

  4. #4
    Membre expérimenté
    Inscrit en
    Juillet 2004
    Messages
    152
    Détails du profil
    Informations forums :
    Inscription : Juillet 2004
    Messages : 152
    Par défaut
    tu dois specifier la propriete hibernate.jndi.class dans ton fichier de configuration Hibernate

  5. #5
    Membre averti
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    43
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2006
    Messages : 43
    Par défaut
    Bon finalement ça venait de mon fichier de configuration xml qui était mauvais. J'ai repris celui de la doc officielle d'hibernate en l'adaptant et depuis ça fonctionne

    Merci à tous pour votre aide

  6. #6
    Membre éprouvé

    Profil pro
    MOA
    Inscrit en
    Décembre 2002
    Messages
    1 232
    Détails du profil
    Informations personnelles :
    Localisation : France, Eure et Loir (Centre)

    Informations professionnelles :
    Activité : MOA

    Informations forums :
    Inscription : Décembre 2002
    Messages : 1 232
    Par défaut
    j'ai a peu pres le meme souci

    Ca me jete cette exception :
    GRAVE: Could not find UserTransaction in JNDI
    javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
    Voici la classe qui en théorie devrait inserer des données dans MySQL
    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
    import java.util.*;
    import net.sf.hibernate.*;
     
    import com.HibernateUtil;
    import com.hibernate.*;
     
    public class Test {
     
    	public static void main(String[] args) throws HibernateException {
    		Session session = HibernateUtil.currentSession();
    		Transaction tx = session.beginTransaction();
    		Utilisateurs contact = new Utilisateurs ();
    		contact.setNom("Dupont");
    		contact.setPrenom("Jean");
    		contact.setPassword("monpassword");
    		session.save(contact);
     
    		tx.commit();
     
    		HibernateUtil.closeSession();
     
    	}
     
    }
    Si on essaie de comprendre l'erreur, il faut spécifier un nom de classe dans l'environnement.
    Avant de poster, j'ai fait des rechercher et il semblerait qu'il faille un fichier JDNI ? Je me trompe ?
    J'ai trouvé un post avec le problème similaire
    http://www.developpez.net/forums/sho...d.php?t=467428

    Le dernier post conseille de coller ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    Hashtable ht=new Hashtable();
    		ht.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
    		ht.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
    		ht.put("java.naming.provider.url", "localhost");
     
    		Context ctx=new InitialContext(ht);
    Ce sont apparrement des alias. La première ligne serait peut etre une solution à ma problème mais le souci c'est que Eclipse n'arrive pas à trouver le package à importer pour l'objet InitialContext, donc erreur.

    EDIT : je viens de trouver les imports mais cela ne fonctionne toujours pas.

    Mon hibernate.cfg.xml si cela peut aider.
    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
    25
    26
    27
    28
    29
    30
    31
    32
    33
     
    <?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:mysql://localhost:3306/hibernate
    		</property>
    		<property name="hibernate.connection.driver_class">
    			net.sourceforge.jtds.jdbc.Driver
    		</property>
    		<property name="hibernate.connection.username">root</property>
    		<property name="hibernate.connection.password" />
    		<!-- property name="hibernate.connection.pool_size"></property -->
    		<!-- dialect for MySQL -->
    		<property name="dialect">
    			net.sf.hibernate.dialect.MySQLDialect
    		</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>
    		<mapping resource="com/hibernate/Utilisateurs.hbm" />
    	</session-factory>
    </hibernate-configuration>
    QUe me conseillez vous de faire ?

    Demain, je reprends tout depuis le début. Je me recréé un nouveau projet et je verrais si on me sort toujours cette erreur.

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

Discussions similaires

  1. [hibernate][netbeans6][jsf]message d'erreur JNDI "\?\?\?\?"
    Par liquideshark dans le forum NetBeans
    Réponses: 2
    Dernier message: 15/07/2009, 22h07
  2. Eclipse-Hibernate-erreur Execution
    Par Invité dans le forum Hibernate
    Réponses: 18
    Dernier message: 22/06/2006, 09h37
  3. [Hibernate] Erreur de lecture
    Par magnus2005 dans le forum Hibernate
    Réponses: 2
    Dernier message: 05/05/2006, 23h51
  4. [Hibernate] Erreur de lecture
    Par magnus2005 dans le forum Hibernate
    Réponses: 2
    Dernier message: 24/01/2006, 16h47
  5. [Hibernate] : Erreur Could not find datasource
    Par tipaquo dans le forum Hibernate
    Réponses: 2
    Dernier message: 12/10/2005, 10h43

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