Hibernate error

Hello there,

I have started learning hibernate and i came across my first error,but dont seem to get the error.can you please help.

15:34:18,527  WARN SessionFactoryObjectFactory:438 - 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: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 com.util.HibernateUtil.<clinit>(HibernateUtil.java:14)
	at com.zolf.Main.saveRecord(Main.java:26)
	at com.zolf.Main.main(Main.java:18)
15:34:18,626  WARN JDBCExceptionReporter:360 - SQL Error: 208, SQLState: S0002
15:34:18,626 ERROR JDBCExceptionReporter:457 - Invalid object name 'COURSES'.
org.hibernate.exception.SQLGrammarException: could not insert: [com.zolf.Course]
	at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90)
	at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
	at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2176)
	at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2656)
	at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
	at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
	at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:321)
	at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:204)
	at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:130)
	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
	at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
	at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
	at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
	at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:563)
	at org.hibernate.impl.SessionImpl.save(SessionImpl.java:551)
	at org.hibernate.impl.SessionImpl.save(SessionImpl.java:547)
	at com.zolf.Main.saveRecord(Main.java:37)
	at com.zolf.Main.main(Main.java:18)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'COURSES'.
	at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(Unknown Source)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(Unknown Source)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(Unknown Source)
	at com.microsoft.sqlserver.jdbc.TDSCommand.execute(Unknown Source)
	at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(Unknown Source)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(Unknown Source)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(Unknown Source)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(Unknown Source)
	at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
	at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
	... 17 more

Open in new window

zolfAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

colr__Commented:
Does the table 'COURSES' definetly exist in your database?
zolfAuthor Commented:

no i was expecting hibernate to create that for me
Gurvinder Pal SinghCommented:
looks like you dont have the COURSES mapped. Do you have something like
Course.hbm.xml?

or you haven't added

<mapping resource="com/zolf/Course.hbm.xml"/>

to hibernate.cfg.xml?
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

zolfAuthor Commented:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 31-Mar-2011 14:46:04 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
	<class name="com.zolf.Course" table="COURSES">
		<id name="courseID" type="long" column="COURSEID">
			<generator class="native" />
		</id>

		<property name="courseName" type="java.lang.String" column="COURSENAME"
			not-null="true" />

		<property name="courseFaculty" type="java.lang.String"
			column="COURSEFACULTY" not-null="true" />

		<property name="courseDate" type="java.util.Calendar" column="COURSEDATE"
			not-null="true" />

		<property name="courseDuration" type="int" column="COURSEDURATION"
			not-null="true" />

	</class>
</hibernate-mapping>

Open in new window

zolfAuthor Commented:

<?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="">
  <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
  <property name="hibernate.connection.url">jdbc:sqlserver://localhost;DatabaseName=test</property>
  <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
  <property name="hibernate.connection.username">test</property>
  <property name="hibernate.connection.password">secret</property>
  
  <!-- MAPPING -->
  <mapping resource="com/zolf/Course.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

Open in new window

colr__Commented:
Try adding this to your config file:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gurvinder Pal SinghCommented:
can you show the query also?

you need to use com.zolf.Course in the query in place of COURSES
zolfAuthor Commented:

can you please tell me more specific,since i am new to hibernate
zolfAuthor Commented:

here is the main code
package com.zolf;

import java.util.Calendar;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.util.HibernateUtil;

public class Main
{
	public static void main(String[] args)
	{
		Calendar c = Calendar.getInstance();
		c.set(2011, 03, 31);
		Main m = new Main();
		Long courseId1 = m.saveRecord("Hibernate", "ABCD", c, 1);
	}

	public Long saveRecord(String courseName, String courseFaculty,
			Calendar courseDate, int courseDuration)
	{
		Long recordID = null;

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;

		try
		{
			transaction = session.beginTransaction();
			Course course = new Course();
			course.setCourseName(courseName);
			course.setCourseFaculty(courseFaculty);
			course.setCourseDuration(courseDuration);
			course.setCourseDate(courseDate);
			recordID = (Long) session.save(course);
			transaction.commit();
		} catch (HibernateException e)
		{
			// TODO Auto-generated catch block
			transaction.rollback();
			e.printStackTrace();
		} finally
		{
			session.close();
		}

		return recordID;
	}
}

Open in new window

Sathish David Kumar NArchitectCommented:
May be Courses pojo in that place or .... ur scema dont have course table
zolfAuthor Commented:

colr__:

OK one step further, i added that line of code which you mentioned.now i get someother error
16:04:45,429  WARN SessionFactoryObjectFactory:438 - 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: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 com.util.HibernateUtil.<clinit>(HibernateUtil.java:14)
	at com.zolf.Main.saveRecord(Main.java:26)
	at com.zolf.Main.main(Main.java:18)

Open in new window

zolfAuthor Commented:

i checked the db and the table has been created and the value has been inserted into the table.
one thing when i set the date to 31/3/2011 in my code,but in the table it is something else.why??
Sathish David Kumar NArchitectCommented:
check the data inserted today ?
zolfAuthor Commented:

what do you mean
colr__Commented:
Sorry, just back from lunch! Is it a java.util.Date object in your code, or a String? And what is the type of the column in the database?
colr__Commented:
Also, what is the value that ends up in the database?
zolfAuthor Commented:

>>Is it a java.util.Date object in your code,
please see my code i pasted above.

>>And what is the type of the column in the database?
datetime.the table was created by hibernate,after adding the tag you mentioned in the cfg.xml file

also i get some message in my eclipse console which i paseted above.can you please tell me the reason for it
colr__Commented:
I'm not sure, but I suspect its something to do with using a Calendar - try using a Date instead
zolfAuthor Commented:

i get error if i change the Calender to Date
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.