单类包含是指一个类是另一个类的成员变量,比如有这样两个类,个人(Person)和地址(Addr),Addr是Person的成员变量,类图如下:
两个类分别对应数据库中的Persons和Addrs表,它们的ER图如下:
具体怎么把OO对象和具体的数据库实体表无缝联系起来呢,下面的代码展示了如何把两个类映射到数据库中的表.
Person类代码:
package com.sitinspring.personaddress;
public class Person{
private long id;
private long aid;
private String name;
private Addr addr;
public Person(){
}
public Person(long id,long aid,String name){
this.id=id;
this.aid=aid;
this.name=name;
}
public String toString(){
return "Person id="+id+" aid="+aid+" name="+name+addr;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Addr getAddr() {
return addr;
}
public void setAddr(Addr addr) {
this.addr = addr;
}
public long getAid() {
return aid;
}
public void setAid(long aid) {
this.aid = aid;
}
}
相应的Person.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.auction">
<class name="com.sitinspring.personaddress.Person"
table="PERSONS">
<id name="id" column="ID" />
<property name="name" column="NAME" />
<many-to-one name="addr" column="AID"
class="com.sitinspring.personaddress.Addr" cascade="all" lazy="false"/>
</class>
</hibernate-mapping>
Addr类代码;
package com.sitinspring.personaddress;
public class Addr{
private long id;
private String phAddr;
private String mailAddr;
public Addr(){
}
public Addr(String phAddr,String mailAddr){
this.phAddr=phAddr;
this.mailAddr=mailAddr;
}
public String toString(){
return "Addr id="+id+" phAddr="+phAddr+" mailAddr="+mailAddr;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getMailAddr() {
return mailAddr;
}
public void setMailAddr(String mailAddr) {
this.mailAddr = mailAddr;
}
public String getPhAddr() {
return phAddr;
}
public void setPhAddr(String phAddr) {
this.phAddr = phAddr;
}
}
Addr.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.auction">
<class name="com.sitinspring.personaddress.Addr" table="ADDRS">
<id name="id" column="ID"/>
<property name="phAddr" column="PHADDR" />
<property name="mailAddr" column="MAILADDR" />
</class>
</hibernate-mapping>
测试代码如下:
package com.sitinspring.personaddress;
import org.hibernate.Session;
import com.sitinspring.util.HibernateUtil;
public class Main {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
// --插入代码开始
Person person=new Person(1,11,"Andy");
person.setAddr(new Addr("Baijing Street","Andy@Andy.com"));
session.save(person);
session.flush();
// --插入代码结束
// --读取代码开始
long i = 1;
Person personLoaded = (Person) session.load(Person.class, i);
System.out.println(personLoaded);
// --读取代码开始
session.getTransaction().commit();
HibernateUtil.getSessionFactory().close();
}
}
插入完毕后数据库的内容如下:
代码下载:
http://www.blogjava.net/Files/sitinspring/HibernateClasses20071109085612.rar