数据库结构:
实体对象:
create
table
team2(id
varchar
(
50
)
primary
key
,name
varchar
(
50
));
create table member2(id varchar ( 50 ) primary key ,name varchar ( 50 ),age varchar ( 50 ));
create table memberAtTeams2(member varchar ( 50 ),team varchar ( 50 ),teamRoles varchar ( 50 ));
create table member2(id varchar ( 50 ) primary key ,name varchar ( 50 ),age varchar ( 50 ));
create table memberAtTeams2(member varchar ( 50 ),team varchar ( 50 ),teamRoles varchar ( 50 ));
实体对象:
package
mapmanytomany;
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;
}
}
package mapmanytomany;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Member {
private String id;
private String name;
private String teamRole;
private Map team = new HashMap();
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 getTeamRole() {
return teamRole;
}
public void setTeamRole(String teamRole) {
this .teamRole = teamRole;
}
public String getId() {
return id;
}
public void setId(String id) {
this .id = id;
}
public Map getTeam() {
return team;
}
public void setTeam(Map team) {
this .team = team;
}
}
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;
}
}
package mapmanytomany;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Member {
private String id;
private String name;
private String teamRole;
private Map team = new HashMap();
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 getTeamRole() {
return teamRole;
}
public void setTeamRole(String teamRole) {
this .teamRole = teamRole;
}
public String getId() {
return id;
}
public void setId(String id) {
this .id = id;
}
public Map getTeam() {
return team;
}
public void setTeam(Map team) {
this .team = team;
}
}
映射:
<?
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 ="mapmanytomany" >
< class name ="Team" table ="team2" >
< id name ="id" column ="id" >
< generator class ="uuid.hex" ></ generator >
</ id >
< property name ="name" column ="name" />
< map name ="members" table ="memberAtTeams2" cascade ="save-update" >
< key column ="team" />
< index type ="string" column ="teamRoles" />
< many-to-many class ="Member" column ="member" ></ many-to-many >
</ map >
</ class >
</ hibernate-mapping >
<? 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 ="mapmanytomany" >
< class name ="Member" table ="member2" >
< id name ="id" column ="id" >
< generator class ="uuid.hex" ></ generator >
</ id >
< property name ="name" column ="name" />
< property name ="age" column ="age" />
< map name ="team" table ="memberAtTeams2" inverse ="true" >
< key column ="member" />
< index type ="string" column ="teamRoles" ></ index >
< many-to-many class ="Team" column ="team" ></ many-to-many >
</ 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 ="mapmanytomany" >
< class name ="Team" table ="team2" >
< id name ="id" column ="id" >
< generator class ="uuid.hex" ></ generator >
</ id >
< property name ="name" column ="name" />
< map name ="members" table ="memberAtTeams2" cascade ="save-update" >
< key column ="team" />
< index type ="string" column ="teamRoles" />
< many-to-many class ="Member" column ="member" ></ many-to-many >
</ map >
</ class >
</ hibernate-mapping >
<? 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 ="mapmanytomany" >
< class name ="Member" table ="member2" >
< id name ="id" column ="id" >
< generator class ="uuid.hex" ></ generator >
</ id >
< property name ="name" column ="name" />
< property name ="age" column ="age" />
< map name ="team" table ="memberAtTeams2" inverse ="true" >
< key column ="member" />
< index type ="string" column ="teamRoles" ></ index >
< many-to-many class ="Team" column ="team" ></ many-to-many >
</ 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 " );
Team team2 = new Team();
team2.setName( " 足球队-02 " );
Member m1 = new Member();
m1.setName( " 张三 " );
m1.setAge( " 11 " );
m1.getTeam().put( " 主力守门员 " , team1);
m1.getTeam().put( " 候选守门员 " , team2);
Member m2 = new Member();
m2.setName( " 张四 " );
m2.setAge( " 22 " );
m2.getTeam().put( " 主力中后卫 " , team1);
m2.getTeam().put( " 候选中后卫 " , team2);
team1.getMembers().put( " 主力守门员 " , m1);
team1.getMembers().put( " 主力中后卫 " , m2);
team2.getMembers().put( " 候选守门员 " , m1);
team2.getMembers().put( " 候选中后卫 " , m2);
session.save(team1);
session.save(team2);
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 " );
Team team2 = new Team();
team2.setName( " 足球队-02 " );
Member m1 = new Member();
m1.setName( " 张三 " );
m1.setAge( " 11 " );
m1.getTeam().put( " 主力守门员 " , team1);
m1.getTeam().put( " 候选守门员 " , team2);
Member m2 = new Member();
m2.setName( " 张四 " );
m2.setAge( " 22 " );
m2.getTeam().put( " 主力中后卫 " , team1);
m2.getTeam().put( " 候选中后卫 " , team2);
team1.getMembers().put( " 主力守门员 " , m1);
team1.getMembers().put( " 主力中后卫 " , m2);
team2.getMembers().put( " 候选守门员 " , m1);
team2.getMembers().put( " 候选中后卫 " , m2);
session.save(team1);
session.save(team2);
t.commit();
System.out.println( " success