现在的位置: 首页 > 综合 > 正文

Java中使用Hibernate存储Date类型及Boolean类型到Orcale数据库中的心得

2013年10月10日 ⁄ 综合 ⁄ 共 3736字 ⁄ 字号 评论关闭

首先在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
);
/


接着编写对应的类Customers.java存放在src/entity目录下,注意下面的类中使用的Date类必须是在java.util.*包内,否则将会引起错误,另外boolean型的私有成员的命名不可以以is开头,否则将引发错误,比如isStudent:
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;

【上篇】
【下篇】

抱歉!评论已关闭.