天天看點

Java中使用Hibernate存儲Date類型及Boolean類型到Orcale資料庫中的心得

首先在Oracle中建立一個Customers的表:

create table CUSTOMERS

(

  ID         NUMBER default 0 not null,

  NAME       VARCHAR2(15) not null,

  AGE        NUMBER not null,

  IS_STUDENT VARCHAR2(1) default 1 not null,

  REGTIME    DATE

);

/

package entity; 
 
import java.io.Serializable;
 import java.util.*;public class Customers implements Serializable {
  private Integer id;
  private String name;
  private int age;
 private boolean student;
  private Date regTime; public Customers() {
  } public int getAge() {
   return age;
  } public void setAge(int age) {
   this.age = age;
  } public Integer getId() {
   return id;
  } public void setId(Integer id) {
   this.id = id;
  } public String getName() {
   return name;
  } public void setName(String name) {
   this.name = name;
  } public boolean isStudent() {
   return student;
  } public void setStudent(boolean student) {
   this.student = student;
  } public Date getRegTime() {
   return regTime;
  } public void setRegTime(Date regTime) {
   this.regTime = regTime;
  }
 } 

 為類建立對應的Mapping 檔案Customers.hbm.xml存放在src/entity目錄下,注意下面的regTime屬性的type類型為java.util.Date:

 <?xml version="1.0"?>

 <!DOCTYPE hibernate-mapping PUBLIC    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"    "
​​http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd​​" >

 <hibernate-mapping><!--     Created by the Middlegen Hibernate plugin   
​​http://boss.bekk.no/boss/middlegen/​​    
​​http://hibernate.sourceforge.net/​​-->

  <class name="entity.Customers" table="customers">

   <id name="id" type="java.lang.Integer" column="id">

    <generator class="assigned" />

   </id>

   <property name="name" type="java.lang.String" column="name" length="15" />

   <property name="age" type="java.lang.Integer" column="age" length="11" />

   <property name="student" type="java.lang.Boolean" column="IS_STUDENT" length="1" />

   <property name="regTime" type="java.util.Date" column="regTime" />

  </class>

 </hibernate-mapping> 
 
 

 Hibernate配置檔案存放在src目錄下:

 <?xml version='1.0' encoding='GB2312'?>

 <!DOCTYPE hibernate-configuration

  PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"

     "
​​http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd​​"> 
 
<hibernate-configuration>
<session-factory>
 <!-- 是否将運作期生成的SQL輸出到日志以供調試 -->
  <property name="show_sql">true</property> <!-- Oracle方言,這裡設定的是Oracle -->
  <property name="dialect">net.sf.hibernate.dialect.Oracle9Dialect</property> <!-- JDBC驅動程式 -->
  <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <!-- JDBC URL-->
  <property name="connection.url">jdbc:oracle:thin:@192.168.1.95:1521:XianBin</property> <!-- 資料庫使用者名 -->
  <property name="connection.username">system</property> <!-- 資料庫密碼 -->
  <property name="connection.password">pwd</property> <!-- 資料庫重新連接配接 -->
  <property name="connection.autoReconnect">true</property> <property name="connection.autoReconnectForPools">true</property>
 <property name="connection.is-connection-validation-required">false</property>
  <!-- 指定User的映射檔案 -->
  <mapping resource="entity/Customers.hbm.xml" />
 </session-factory>
 </hibernate-configuration>建立useHibernate.java檔案,存放在src/business目錄下:
import entity.Customers;
 import net.sf.hibernate.Session;
 import net.sf.hibernate.SessionFactory;
 import net.sf.hibernate.Transaction;
 import net.sf.hibernate.cfg.Configuration;public class useHibernate {
  public static SessionFactory sessionFactory; // 初始化Hibernate,建立SessionFactory執行個體 public void saveCustomers(Customers customers) throws Exception {
   Configuration config = null;
   config = new Configuration().configure(); // 建立一個SessionFactory 執行個體
   sessionFactory = config.buildSessionFactory();
   Session session = sessionFactory.openSession();
   Transaction tx = null;
   try {
    /* 開始一個事務 */
    tx = session.beginTransaction();
    session.save(customers); /* 送出事務 */
    tx.commit();
   } catch (Exception e) { // TODO Auto-generated catch block
    if (tx != null)
     tx.rollback();
    throw e;
   } finally {
    session.close();
   }
  }
 } 

 package business;