Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate 冬眠不';不要在数据库中保存任何记录_Hibernate_Hsqldb - Fatal编程技术网

Hibernate 冬眠不';不要在数据库中保存任何记录

Hibernate 冬眠不';不要在数据库中保存任何记录,hibernate,hsqldb,Hibernate,Hsqldb,您好,我已经使用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,

您好,我已经使用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..
但是当我检查表时,它没有任何新输入的记录

以下是我的申请代码

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

}

 }
如果有人知道这个问题,请帮我解决这个问题。。。
谢谢,

在应用程序退出之前是否关闭数据库?检查以下答案:。

您的配置基本正确。我只发现您在hibernate.cfg.xml中有两次当前会话上下文类线程

我用MySQL尝试了您的源代码,它可以正常工作,因此问题与HSQLDB有关,而与您的代码无关

看看这个答案:

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

}

 }