-->
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: I cannot insert record into database by using Hibernate...
PostPosted: Mon Sep 28, 2009 10:58 am 
Newbie

Joined: Sat Sep 19, 2009 12:25 am
Posts: 7
Hi all,

I am having problem in inserting record into MySQL database by using Hibernate. There is no error message. Dont know what happened. I really approciate if anyone can give me some idea on how to solve the problem. Thanks :)

Below is my source code.
Course.java
Code:
package com.vaannila.course;
public class Course {

   private int courseId;
   private String courseName;
   
   public Course(){
      
   }
   
   public Course(String courseName){
      this.courseName = courseName;
   }
   
   public int getCourseId(){
      return this.courseId;
   }
   
   public void setCourseId(int courseId) {
      this.courseId = courseId;
   }
   
   public String getCourseName() {
      return this.courseName;
   }
   
   public void setCourseName(String courseName) {
      this.courseName = courseName;
   }   
}


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="connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="connection.url">jdbc:mysql://localhost:3306/tutorial</property>
      <property name="connection.username">root</property>
      <property name="connection.password">password</property>
      <property name="connection.pool_size">1</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="show_sql">true</property>
      <property name="hbm2ddl.auto">create</property>
      <mapping resource="com/vaannila/course/Course.hbm.xml"/>
   </session-factory>
</hibernate-configuration>


Course.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!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.vaannila.course.Course" table="COURSES">
        <id name="courseId" type="integer" column="COURSE_ID"></id>
      <property name="courseName" type="string" column="COURSE_NAME" />
   </class>
</hibernate-mapping>


HibernateUtil.java
Code:
package com.vaannila.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
   private static final SessionFactory sessionFactory;
   static {
      try {
         sessionFactory = new Configuration().configure().buildSessionFactory();
      }catch (Throwable ex) {
         System.err.println("Initial SessionFactory creation failed." + ex);
         throw new ExceptionInInitializerError(ex);
      }
   }
   
   public static SessionFactory getSessionFactory() {
      return sessionFactory;
   }
}


Main.java
Code:
package com.vaannila.course;

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

import com.vaannila.util.HibernateUtil;

public class Main {
   public static void main(String[] args) {
      Main obj = new Main();
      obj.saveCourse("Physics");
      obj.saveCourse("Chemistry");
      obj.saveCourse("Maths");
   }
   
   public void saveCourse(String courseName){
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction transaction = null;
      try {
         System.out.println("Checkpoint1");
         transaction = session.beginTransaction();
         System.out.println("Checkpoint2");
         Course course = new Course();
         course.setCourseName(courseName);
         System.out.println("Checkpoint3");
         transaction.commit();
         System.out.println("Checkpoint4");
      }catch (HibernateException e) {
         System.out.println("Checkpoint5");
         transaction.rollback();
         System.out.println("Checkpoint6");
         e.printStackTrace();
      }finally {
         session.close();
      }
   }
}


Checkpoint1,Checkpoint2,Checkpoint3,Checkpoint4 is displayed in the console. There is no error message, but the COURSE is still empty.

Below is my table structure in MySQL:
table name: COURSES
column:
COURSE_ID (INT(11), PRIMARY KEY, AUTO-INCREMENT, NOT NULL)
COURRSE_NAME (VARCHAR(225) )


Top
 Profile  
 
 Post subject: Re: I cannot insert record into database by using Hibernate...
PostPosted: Mon Sep 28, 2009 3:11 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi suigion,

you need to tell Hibernate to actually save your entity.
This ...
Code:
...
         course.setCourseName(courseName);
         System.out.println("Checkpoint3");
         session.save(course);
         transaction.commit();
         System.out.println("Checkpoint4");
         ...

... should do the trick.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: I cannot insert record into database by using Hibernate...
PostPosted: Wed Sep 30, 2009 9:55 am 
Newbie

Joined: Sat Sep 19, 2009 12:25 am
Posts: 7
hi Froestel,

Thanks for the reply. It works.. :)


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.