1

您好,我已经使用 hibernate 和 hsqldb 创建了一个示例应用程序。

当我运行我的应用程序时,它工作正常并且没有给出任何错误,它为我的表提供了以下确认。

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

但是当我检查表格时,它没有任何新输入的记录。

以下是我的应用程序的代码。

休眠.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 文件

<?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;
}

}

主类 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();
    }

}

 }

如果有人知道这个问题,那么请帮我解决这个问题......谢谢,

4

2 回答 2

2

您的配置大部分是正确的。我只发现你在 hibernate.cfg.xml 中有两次 current_session_context_class 线程

我用 MySQL 尝试了你的源代码,它可以工作,所以问题与 HSQLDB 有关,而不是与你的代码有关。

看到这个答案:

HSQLDB 和 Hibernate/JPA - 不持久化到磁盘?

于 2012-04-28T12:39:49.860 回答
1

您是否在应用程序退出之前关闭数据库?检查这个答案:数据没有保存在 HSQL 的 MEMORY 表中

于 2012-04-28T12:05:33.827 回答