类集合包含意味着一个类中的成员变量是另一个类的集合,比如说公司类Company包含成员类Member的集合.
类图如下:
它们分别对应数据库中的Companys表和Members表,它们的ER图如下:
以下代码演示了如何将类与数据库映射起来:
Company类代码:
package com.sitinspring.companymember;
import java.util.HashSet;
import java.util.Set;
public class Company{
private long id;
private String name;
private Set<Member> members = new HashSet<Member>();
public Company(){
}
public Company(long id,String name){
this.id=id;
this.name=name;
}
public void add(Member member){
member.setCid(id);
members.add(member);
}
public String toString(){
String retval="Company id="+id+" name="+name;
for(Member member:members){
retval+="\n"+member;
}
return retval;
}
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 Set<Member> getMembers() {
return members;
}
public void setMembers(Set<Member> members) {
this.members = members;
}
}
Company.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.companymember.Company" table="COMPANYS">
<id name="id" column="ID"/>
<property name="name" column="NAME" />
<set name="members" inverse="true" cascade="all" lazy="false">
<key column="CID" />
<one-to-many class="com.sitinspring.companymember.Member" />
</set>
</class>
</hibernate-mapping>
Member类代码:
package com.sitinspring.companymember;
public class Member {
private long id;
private long cid;
private String name;
public Member() {
}
public Member(long id, String name) {
this.id = id;
this.name = name;
}
public String toString(){
return "Member id="+id+" cid="+cid+" name="+name;
}
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 long getCid() {
return cid;
}
public void setCid(long cid) {
this.cid = cid;
}
}
Member.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.companymember.Member" table="MEMBERS">
<id name="id" column="ID"/>
<property name="cid" column="CID" />
<property name="name" column="NAME" />
</class>
</hibernate-mapping>
测试代码如下:
package com.sitinspring.companymember;
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();
// --插入代码开始
Company citi = new Company(1, "Citi");
citi.add(new Member(10, "Andy"));
citi.add(new Member(11, "Bill"));
session.save(citi);
Company google = new Company(2, "google");
google.add(new Member(20, "Cindy"));
google.add(new Member(21, "Dennis"));
session.save(google);
session.flush();
Company company = (Company) session.load(Company.class, citi.getId());
company = (Company) session.load(Company.class, google.getId());
// --插入代码结束
// --读取代码开始
long i = 1;
Company companyLoaded = (Company) session.load(Company.class, i);
System.out.println(companyLoaded);
i = 2;
companyLoaded = (Company) session.load(Company.class, i);
System.out.println(companyLoaded);
// --读取代码开始
session.getTransaction().commit();
HibernateUtil.getSessionFactory().close();
}
}
测试结果:
代码下载:
http://www.blogjava.net/Files/sitinspring/HibernateClasses20071109085612.rar