您好我已经使用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.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();
}
}
}
如果有人知道这个问题那么请帮我解决这个问题...... 谢谢,
答案 0 :(得分:2)
您的配置大多正确。我只发现你在hibernate.cfg.xml中有两次current_session_context_class线程
我尝试使用MySQL的源代码并且它可以工作,因此问题与HSQLDB有关,而与代码无关。
看到这个答案:
答案 1 :(得分:1)
在应用程序退出之前关闭数据库吗?请检查此答案:Data is not saving in MEMORY Table of HSQL。