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?
 
colr__Commented:
Try adding this to your config file:

            <property name="hibernate.hbm2ddl.auto" value="create"/>
0
 
colr__Commented:
Does the table 'COURSES' definetly exist in your database?
0
 
zolfAuthor Commented:

no i was expecting hibernate to create that for me
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
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?
0
 
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

0
 
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

0
 
Gurvinder Pal SinghCommented:
can you show the query also?

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

can you please tell me more specific,since i am new to hibernate
0
 
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

0
 
Sathish David Kumar NArchitectCommented:
May be Courses pojo in that place or .... ur scema dont have course table
0
 
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

0
 
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??
0
 
Sathish David Kumar NArchitectCommented:
check the data inserted today ?
0
 
zolfAuthor Commented:

what do you mean
0
 
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?
0
 
colr__Commented:
Also, what is the value that ends up in the database?
0
 
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
0
 
colr__Commented:
I'm not sure, but I suspect its something to do with using a Calendar - try using a Date instead
0
 
zolfAuthor Commented:

i get error if i change the Calender to Date
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.