javax-naming-noinitialcontextexception-need-specify-class-name-environment-system-property
If you see below exception when try to perform CRUD operation using hibernate:
WARNING: 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 org.hibernate.util.NamingHelper.bind(NamingHelper.java:52) at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:90) at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:306) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294) at com.javahonk.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:11) at com.javahonk.util.HibernateUtil.<clinit>(HibernateUtil.java:7) at com.javahonk.InsertData.main(InsertData.java:16)
Solution: This happens due to name attribute in session-factory tag in hibernet.cfg.xml file 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 name="sessionFactory"> <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.default_schema">JavaHonk</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <mapping resource="Address.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
- Remove name attribute from session-factory tag and run code again. This should resolve the issue.