I am having a strange problem and all the common solutions don't seem to work. I am trying to run the runStore task from the ant build below, and getting an error:
WARNING: Could not bind factory to JNDI
javax.naming.NoInitialContextException:
The full stack is below.
I am running netbeans 6.5, with an embedded tomcat 5.5. I am not trying to use JNDI, just the default connection pool. I have setup hibernate many times and never come across this error.
Any suggestions? Thanks.
Hibernate: 3.3.1
Mapping documents: <?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 dynamic-insert="false" dynamic-update="false" mutable="true" name="com.bbs.beans.User" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="bbs_user"> <id column="user_number" name="userNumber"> <generator class="native"/> </id> <property column="user_name" name="userName"/> <property column="first_name" name="firstName"/> <property column="last_name" name="lastName"/> <property column="middle_initial" name="middleInitial"/> <!-- <property column="user_rank" name="userRank"/> --> <!-- <property column="user_store_number" name="storeNumber"/> --> <!-- <property column="heart_count" name="heartCount"/> --> <!-- <property column="date_of_birth" name="dateOfBirth" type="timestamp"/> --> <!-- <property column="user_rank_number" name="rankNumber"/> --> <!-- <property column="contact_info_number" name="contactInfoNumber"/> --> <!-- <property column="token_count" name="tokenCount"/> --> </class> </hibernate-mapping>
Code between sessionFactory.openSession() and session.close(): public class HibernateUtil {
private static final SessionFactory sessionFactory; static { try { // Create the SessionFactory from hibernate.cfg.xml sessionFactory = new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } }
public static SessionFactory getSessionFactory() { return sessionFactory; } }
Full stack trace: 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(NamingManager.java:645) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288) at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325) at javax.naming.InitialContext.getNameParser(InitialContext.java:480) 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.b at com.bbs.util.HibernateUtil.<clinit>(Unknown Source) at com.bbs.database.DatabaseBuilder.createAndStoreUser(Unknown Source) at com.bbs.database.DatabaseBuilder.main(Unknown Source) Jan 17, 2009 7:58:21 PM org.hibernate.tool.hbm2ddl.SchemaExport execute INFO: Running hbm2ddl schema export Jan 17, 2009 7:58:21 PM org.hibernate.tool.hbm2ddl.SchemaExport execute INFO: exporting generated schema to database Jan 17, 2009 7:58:22 PM org.hibernate.tool.hbm2ddl.SchemaExport execute INFO: schema export complete Hibernate: insert into bbs_user (user_name, first_name, last_name, middle_initial) values (?, ?, ?, ?) Jan 17, 2009 7:58:22 PM org.hibernate.impl.SessionFactoryImpl close INFO: closing Jan 17, 2009 7:58:22 PM org.hibernate.connection.DriverManagerConnectionProvider close INFO: cleaning up connection pool: jdbc:mysql://localhost:3306/bbs Jan 17, 2009 7:58:22 PM org.hibernate.impl.SessionFactoryObjectFactory removeInstance INFO: Unbinding factory from JNDI name: bbs Jan 17, 2009 7:58:22 PM org.hibernate.util.NamingHelper getInitialContext INFO: JNDI InitialContext properties:{} Jan 17, 2009 7:58:22 PM org.hibernate.impl.SessionFactoryObjectFactory removeInstance WARNING: 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:645) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288) at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325) at javax.naming.InitialContext.unbind(InitialContext.java:416) at org.hibernate.impl.SessionFactoryObjectFactory.removeInstance(SessionFactoryObjectFactory.java:116) at org.hibernate.impl.SessionFactoryImpl.close(SessionFactoryImpl.java:806) at com.bbs.database.DatabaseBuilder.main(Unknown Source) BUILD SUCCESSFUL (total time: 7 seconds)
Name and version of the database you are using: MySql 5.1.7
The generated SQL (show_sql=true): INFO: Running hbm2ddl schema export Jan 17, 2009 7:58:21 PM org.hibernate.tool.hbm2ddl.SchemaExport execute INFO: exporting generated schema to database Jan 17, 2009 7:58:22 PM org.hibernate.tool.hbm2ddl.SchemaExport execute INFO: schema export complete Hibernate: insert into bbs_user (user_name, first_name, last_name, middle_initial) values (?, ?, ?, ?) Jan 17, 2009 7:58:22 PM org.hibernate.impl.SessionFactoryImpl close INFO: closing
Ant Script: <project name="hibernate-tutorial" default="compile">
<property name="sourcedir" value="${basedir}/src"/> <property name="targetdir" value="${basedir}/bin"/> <property name="librarydir" value="${basedir}/lib"/>
<path id="libraries"> <fileset dir="${librarydir}"> <include name="*.jar"/> </fileset> </path>
<target name="clean"> <delete dir="${targetdir}"/> <mkdir dir="${targetdir}"/> </target>
<target name="compile" depends="clean, copy-resources"> <javac srcdir="${sourcedir}" destdir="${targetdir}" classpathref="libraries"/> </target>
<target name="copy-resources"> <copy todir="${targetdir}"> <fileset dir="${sourcedir}"> <exclude name="**/*.java"/> </fileset> </copy> </target>
<target name="run" depends="compile"> <java fork="true" classname="com.bbs.database.DatabaseBuilder" classpathref="libraries"> <classpath path="${targetdir}"/> <arg value="${action}"/> </java> </target>
<target name="runStore" depends="compile"> <java fork="true" classname="com.bbs.database.DatabaseBuilder" classpathref="libraries"> <classpath path="${targetdir}"/> <arg value="store"/> </java> </target> <target name="runList" depends="compile"> <java fork="true" classname="com.bbs.database.DatabaseBuilder" classpathref="libraries"> <classpath path="${targetdir}"/> <arg value="list"/> </java> </target>
</project>
User Bean: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.bbs.beans;
//import java.util.GregorianCalendar;
/** * * @author scott */ public class User {
private Long userNumber; private String userName; private String firstName; private String lastName; private String middleInitial;
/** * @return the userName */ public String getUserName() { return userName; }
/** * @param userName the userName to set */ public void setUserName(String userName) { this.userName = userName; }
/** * @return the firstName */ public String getFirstName() { return firstName; }
/** * @param firstName the firstName to set */ public void setFirstName(String firstName) { this.firstName = firstName; }
/** * @return the lastName */ public String getLastName() { return lastName; }
/** * @param lastName the lastName to set */ public void setLastName(String lastName) { this.lastName = lastName; }
/** * @return the middleInitial */ public String getMiddleInitial() { return middleInitial; }
/** * @param middleInitial the middleInitial to set */ public void setMiddleInitial(String middleInitial) { this.middleInitial = middleInitial; }
/** * @return the userNumber */ public Long getUserNumber() { return userNumber; }
/** * @param userNumber the userNumber to set */ public void setUserNumber(Long userNumber) { this.userNumber = userNumber; }
}
Hibernate Config: <?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="bbs"> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect </property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver </property> <property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/bbs</property> <property name="hibernate.connection.username">user1</property> <property name="hibernate.connection.password">tbd34</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- Enable Hibernate's automatic session context management--> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class"> org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">create</property> <mapping resource="com/bbs/beans/User.hbm.xml"/> </session-factory> </hibernate-configuration>
_________________ Scott
www.HikeHaven.com
|