• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

org.hibernate.exception.SQLGrammarException: could not insert: [com.Course]

 
Vick Chak
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Stuck with this error for the past three days.

org.hibernate.exception.SQLGrammarException: could not insert: [com.Course]
at org.hibernate.exception.CacheSQLStateConverter.convert(CacheSQLStateConverter.java:89)
............
at com.Main.saveCourse(Main.java:43)
at com.Main.main(Main.java:19)
Caused by: java.sql.SQLException: [SQLCODE: <-30>:<Table or View not found>]
[Cache Error: <<SYNTAX>errdone+2^%qaqqt>]
[Details: <Prepare>]
[%msg: < SQL ERROR #30: Table 'SQLUSER.COURSE' not found>]
at com.intersys.jdbc.CacheConnection.getServerError(CacheConnection.java:1057)

Code provided below. Using intersystems cache

hibernate.cfg.xml

<hibernate-configuration>
<session-factory name="SessionFactory">
<property name="hibernate.connection.driver_class">com.intersys.jdbc.CacheDriver</property>
<property name="hibernate.connection.password">SYS</property>
<property name="hibernate.connection.url">jdbc:Cache://127.0.0.1:1972/USER</property>
<property name="hibernate.connection.username">_SYSTEM</property>
<property name="hibernate.dialect">org.hibernate.dialect.Cache71Dialect</property>
<mapping resource="com/Course.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Course.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jun 4, 2011 11:09:14 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.Course" table="SQLUSER.COURSE">(Have tried with COURSE as well)
<id name="courseId" type="long">
<column name="COURSEID" />
<generator class="assigned" />
</id>
<property name="courseName" type="java.lang.String">
<column name="COURSENAME" />
</property>
</class>
</hibernate-mapping>

Main.java

public static void main(String[] args) {
// TODO Auto-generated method stub
Main obj=new Main();
System.out.println("main");
obj.saveCourse("Physics");
obj.saveCourse("Chemistry");
obj.saveCourse("Maths");
}
public void saveCourse(String subject)
{
Session sess=HibernateUtil.getSessionFactory().openSession();
Transaction trans= null;
Long courseId=null;
try
{
trans=sess.beginTransaction();
trans.begin();
Course course=new Course();
course.setCourseName(subject);
courseId= (Long)sess.save(course);
trans.commit();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
sess.close();
}
}

Would be very helpful if someone could point out the issue in the above code.

Thanks.
Vickram
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vick Chak,

problem might be because you are using generator class as "assigned", but not setting the primary key in your.

here is the reference link.


Hope this Help,
Hemant
 
Vick Chak
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I'll try that. But when I created the table course explicitly using a query in the database, it inserted fine.Seems like I need to write explicit code for creating the table and then call session.save method?
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Vick,

That may be because you have used auto-increment setting in your DB.
but when you use generator class = "assigned", hibernate expects you to set id on your bean class while saving.

try setting generator class to native and post result.


Regards,
Hemant
 
Waswani Naresh
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vick,

Could you please paste the stack trace after configuring table as table="COURSE" instead of table="SQLUSER.COURSE". And also please paste the entire code for Main.java.

Regards,
Naresh Waswani
 
in Mkumar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dude,

Since your genegrator is assigned hence you need to set it's value explictly which is missing in your code base below.
So, set value like obj.setCourseId(1111); make sure COURSEID is declared in smaller in your pojo class. I am sure it will resolve the issue.



<id name="courseId" type="long">
<column name="COURSEID" />
<generator class="assigned" />
</id>


Your code -

Main obj=new Main();
System.out.println("main");
obj.saveCourse("Physics");
obj.saveCourse("Chemistry");
obj.saveCourse("Maths");

 
Shiva hari kumar Bala
Greenhorn
Posts: 3
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try setting this property in you .cfg file

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic