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;
}
}
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<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" />
<!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>
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 constraint isMan_check check(isMan in(0,1));
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Date;
import java.sql.Timestamp;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
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
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);
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 p_id,p_gender,p_graduate,p_file,decode(isMan, 1,'true',0,'false') as isMan from person;