Search code examples
javahibernatejndi

javax.naming.NoInitialContextException: Need to specify class name in environment or system property


I'm trying to do a simple app with Hibernate. I've created hibernate.cfg.xml and mapping file Personne.hbm.xml. Once I start it I get an exception javax.naming.NoInitialContextException: Need to specify class name in environment or system property...

In detail: The error I am getting is this:

14 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.3.2.GA
16 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
20 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
25 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
83 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
83 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
170 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : bean/Personne.hbm.xml
223 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: bean.Personne -> PERSONNES
238 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: java:hibernate/SessionFactory
292 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
292 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 1
292 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
300 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/Gestion_BD_Personne
300 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=Gestion_user, password=****}
600 [main] INFO org.hibernate.cfg.SettingsFactory - RDBMS: MySQL, version: 5.1.32-community
600 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC driver: MySQL Connector Java, version: mysql-connector-java-5.1.39 ( Revision: 3289a357af6d09ecc1a10fd3c26e95183e5790ad )
618 [main] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.MySQLDialect
621 [main] INFO org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
623 [main] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
623 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
623 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
623 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
623 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
624 [main] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
624 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled
624 [main] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto
625 [main] INFO org.hibernate.cfg.SettingsFactory - Maximum outer join fetch depth: 2
625 [main] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
625 [main] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
625 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
625 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
625 [main] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
627 [main] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
627 [main] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
627 [main] INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
627 [main] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
627 [main] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
629 [main] INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge
630 [main] INFO org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge - Cache provider: org.hibernate.cache.NoCacheProvider
630 [main] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
630 [main] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
632 [main] INFO org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout
633 [main] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled
633 [main] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
633 [main] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
633 [main] INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled
680 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
831 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Factory name: java:hibernate/SessionFactory
831 [main] INFO org.hibernate.util.NamingHelper - JNDI InitialContext properties:{}
833 [main] WARN org.hibernate.impl.SessionFactoryObjectFactory - 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(NamingManager.java:662)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
    at javax.naming.InitialContext.getNameParser(InitialContext.java:505)
    at org.hibernate.util.NamingHelper.bind(NamingHelper.java:75)
    at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:113)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:348)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1341)
    at util.HibernateUtil.<clinit>(HibernateUtil.java:12)
    at manager.PersonneManager.ajouterPersonne(PersonneManager.java:12)
    at test.TestMain.main(TestMain.java:14)
840 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport - Running hbm2ddl schema export
840 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport - exporting generated schema to database
1564 [main] INFO org.hibernate.tool.hbm2ddl.SchemaExport - schema export complete
Hibernate: select max(id_personne) from PERSONNES
Hibernate: insert into PERSONNES (nom_personne, prenom_personne, tel_personne, email_personne, id_personne) values (?, ?, ?, ?, ?)
Hibernate: insert into PERSONNES (nom_personne, prenom_personne, tel_personne, email_personne, id_personne) values (?, ?, ?, ?, ?)
Hibernate: insert into PERSONNES (nom_personne, prenom_personne, tel_personne, email_personne, id_personne) values (?, ?, ?, ?, ?)
1683 [main] INFO org.hibernate.impl.SessionFactoryImpl - closing
1684 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - cleaning up connection pool: jdbc:mysql://localhost:3306/Gestion_BD_Personne
1685 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Unbinding factory from JNDI name: java:hibernate/SessionFactory
1685 [main] INFO org.hibernate.util.NamingHelper - JNDI InitialContext properties:{}
1685 [main] WARN org.hibernate.impl.SessionFactoryObjectFactory - Could not unbind factory from 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(NamingManager.java:662)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
    at javax.naming.InitialContext.unbind(InitialContext.java:441)
    at org.hibernate.impl.SessionFactoryObjectFactory.removeInstance(SessionFactoryObjectFactory.java:139)
    at org.hibernate.impl.SessionFactoryImpl.close(SessionFactoryImpl.java:894)
    at test.TestMain.main(TestMain.java:18)

hibernate.cfg.xml

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

<hibernate-configuration>

    <!-- a SessionFactory instance listed as /jndi/name -->
    <session-factory
        name="java:hibernate/SessionFactory">

        <!-- properties -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/Gestion_BD_Personne</property>
        <property name="connection.username">Gestion_user</property>
        <property name="connection.password">user</property>

        <property name="connection.pool_size">1</property>

        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

        <property name="current_session_context_class">thread</property>

        <property name="cache.provider_class">hibernate.cache.NoCacheProvider</property>

        <property name="show_sql">true</property>

        <property name="hbm2ddl.auto">create</property>

        <!-- mapping files -->
        <mapping resource="bean/Personne.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

Personne.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping> 

    <class name="bean.Personne" table="PERSONNES"> 
        <id name="id" column="id_personne" > 
            <generator class="increment" /> 
        </id> 

        <property name="nom" column="nom_personne" />
        <property name="prenom" column="prenom_personne" />
        <property name="tel" column="tel_personne" />
        <property name="email" column="email_personne" />
    </class> 

</hibernate-mapping>

HibernateUtil.java

package util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    public static final SessionFactory sessionFactory;

    static {
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();

        } catch (Throwable ex) {
            System.out.println("Creation du sessionFactory a échoué"+ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;

    }




}

TestMain.java

package test;

import manager.PersonneManager;
import util.HibernateUtil;

public class TestMain {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    PersonneManager personneManager = new PersonneManager();
    personneManager.ajouterPersonne("nom1", "prenom1", "tel1", "email1");
    personneManager.ajouterPersonne("nom2", "prenom2", "tel2", "email2");
    personneManager.ajouterPersonne("nom3", "prenom3", "tel3", "email3");

    HibernateUtil.sessionFactory.close();

    }

}

Solution

  • Since you said you are just starting to learn Hibernate, I think you have not intentionally tried to create a JNDI bound SessionFactory:

    <session-factory
            name="java:hibernate/SessionFactory">
    

    I can see that this is a copy from the official Hibernate documentation. There the author explains that with this naming you are going to bind your SessionFactory to a JNDI context. And this is exactly what is not working for you because you have not configured that yet. The easiest solution for you is actually just to remove the name parameter and stop trying to do the binding.