在这个案例的hbm文件中,只有一个持久话类Team,Member作为class引入,map的关系被映射到teammember表中,
数据库结构:
CREATE
TABLE
`team5` (
`id` varchar ( 50 ) NOT NULL ,
`name` varchar ( 50 ) default NULL ,
PRIMARY KEY (`id`)
) ENGINE = InnoDB DEFAULT CHARSET = gb2312;
CREATE TABLE `teammembers` (
`teamid` varchar ( 50 ) NOT NULL ,
`name` varchar ( 50 ) default NULL ,
`age` varchar ( 50 ) default NULL ,
`teamrole` varchar ( 50 ) default NULL ,
) ENGINE = InnoDB DEFAULT CHARSET = gb2312;
`id` varchar ( 50 ) NOT NULL ,
`name` varchar ( 50 ) default NULL ,
PRIMARY KEY (`id`)
) ENGINE = InnoDB DEFAULT CHARSET = gb2312;
CREATE TABLE `teammembers` (
`teamid` varchar ( 50 ) NOT NULL ,
`name` varchar ( 50 ) default NULL ,
`age` varchar ( 50 ) default NULL ,
`teamrole` varchar ( 50 ) default NULL ,
) ENGINE = InnoDB DEFAULT CHARSET = gb2312;
持久类:
package
compositeelement;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Member ... {
private String id;
private String name;
private Team team;
private String age;
public String getAge() ...{
return age;
}
public void setAge(String age) ...{
this.age = age;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public String getId() ...{
return id;
}
public void setId(String id) ...{
this.id = id;
}
public Team getTeam() ...{
return team;
}
public void setTeam(Team team) ...{
this.team = team;
}
}
package compositeelement;
import java.util.HashMap;
import java.util.Map;
public class Team ... {
private String id;
private String name;
private Map members=new HashMap();
public String getId() ...{
return id;
}
public void setId(String id) ...{
this.id = id;
}
public Map getMembers() ...{
return members;
}
public void setMembers(Map members) ...{
this.members = members;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Member ... {
private String id;
private String name;
private Team team;
private String age;
public String getAge() ...{
return age;
}
public void setAge(String age) ...{
this.age = age;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public String getId() ...{
return id;
}
public void setId(String id) ...{
this.id = id;
}
public Team getTeam() ...{
return team;
}
public void setTeam(Team team) ...{
this.team = team;
}
}
package compositeelement;
import java.util.HashMap;
import java.util.Map;
public class Team ... {
private String id;
private String name;
private Map members=new HashMap();
public String getId() ...{
return id;
}
public void setId(String id) ...{
this.id = id;
}
public Map getMembers() ...{
return members;
}
public void setMembers(Map members) ...{
this.members = members;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
}
映射文件:
<?
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 - Hibernate Tools
-->
< hibernate-mapping package ="compositeelement" >
< import class ="Member" />
< class name ="Team" table ="team5" >
< id name ="id" column ="id" >
< generator class ="uuid.hex" ></ generator >
</ id >
< property name ="name" column ="name" />
< map name ="members" table ="teammembers" inverse ="false" cascade ="save-update" >
< key column ="teamid" />
< index column ="teamrole" type ="string" ></ index >
< composite-element class ="Member" >
< parent name ="team" />
< property name ="name" />
< property name ="age" ></ property >
</ composite-element >
</ map >
</ class >
</ hibernate-mapping >
<! 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 - Hibernate Tools
-->
< hibernate-mapping package ="compositeelement" >
< import class ="Member" />
< class name ="Team" table ="team5" >
< id name ="id" column ="id" >
< generator class ="uuid.hex" ></ generator >
</ id >
< property name ="name" column ="name" />
< map name ="members" table ="teammembers" inverse ="false" cascade ="save-update" >
< key column ="teamid" />
< index column ="teamrole" type ="string" ></ index >
< composite-element class ="Member" >
< parent name ="team" />
< property name ="name" />
< property name ="age" ></ property >
</ composite-element >
</ map >
</ class >
</ hibernate-mapping >
测试代码:
public
static
void
main(String[] args)
...
{
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction t=session.beginTransaction();
Team team1=new Team();
team1.setName("足球队-01");
Member m1=new Member();
m1.setName("张三");
m1.setAge("11");
Member m2=new Member();
m2.setName("张四");
m2.setAge("22");
team1.getMembers().put("守门员", m1);
team1.getMembers().put("中后卫", m2);
session.save(team1);
t.commit();
System.out.println("success");
}
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Transaction t=session.beginTransaction();
Team team1=new Team();
team1.setName("足球队-01");
Member m1=new Member();
m1.setName("张三");
m1.setAge("11");
Member m2=new Member();
m2.setName("张四");
m2.setAge("22");
team1.getMembers().put("守门员", m1);
team1.getMembers().put("中后卫", m2);
session.save(team1);
t.commit();
System.out.println("success");
}
运行结果:
Hibernate: insert into team5 (name, id) values (?, ?)
Hibernate: insert into teammembers (teamid, teamrole, name, age) values (?, ?, ?, ?)
Hibernate: insert into teammembers (teamid, teamrole, name, age) values (?, ?, ?, ?)