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