Hibernate Map映射的index-many-to-many案例
2007-03-19 14:06
393 查看
使用Map映射,index部分可以使用组件,也可以使用持久话类,本例使用<index-many-to-many>来实现使用持久话类为map映射定义index
数据库结构:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE TABLE `team4` (
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`id` varchar(50) NOT NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`name` varchar(50) default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
PRIMARY KEY (`id`)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE TABLE `member4` (
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`id` varchar(50) NOT NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`name` varchar(50) default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`age` varchar(50) default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`team` varchar(50) default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
PRIMARY KEY (`id`)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE TABLE `position` (
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`id` varchar(50) NOT NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`role` varchar(50) NOT NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`scene` varchar(50) NOT NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`bestchoice` varchar(50) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
持久话类:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package mapindexmanytomany;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.HashMap;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Map;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Team ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Map members=new HashMap();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getId() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setId(String id) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.id = id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Map getMembers() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return members;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setMembers(Map members) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.members = members;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getName() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setName(String name) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name = name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package mapindexmanytomany;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.HashMap;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Map;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Set;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Member ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Position postition;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Team team;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getAge() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setAge(String age) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.age = age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getName() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setName(String name) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name = name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Position getPostition() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return postition;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setPostition(Position postition) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.postition = postition;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getId() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setId(String id) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.id = id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Team getTeam() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return team;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setTeam(Team team) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.team = team;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package mapindexmanytomany;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Position ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String role;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String scene;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Member bestChoice;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Member getBestChoice() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return bestChoice;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setBestChoice(Member bestChoice) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.bestChoice = bestChoice;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getRole() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return role;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setRole(String role) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.role = role;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getScene() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return scene;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setScene(String scene) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.scene = scene;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getId() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setId(String id) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.id = id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
映射文件:
其中 <index-many-to-many class="Position" column="id"></index-many-to-many>定义了,数据表Position的主键id值使用Member表的主键数值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="utf-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Mapping file autogenerated by MyEclipse - Hibernate Tools
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<hibernate-mapping package="mapindexmanytomany">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<class name="Team" table="team4">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<id name="id" column="id">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<generator class="uuid.hex"></generator>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</id>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="name" column="name"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<map name="members" inverse="false" cascade="all">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<key column="team"></key>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<index-many-to-many class="Position" column="id"></index-many-to-many>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<one-to-many class="Member"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</map>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</hibernate-mapping>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="utf-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Mapping file autogenerated by MyEclipse - Hibernate Tools
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<hibernate-mapping package="mapindexmanytomany">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<class name="Member" table="member4">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<id name="id" column="id">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<generator class="uuid.hex"></generator>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</id>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="name" column="name"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="age" column="age"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<many-to-one name="team" ></many-to-one>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</hibernate-mapping>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="utf-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Mapping file autogenerated by MyEclipse - Hibernate Tools
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<hibernate-mapping package="mapindexmanytomany">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<class name="Position" table="position">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<id name="id" column="id">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<generator class="uuid.hex"></generator>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</id>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="role" column="role"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="scene" column="scene"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<many-to-one name="bestChoice" column="bestchoice" cascade="save-update"></many-to-one>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</hibernate-mapping>
测试代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Configuration cfg=new Configuration();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cfg.configure();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SessionFactory sf=cfg.buildSessionFactory();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Session session=sf.openSession();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Transaction t=session.beginTransaction();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Team team1=new Team();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
team1.setName("足球队-01");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Member m1=new Member();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m1.setName("张三");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m1.setAge("11");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Member m2=new Member();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m2.setName("张四");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m2.setAge("22");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Position p1=new Position();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p1.setRole("守门员");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p1.setScene("上半场");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p1.setBestChoice(m1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Position p2=new Position();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p2.setRole("中后卫");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p2.setScene("下半场");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p2.setBestChoice(m2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
team1.getMembers().put(p1, m1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
team1.getMembers().put(p2, m2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.save(p1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.save(p2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.save(team1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
t.commit();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("success");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
运行结果:
Hibernate: insert into member4 (name, age, team, id) values (?, ?, ?, ?)
Hibernate: insert into position (role, scene, bestchoice, id) values (?, ?, ?, ?)
Hibernate: insert into member4 (name, age, team, id) values (?, ?, ?, ?)
Hibernate: insert into position (role, scene, bestchoice, id) values (?, ?, ?, ?)
Hibernate: insert into team4 (name, id) values (?, ?)
Hibernate: update member4 set team=?, id=? where id=?
Hibernate: update member4 set team=?, id=? where id=?
数据库结构:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE TABLE `team4` (
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`id` varchar(50) NOT NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`name` varchar(50) default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
PRIMARY KEY (`id`)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE TABLE `member4` (
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`id` varchar(50) NOT NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`name` varchar(50) default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`age` varchar(50) default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`team` varchar(50) default NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
PRIMARY KEY (`id`)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
CREATE TABLE `position` (
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`id` varchar(50) NOT NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`role` varchar(50) NOT NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`scene` varchar(50) NOT NULL,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
`bestchoice` varchar(50) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
持久话类:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package mapindexmanytomany;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.HashMap;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Map;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Team ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Map members=new HashMap();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getId() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setId(String id) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.id = id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Map getMembers() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return members;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setMembers(Map members) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.members = members;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getName() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setName(String name) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name = name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package mapindexmanytomany;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.HashMap;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Map;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.util.Set;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Member ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Position postition;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Team team;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getAge() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setAge(String age) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.age = age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getName() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setName(String name) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name = name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Position getPostition() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return postition;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setPostition(Position postition) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.postition = postition;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getId() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setId(String id) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.id = id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Team getTeam() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return team;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setTeam(Team team) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.team = team;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package mapindexmanytomany;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class Position ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String role;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String scene;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Member bestChoice;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public Member getBestChoice() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return bestChoice;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setBestChoice(Member bestChoice) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.bestChoice = bestChoice;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getRole() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return role;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setRole(String role) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.role = role;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getScene() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return scene;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setScene(String scene) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.scene = scene;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public String getId() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void setId(String id) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.id = id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
映射文件:
其中 <index-many-to-many class="Position" column="id"></index-many-to-many>定义了,数据表Position的主键id值使用Member表的主键数值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="utf-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Mapping file autogenerated by MyEclipse - Hibernate Tools
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<hibernate-mapping package="mapindexmanytomany">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<class name="Team" table="team4">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<id name="id" column="id">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<generator class="uuid.hex"></generator>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</id>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="name" column="name"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<map name="members" inverse="false" cascade="all">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<key column="team"></key>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<index-many-to-many class="Position" column="id"></index-many-to-many>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<one-to-many class="Member"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</map>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</hibernate-mapping>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="utf-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Mapping file autogenerated by MyEclipse - Hibernate Tools
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<hibernate-mapping package="mapindexmanytomany">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<class name="Member" table="member4">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<id name="id" column="id">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<generator class="uuid.hex"></generator>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</id>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="name" column="name"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="age" column="age"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<many-to-one name="team" ></many-to-one>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</hibernate-mapping>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="utf-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Mapping file autogenerated by MyEclipse - Hibernate Tools
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<hibernate-mapping package="mapindexmanytomany">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<class name="Position" table="position">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<id name="id" column="id">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<generator class="uuid.hex"></generator>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</id>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="role" column="role"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="scene" column="scene"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<many-to-one name="bestChoice" column="bestchoice" cascade="save-update"></many-to-one>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</hibernate-mapping>
测试代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Configuration cfg=new Configuration();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
cfg.configure();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SessionFactory sf=cfg.buildSessionFactory();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Session session=sf.openSession();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Transaction t=session.beginTransaction();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Team team1=new Team();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
team1.setName("足球队-01");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Member m1=new Member();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m1.setName("张三");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m1.setAge("11");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Member m2=new Member();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m2.setName("张四");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m2.setAge("22");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Position p1=new Position();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p1.setRole("守门员");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p1.setScene("上半场");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p1.setBestChoice(m1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Position p2=new Position();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p2.setRole("中后卫");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p2.setScene("下半场");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
p2.setBestChoice(m2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
team1.getMembers().put(p1, m1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
team1.getMembers().put(p2, m2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.save(p1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.save(p2);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.save(team1);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
t.commit();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("success");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
运行结果:
Hibernate: insert into member4 (name, age, team, id) values (?, ?, ?, ?)
Hibernate: insert into position (role, scene, bestchoice, id) values (?, ?, ?, ?)
Hibernate: insert into member4 (name, age, team, id) values (?, ?, ?, ?)
Hibernate: insert into position (role, scene, bestchoice, id) values (?, ?, ?, ?)
Hibernate: insert into team4 (name, id) values (?, ?)
Hibernate: update member4 set team=?, id=? where id=?
Hibernate: update member4 set team=?, id=? where id=?
相关文章推荐
- hibernate一对多映射时异常Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @Collection
- 使用注解的Hibernate one-to-many映射
- hibernate映射文件one-to-many元素属性
- Hibernate 多对多的映射关系(many-to-many)
- Hibernate中的manytomany映射实例一
- Hibernate 、多表关联映射-多对一关系(many-to-one)
- Hibernate 、多表关联映射 - 多对多关系映射(many-to-many)
- Hibernate的配置文件的映射many-to-one错误
- Hibernate实体关系映射(OneToMany单边)——完整实例
- hibernate映射文件many-to-one元素属性
- Hibernate中的manytomany映射实例一(多对多双向)
- hibernate映射many-to-many
- Hibernate多对多映射为第三张表manytomany【xml和注解】实例(十七)
- Hibernate 、多表关联映射 - 多对多关系映射(many-to-many)
- hibernate映射文件many-to-one元素属性
- Hibernate 、多表关联映射 - 多对多关系映射(many-to-many)
- Hibernate学习笔记(4)Hibernate映射文件之many-to-many(多对多关联)
- Hibernate 关系映射 many-to-many
- Hibernate one-to-many && many-to-many 的关系映射
- hibernate映射技巧one-to-many(单项一对多)