Map映射关系:
1)
Team.java:
private Stringid;
private StringteamName;
privateMap students;
为其生成set、get方法。
Team.hbm.xml:
<classname="com.songjinghao.hibernate.Team"table="team">
<idname="id"column="id"type="string">
<generatorclass="uuid"></generator>
</id>
<propertyname="teamName"column="teamName"type="string"></property>
<mapname="students"table="student">
<keycolumn="team_id"></key>
<indexcolumn="name"type="string"></index> <!-- 指定的是Map中的key值 -->
<elementcolumn="decription"type="string"></element> <!-- 指定的是Map中的value值 -->
</map>
</class>
2)
Team.java:
与1)中相同。
Student.java:
private Stringid;
private Stringname;
private StringcardId;
privateintage;
private Teamteam;
为其生成set、get方法。
Team.hbm.xml:
<classname="com.songjinghao.hibernate.Team"table="team">
< id name = "id" column = "id" type = "string" ><generatorclass="uuid"></generator>
</id>
< property name = "teamName" column = "teamName" type = "string" ></ property ><mapname="students"table="student"cascade="all">
<keycolumn="team_id"></key>
<indexcolumn="card_id"type="string"></index> <!-- 指定的是Map中的key值 -->
<one-to-manyclass="com.songjinghao.hibernate.Student"/>
</map>
</class>
Student.hbm.xml:
<classname="com.songjinghao.hibernate.Student"table="student">
<idname="id"column="id"type="string">
<generatorclass="uuid"></generator>
</id>
<propertyname="cardId"column="card_id"type="string"></property>
<propertyname="name"column="name"type="string"></property>
<propertyname="age"column="age"type="integer"></property>
<many-to-onename="team"class="com.songjinghao.hibernate.Team"column="team_id"></many-to-one>
</class>
总结:
map与set标签中的element子标签映射的是原子类型(string,date,int,long…),即能够直接映射到数据库表字段上的类型,而one-to-many映射的则是实体类型,指的是无法映射到表的某个字段,而是要映射到整张表的类型。
List映射关系:
Team.java:
private Stringid;
private StringteamName;
privateList students =newArrayList();
为其生成set、get方法。
Student.java:
private Stringid;
private StringcardId;
private Stringname;
privateintage;
private Teamteam;
为其生成set、get方法。
Team.hbm.xml:
<classname="com.songjinghao.hibernate.Team"table="team">
<idname="id"column="id"type="string">
<generatorclass="uuid"></generator>
</id>
<propertyname="teamName"column="teamName"type="string"></property>
<listname="students"table="student"cascade="all">
<key column="team_id"></key>
<index column="index_"></index> <!-- 对应于一的一方的多的一方的索引值 -->
<one-to-many class="com.songjinghao.hibernate.Student"/>
</list>
Student.hbm.xml:
<classname="com.songjinghao.hibernate.Student"table="student">
<idname="id"column="id"type="string">
<generatorclass="uuid"></generator>
</id>
<propertyname="cardId"column="card_id"type="string"></property>
<propertyname="name"column="name"type="string"></property>
<propertyname="age"column="age"type="integer"></property>
<many-to-onename="team"column="team_id"class="com.songjinghao.hibernate.Team"></many-to-one>
</class>
Bag映射关系:
因为Bag要用List模拟,Team.java、Student.java代码与List映射关系的一样。
Student.hbm.xml也没有变化。
Team.hbm.xml:
<classname="com.songjinghao.hibernate.Team"table="team">
<idname="id"column="id"type="string">
<generatorclass="uuid"></generator>
</id>
<propertyname="teamName"column="teamName"type="string"></property>
<bagname="students"table="student"cascade="all"inverse="true">
<key column="team_id"></key>
<one-to-many class="com.songjinghao.hibernate.Student"/>
</bag>
</class>
总结:
inverse的设置问题:有序的让一的一方去维护。
Bag (结合了 List 与 Set ),可以重复且没有顺序的一种集合,是 Hibernate 提供的。