日期:2014-05-16  浏览次数:20846 次

使用hibernate插入、更新数据

首先创建表 person


drop table person

create table person(
?? id varchar(32) not null primary key,
?? name varchar(20) not null,
?? password varchar(20) not null,
?? sex varchar(20) ,
?? email varchar(30)
)
?

?

public class Person { //POJO类   
    private String id;  
    private String name;  
    private String password;  
    private String sex;  
    private String email;  
      
    public String getId() {  
        return id;  
    }  
    public void setId(String id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public String getPassword() {  
        return password;  
    }  
    public void setPassword(String password) {  
        this.password = password;  
    }  
    public String getSex() {  
        return sex;  
    }  
    public void setSex(String sex) {  
        this.sex = sex;  
    }  
    public String getEmail() {  
        return email;  
    }  
    public void setEmail(String email) {  
        this.email = email;  
    }  
  
}  

?

测试类:

?

public class PersonOperator {  
    private Session session = null;  
    public PersonOperator() {  
        Configuration config = new Configuration();  
        SessionFactory factory = config.configure().buildSessionFactory();  
        this.session = factory.openSession();  
    }  
    // 所有的操作都是通过session来完成的   
    public void PersonInsert(Person p) {  
        Transaction tran = this.session.beginTransaction();//开始事物   
        this.session.save(p);//执行   
        tran.commit();//提交   
    }  
     public static void main(String[] args) {  
          
        Person p = new Person();  
        p.setId("1");  
        p.setName("jack");  
        p.setPassword("123456");  
        p.setSex("male");  
        p.setEmail("123@123.com");  
          
        //通过实例化的Person对象插入数据   
        PersonOperator po = new PersonOperator();  
        po.PersonInsert(p);  
  
    }  

}  

?配置文件 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="org.lz.hibernate.Person" table="PERSON" schema="SCOTT">
<id name="id" type="java.lang.String">
??? <column name="ID" length="32" />
?? ? <generator class="assigned" />
</id>
<property name="name" type="java.lang.String" column="NAME" />

<property name="password" type="java.lang.String" column="PASSWORD/>

<property name="sex" type="java.lang.String" column="SEX"/>

<property name="email" type="java.lang.String" column="EMAIL"/>
</class>
</hibernate-mapping>

?

注意,其中id是表的主键,?? ? <generator class="assigned" / > 是声明主键的生成方式的

assigned 表示由外部程序生成,即我们附进去值。还有 increment(自增模式)、identity(采用数据库提供的主键生成模式)等

?

最后在cfg配置文件中要加入 <mapping resource="person.hbm.xml" /> 这一行

写道
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.source