こんにちは、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();
}
}
}
誰かがこの問題を知っているなら、これから私を助けてください...ありがとう、