环境: eclipse+myeclipse+mysql.
(1) 建表脚本
--刪除表
drop table if exists address ;
drop table if exists person ;
--創建表
create table person
(
id int not null auto_increment primary key ,
name varchar(20) not null,
age int
) ;
create table address
(
id int ,
post int ,
adr varchar(20) not null,
foreign key (id) references person(id) on delete cascade
) ;
--事務提交
commit ;
(2) Person.java的编写.
package org.hibernate.listDemo.vo;
import java.util.List;
public class Person {
private int id ;
private String name ;
private int age ;
private List address ;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getAddress() {
return address;
}
public void setAddress(List address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
(3) 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.hibernate.listDemo.vo.Person" table="person">
<id name="id" type="java.lang.Integer">
<column name="id"></column>
<generator class="increment"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="name"></column>
</property>
<property name="age" type="java.lang.Integer">
<column name="age"></column>
</property>
<list name="address" table="address" lazy="false">
<key column="id"></key>
<index column="post"></index>
<element type="java.lang.String">
<column name="adr"></column>
</element>
</list>
</class>
</hibernate-mapping>
<4> 測試類的編寫.
package org.hibernate.listDemo.dao;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Expression;
import org.hibernate.listDemo.vo.Person;
public class PersonDao {
private Session session ;
public PersonDao()
{
session = new Configuration().configure()
.buildSessionFactory().openSession() ;
}//end constructor
public void insert(Person p)
{
session.save(p) ;
session.beginTransaction().commit() ;
session.close() ;
}
public void update(Person p)
{
session.update(p) ;
session.beginTransaction().commit() ;
session.close() ;
}
public void deletebyId(int id)
{
String hql = "delete from Person as p where p.id=?" ;
Query q = session.createQuery(hql) ;
q.setInteger(0, id) ;
q.executeUpdate() ;
session.beginTransaction().commit() ;
session.close() ;
}
public Person findById(int id)
{
Criteria c = session.createCriteria(Person.class) ;
c.add(Expression.eq("id",id)) ;
List l = c.list() ;
if(l.size()>0)
return (Person)l.get(0) ;
return null ;
}
public static void main(String[] args)
{
PersonDao pd = new PersonDao() ;
//插入
// Person p = new Person() ;
// p.setName("zhangsan") ;
// p.setAge(10) ;
// p.setAddress(new ArrayList()) ;
// List l = p.getAddress() ;
//根據list的特性,可以插入相同的對象.
// l.add("guangdong") ;
// l.add("guangdong") ;
// l.add("shenzhen") ;
// l.add("shenzhen") ;
// p.setAddress(l) ;
// pd.insert(p) ;
//更新
// Person p = pd.findById(1) ;
// System.out.println("ddddddddddddd") ;
// p.setName("lisi") ;
// p.setAge(33) ;
// List l = p.getAddress() ;
// l.add("dddddddddd") ;
// l.add("dddddddddd") ;
// p.setAddress(l) ;
// pd.update(p) ;
//刪除. 級連刪除.
pd.deletebyId(1) ;
}//end mehotd main
}//end class PersonDao
注:此處免去了Hibernate.cfg.xml的編寫.