-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Can't view data inserted by Hibernate
PostPosted: Tue Dec 07, 2010 3:27 pm 
Newbie

Joined: Tue Dec 07, 2010 3:16 pm
Posts: 3
Hello,

I am learning hibernate and followed a simple example for inserting, updating, deleting, and selecting records. Everything appears to function properly, but when I load the database file with an external SQL client and do a select on the table, it returns 0 results.

Here is my hibernate.cfg.xml

Code:
<?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>
  <property name="hibernate.connection.driver_class">org.h2.Driver</property>
  <property name="hibernate.connection.url">jdbc:h2:course</property>
  <property name="hibernate.connection.username">sa</property>
  <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
  <property name="show_sql">true</property>
  <property name="hbm2ddl.auto">create</property>
  <mapping resource="com/vaanila/course/Course.hbm.xml"/>
</session-factory>
</hibernate-configuration>


Here is my class mapping file

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.vaanila.course.Course" table="COURSES">
        <meta attribute="class-description">
            This class contains the course details.
        </meta>
        <id name="courseId" type="long" column="COURSE_ID">
            <generator class="native"/>
        </id>
        <property name="courseName" type="string" column="COURSE_NAME" not-null="true" />
    </class>

</hibernate-mapping>


And here is the file I use to run everything

Code:
package com.vaanila.course;

import java.util.List;
import java.util.Iterator;

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

import com.vaanila.util.HibernateUtil;

public class Main {
   
   public static void main(String[] args) {
      Main obj = new Main();
      Long courseId1 = obj.saveCourse("Physics");
      System.out.println(courseId1);
      Long courseId2 = obj.saveCourse("Chemistry");
      System.out.println(courseId2);      
      Long courseId3 = obj.saveCourse("Maths");
      System.out.println(courseId3);
      obj.listCourse();
      obj.updateCourse(courseId3, "Mathematics");
      obj.deleteCourse(courseId2);
      obj.listCourse();
   }
   
   public Main() {
      
   }
   
   public Long saveCourse(String courseName) {
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction transaction = null;
      Long courseId = null;
      try {
         transaction = session.beginTransaction();
         Course course = new Course();
         course.setCourseName(courseName);
         courseId = (Long) session.save(course);
         transaction.commit();
      }
      catch (HibernateException e) {
         transaction.rollback();
         e.printStackTrace();
      }
      finally {
         session.close();
      }
      return courseId;
   }
   
   public void listCourse() {
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction transaction = null;
      try {
         transaction = session.beginTransaction();
         List courses = session.createQuery("from Course").list();
         for (Iterator iterator = courses.iterator(); iterator.hasNext();) {
            Course course = (Course) iterator.next();
            System.out.println(course.getCourseId() + "::" + course.getCourseName());
         }
         transaction.commit();
      }
      catch (HibernateException e) {
         transaction.rollback();
         e.printStackTrace();
      }
      finally {
         session.close();
      }
   }
   
   public void updateCourse(Long courseId, String courseName) {
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction transaction = null;
      try {
         transaction = session.beginTransaction();
         Course course = (Course) session.get(Course.class, courseId);
         course.setCourseName(courseName);
         transaction.commit();
      }
      catch (HibernateException e) {
         transaction.rollback();
         e.printStackTrace();
      }
      finally {
         session.close();
      }
   }
   
   public void deleteCourse(Long courseId) {
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction transaction = null;
      try {
         transaction = session.beginTransaction();
         Course course = (Course) session.get(Course.class, courseId);
         session.delete(course);
         transaction.commit();
      }
      catch (HibernateException e) {
         transaction.rollback();
         e.printStackTrace();
      }
      finally {
         session.close();
      }
   }

}


Here is a sample of the console output when I run the file:

Code:
Hibernate: insert into COURSES (COURSE_ID, COURSE_NAME) values (null, ?)
1
Hibernate: insert into COURSES (COURSE_ID, COURSE_NAME) values (null, ?)
2
Hibernate: insert into COURSES (COURSE_ID, COURSE_NAME) values (null, ?)
3
Hibernate: select course0_.COURSE_ID as COURSE1_0_, course0_.COURSE_NAME as COURSE2_0_ from COURSES course0_
1::Physics
2::Chemistry
3::Maths
Hibernate: select course0_.COURSE_ID as COURSE1_0_0_, course0_.COURSE_NAME as COURSE2_0_0_ from COURSES course0_ where course0_.COURSE_ID=?
Hibernate: update COURSES set COURSE_NAME=? where COURSE_ID=?
Hibernate: select course0_.COURSE_ID as COURSE1_0_0_, course0_.COURSE_NAME as COURSE2_0_0_ from COURSES course0_ where course0_.COURSE_ID=?
Hibernate: delete from COURSES where COURSE_ID=?
Hibernate: select course0_.COURSE_ID as COURSE1_0_, course0_.COURSE_NAME as COURSE2_0_ from COURSES course0_
1::Physics
3::Mathematics


At first, I thought perhaps the program was working in-memory only and not persisting the data to file, but I have my H2 connection set up to write to file. When the code runs for the first time and the database file does not exist, it is created on disk. Also, if I disable the hbm2ddl.auto property, and run the code multiple times, the id increments properly as follows:

Code:
Hibernate: insert into COURSES (COURSE_ID, COURSE_NAME) values (null, ?)
4
Hibernate: insert into COURSES (COURSE_ID, COURSE_NAME) values (null, ?)
5
Hibernate: insert into COURSES (COURSE_ID, COURSE_NAME) values (null, ?)
6
Hibernate: select course0_.COURSE_ID as COURSE1_0_, course0_.COURSE_NAME as COURSE2_0_ from COURSES course0_
1::Physics
3::Mathematics
4::Physics
5::Chemistry
6::Maths
Hibernate: select course0_.COURSE_ID as COURSE1_0_0_, course0_.COURSE_NAME as COURSE2_0_0_ from COURSES course0_ where course0_.COURSE_ID=?
Hibernate: update COURSES set COURSE_NAME=? where COURSE_ID=?
Hibernate: select course0_.COURSE_ID as COURSE1_0_0_, course0_.COURSE_NAME as COURSE2_0_0_ from COURSES course0_ where course0_.COURSE_ID=?
Hibernate: delete from COURSES where COURSE_ID=?
Hibernate: select course0_.COURSE_ID as COURSE1_0_, course0_.COURSE_NAME as COURSE2_0_ from COURSES course0_
1::Physics
3::Mathematics
4::Physics
6::Mathematics


Despite all this, when I open the H2 db file with an external editor, a select * from COURSES turns up empty. The courses table is created, the column attributes are all there and correctly configured by Hibernate, but there is no data.????


Top
 Profile  
 
 Post subject: Re: Can't view data inserted by Hibernate
PostPosted: Thu Dec 09, 2010 10:49 am 
Newbie

Joined: Mon Nov 19, 2007 1:57 pm
Posts: 14
If run Main only with one uncommented line
Code:
obj.listCourse();

data exist?


Top
 Profile  
 
 Post subject: Re: Can't view data inserted by Hibernate
PostPosted: Fri Dec 10, 2010 8:55 am 
Beginner
Beginner

Joined: Fri Nov 14, 2008 7:34 pm
Posts: 24
run H2 in server mode


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.