因为Oracle10g中并不提供字段类型为boolean的属性,所以我们需要通过使用char或number来代替!接下来看一个例子。
Person类
import java.sql.Timestamp;
public class Person{
 private int PId;
 private String PGender;
 private Timestamp PGraduate;
 private byte[] PFile;
 private boolean isMan; 
 public boolean getIsMan() {
  return isMan;
 }
 public void setIsMan(boolean isMan) {
  this.isMan = isMan;
 }
 public int getPId() {
  return PId;
 } 
 public void setPId(int pId) {
  PId = pId;
 }
 public String getPGender() {
  return PGender;
 }
 public void setPGender(String pGender) {
  PGender = pGender;
 }
 public Timestamp getPGraduate() {
  return PGraduate;
 }
 public void setPGraduate(Timestamp pGraduate) {
  PGraduate = pGraduate;
 }
 public byte[] getPFile() {
  return PFile;
 }
 public void setPFile(byte[] pFile) {
  PFile = pFile;
 }
}
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          " http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>
 <session-factory>
  <property name="hbm2ddl.auto">update</property>
  <property name="dialect">
   org.hibernate.dialect.Oracle9Dialect
  </property>
  <property name="connection.url">
   jdbc:oracle:thin:@localhost:1521:Eric
  </property>
  <property name="connection.username">jinhua</property>
  <property name="connection.password">jinhua</property>
  <property name="connection.driver_class">
   oracle.jdbc.driver.OracleDriver
  </property>
  <property name="myeclipse.connection.profile">oracle</property>
  <property name="show_sql">true</property>
  <mapping resource="Person.hbm.xml" />
 </session-factory>
</hibernate-configuration>
Person.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">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.zhanlang.bean.Person"  table="person">
        <id name="PId" type="int" column="P_ID">
            <generator class="increment"></generator>
        </id>
        <property name="PGender" type="string" column="P_GENDER" />
        <property name="PGraduate" type="timestamp" column="P_GRADUATE" />
        <property name="PFile" type="binary" column="P_FILE" />
        <property name="isMan" type="boolean" column="isMan"/>
    </class>
</hibernate-mapping>
person表
create table person(
   p_id number(4) primary key,
   p_gender varchar(10) check(p_gender in ('男','女')),
   p_graduate Timestamp(4),
   p_file blob not null
)
alter table person add isMan number(1);
alter table person add constraint isMan_check check(isMan in(0,1));
主函数:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Date;
import java.sql.Timestamp;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class App {
 private static SessionFactory sessionFactory;
 static{
  try {
   sessionFactory=new Configuration().configure().buildSessionFactory();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 /**
  * @param args
  * @throws FileNotFoundException
  */
 public static void main(String[] args) throws Exception {
  // TODO Auto-generated method stub
  Person person=new Person();
  Timestamp  time=new Timestamp(new java.util.Date().getTime());
  System.out.println(time);
  Date graduate =new Date(new java.util.Date().getTime());
  System.out.println(graduate);
  InputStream is=new FileInputStream("c:/sava.txt");
  int length=is.available();
  byte[] buffer=new byte[length];
  is.read(buffer);
  
  person.setPGraduate(time);
  person.setPFile(buffer);
  person.setPGender("男");
  person.setIsMan(true);
  save(person);
 } 
 public static boolean save(Person person)
 {  
  Session session=sessionFactory.openSession();
  Transaction tx=session.beginTransaction();
  try {
   session.save(person);
   tx.commit();
  } catch (Exception e) {
   if(tx!=null)
   {
    tx.rollback();
   }
   e.printStackTrace();
  }finally
  {
   session.close();
  }
  return true;
 }
}
查询:
select * from person;
--当hibernate检索到用户输入的为true时,自动转换为1,false为0!而下面的decode方法则可以将1、0转为true、false
select p_id,p_gender,p_graduate,p_file,decode(isMan, 1,'true',0,'false') as isMan from person;