SessionFactoryObjectFactory Could not bind factory to JNDI

SessionFactoryObjectFactory Could not bind factory to JNDI

WARN SessionFactoryObjectFactory:121 – Could not bind factory to JNDI

19:35:55,017  WARN SessionFactoryObjectFactory:121 - 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:307)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
    at javax.naming.InitialContext.getNameParser(InitialContext.java:499)
    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:378)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1385)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)
    at com.javahonk.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:15)
    at com.javahonk.util.HibernateUtil.<clinit>(HibernateUtil.java:7)
    at com.javahonk.OneToOneInsert.main(OneToOneInsert.java:16)

Solution: If you see above exception it means name=”sessionFacotory” attribute is added to *.cfg.xml file as shown below in Red

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="sessionFacotory">
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">admin</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/javahonk</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.search.autoregister_listeners">false</property>
        <mapping class="com.javahonk.bean.Person" />
        <mapping class="com.javahonk.bean.Address" />
    </session-factory>
</hibernate-configuration>

  • To fix this please remove name=”sessionFacotory” from <session-factory> as shown below:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">admin</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/javahonk</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.search.autoregister_listeners">false</property>
        <mapping class="com.javahonk.bean.Person" />
        <mapping class="com.javahonk.bean.Address" />
    </session-factory>
</hibernate-configuration>

Leave a Reply

Your email address will not be published. Required fields are marked *