1

Hello i have created a sample application using hibernate with hsqldb.

When i run my application it works fine and doesn't give any error,it gives the following acknowledgement for my tables.

Inserting Record
Hibernate: insert into SC_EMPLOYEE.tbl_test (name, id) values (?, ?)
Hibernate: insert into SC_EMPLOYEE.TBL_EMPLOYEE (EMP_NAME, EMP_ADDRESS, EMP_EMAIL,    EMP_PHONE, EMP_SALARY, EMP_ID) values (?, ?, ?, ?, ?, ?)
Record inserted successfully..

But when i am checking the tables it doesn't have any newly entered records.

following is the code of my application.

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>
<!--Database Connection Settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:file:i:/DATABASE/Employee/db_emp</property>
<property name="hibernate.default_schema">SC_EMPLOYEE</property>
<property name="connection.username">SA</property>
<property name="connection.password"></property>
<property name = "current_session_context_class">thread</property> 
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache  -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<mapping resource="cls_employee.hbm.xml"/>   
</session-factory>

</hibernate-configuration>

cls.employee.hbm.xml file

<?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="sample_hibernate.cls_employee" table="TBL_EMPLOYEE">
<id name="emp_id" type="string" column="EMP_ID">
    <generator class="assigned"/>
</id>
<property name="emp_name" type="string" column="EMP_NAME"/>
<property name="emp_address" type="string" column="EMP_ADDRESS"/>
<property name="emp_email" type="string" column="EMP_EMAIL"/>
<property name="emp_phone" type="string" column="EMP_PHONE"/>
<property name="emp_salary" type="string" column="EMP_SALARY"/>

</class>

</hibernate-mapping>

cls_employee.java

 public class cls_employee 
 {

private String emp_id;
private String emp_name;
private String emp_address;
private String emp_phone;
private String emp_email;
private String emp_salary;

public String getEmp_id() {
    return emp_id;
}
public void setEmp_id(String emp_id) {
    this.emp_id = emp_id;
}
public String getEmp_name() {
    return emp_name;
}
public void setEmp_name(String emp_name) {
    this.emp_name = emp_name;
}
public String getEmp_address() {
    return emp_address;
}
public void setEmp_address(String emp_address) {
    this.emp_address = emp_address;
}
public String getEmp_phone() {
    return emp_phone;
}
public void setEmp_phone(String emp_phone) {
    this.emp_phone = emp_phone;
}
public String getEmp_email() {
    return emp_email;
}
public void setEmp_email(String emp_email) {
    this.emp_email = emp_email;
}
public String getEmp_salary() {
    return emp_salary;
}
public void setEmp_salary(String emp_salary) {
    this.emp_salary = emp_salary;
}

}

Main Class cls_main.java

 package sample_hibernate;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.Session;

public class cls_main {

/**
 * @param args
 */
public static void main(String[] args) 
{
    Transaction transaction = null;
    Session session =null;
    try
    {
        SessionFactory session_factory = new  Configuration().configure().buildSessionFactory();
     session = session_factory.openSession();

         transaction = session.beginTransaction();  

        System.out.println("Inserting Record");
        cls_employee obj_employee = new cls_employee();
        obj_employee.setEmp_id("2");
        obj_employee.setEmp_name("raj");
        obj_employee.setEmp_address("surat");
        obj_employee.setEmp_email("raj@gmail.com");
        obj_employee.setEmp_phone("9979378641");



        cls_tbl_test test_obj = new cls_tbl_test();
        test_obj.setId("1");
        test_obj.setName("test");

        session.save(test_obj);
         session.save(obj_employee);

         session.flush();
         session.clear();


         transaction.commit();

        System.out.println("Record inserted successfully..");

    }catch(HibernateException e)
    {
        System.out.println(e.getMessage());
    }
    finally
    {
         if (!transaction.wasCommitted()) {
             transaction.rollback();
            }

        session.close();
    }

}

 }

If any one knows this issue then please help me out of this... Thanks,

Rahul Gamit
  • 11
  • 1
  • 4

2 Answers2

2

Your configuration is mostly correct. I only spot that you have current_session_context_class thread twice in hibernate.cfg.xml

I tried your source code with MySQL and it works, so the issue is related to HSQLDB and not to your code.

See this answer:

HSQLDB and Hibernate/JPA - not persisting to disk?

Community
  • 1
  • 1
stivlo
  • 83,644
  • 31
  • 142
  • 199
1

Do you close the database before the application exits? Check this answer: Data is not saving in MEMORY Table of HSQL.

Community
  • 1
  • 1
palacsint
  • 28,416
  • 10
  • 82
  • 109