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

  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 242
    Détails du profil
    Informations personnelles :
    Localisation : France, Eure et Loir (Centre)

    Informations professionnelles :
    Activité : MOA

    Informations forums :
    Inscription : Décembre 2002
    Messages : 1 242
    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.

  7. #7
    Membre confirmé
    Profil pro
    Ingénieur
    Inscrit en
    Juillet 2006
    Messages
    61
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Ingénieur

    Informations forums :
    Inscription : Juillet 2006
    Messages : 61
    Par défaut
    Bonjour, on a essayé apparemment le même tutoriel et j ai le même problème que toi, j ai toujours pas trouvé comment faire!

  8. #8
    Nouveau candidat au Club
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    2
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2008
    Messages : 2
    Par défaut Salut à tous !
    J'ai exactement le même soucis.. ce qui est embêtant pour moi car il s'agit d'un projet à réaliser pour mon stage...

    Si par hasard quelqu'un avait la solution à cet épineux problème mais n'eut pas jugé utile de revenir sur le thread tenant compte de l'âge des posts.. qu'il se manifeste !

    Reconnaissance éternelle à la clef

    Merci d'avance !

    Bien à vous,
    Alexandre.

  9. #9
    Membre confirmé Avatar de arnaud.tlse
    Profil pro
    Étudiant
    Inscrit en
    Avril 2008
    Messages
    115
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2008
    Messages : 115
    Par défaut
    Salut, voici la solution je pense :

    Vous avez suivi le tuto de Minosis Débuter avec Hibernate sous Eclipse, cependant vous avez oublié le passage sur la configuration du fichier xml où il indiquant de remplacer les lignes :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    		<property name="hibernate.transaction.factory_class">
    			net.sf.hibernate.transaction.JTATransactionFactory
    		</property>
    		<property name="jta.UserTransaction">
    			java:comp/UserTransaction
    		</property>
    par :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    		<!--
    		<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>
    Remplacez ces lignes dans votre fichier hibernate.cfg.xml et ça devrait fonctionner.

  10. #10
    Invité de passage
    Femme Profil pro
    Étudiant
    Inscrit en
    Juillet 2011
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Juillet 2011
    Messages : 1
    Par défaut
    moi aussi j'ai la même erreur et j'ai essayé les propositions ci-dessous et ça ne marche pas
    voici le 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
    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
     
    public class HibernateUtil {
     
    	private static final SessionFactory sessionFactory;
    	static {
    		try {
    			// Crée la SessionFactory
    			sessionFactory = new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory();
    		} catch (HibernateException ex) {
    		throw new RuntimeException("Problème de configuration : " + ex.getMessage(), ex);
    		}
    	}
     
    	public static final ThreadLocal<Session>  threadLocal = new ThreadLocal<Session>();
     
    	public static Session currentSession() throws HibernateException {
     
    		Session s = (Session) threadLocal.get();
    		// Ouvre une nouvelle Session, si ce Thread n'en a aucune
    		if (s == null) {
    			 s = sessionFactory.openSession();
    			 threadLocal.set(s);
    			}
    			return s;
    	}
     
    	public static void closeSession() throws HibernateException {
    		Session s = (Session) threadLocal.get();
    		threadLocal.set(null);
    		if (s != null)
    		s.close();
    	}
    }
     
    <hibernate-configuration>
    	<session-factory>
    		<!-- local connection properties -->
    		<property name="hibernate.connection.url">
    			jdbc:mysql://localhost/base_donn?e
    		</property>
    		<property name="hibernate.connection.driver_class">
    			com.mysql.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="Client.hbm" />
    	</session-factory>
    </hibernate-configuration>
     
    public class Test {
     
     public static void main(String[] args)
    	throws HibernateException {
    	 Session session = HibernateUtil.currentSession();
     
    	 Transaction tx = session.beginTransaction();
     
     
    	 Personnel contact = new Personnel();
    	 contact.setNom("Dupont");
    	 contact.setPrenom("Jean");
    	 contact.setFonction("administrateur");
    	 contact.setService("marketing");
    	 session.save(contact);
     
    	 tx.commit();
     
    	 HibernateUtil.closeSession();
     
     }
    }

+ 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