Link to home
Create AccountLog in
Avatar of Zolf
ZolfFlag for United Arab Emirates

asked on

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

Avatar of colr__
colr__

Does the table 'COURSES' definetly exist in your database?
Avatar of Zolf

ASKER


no i was expecting hibernate to create that for me
Avatar of Gurvinder Pal Singh
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?
Avatar of Zolf

ASKER


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

Avatar of Zolf

ASKER


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

ASKER CERTIFIED SOLUTION
Avatar of colr__
colr__

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
can you show the query also?

you need to use com.zolf.Course in the query in place of COURSES
Avatar of Zolf

ASKER


can you please tell me more specific,since i am new to hibernate
Avatar of Zolf

ASKER


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

May be Courses pojo in that place or .... ur scema dont have course table
Avatar of Zolf

ASKER


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

Avatar of Zolf

ASKER


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??
check the data inserted today ?
Avatar of Zolf

ASKER


what do you mean
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?
Also, what is the value that ends up in the database?
Avatar of Zolf

ASKER


>>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
I'm not sure, but I suspect its something to do with using a Calendar - try using a Date instead
Avatar of Zolf

ASKER


i get error if i change the Calender to Date