关联映射方面的最后一篇了,我觉得映射文件的编写是使用hibernate的基础,而关联映射又是基础的基础,所以这方面分的细一些,罗嗦一些,说明白就好,呵呵。
多对多关联(双向),相对单向,在实体上就是一端也保持另一端的对象集合,在映射文件中也配置上。但是要注意两边配置文件中指定的外键一定要对应。
1.对象模型:
2.关系模型:
3.实体类:
Player.java
public
class Player {
private Integer id;
private String name;
private Set<Role> roles;
//一系列的setter.getter方法
@Override
public String toString() {
return "Player: " + name;
}
}
private Integer id;
private String name;
private Set<Role> roles;
//一系列的setter.getter方法
@Override
public String toString() {
return "Player: " + name;
}
}
Role.java
public
class Role {
private Integer id;
private String name;
private Set<Player> players;
//一系列的setter.getter方法
@Override
public String toString() {
return "Role: " + name;
}
}
private Integer id;
private String name;
private Set<Player> players;
//一系列的setter.getter方法
@Override
public String toString() {
return "Role: " + name;
}
}
4.映射文件:
Player.hbm.xml
<
class
name
="com.sxt.hibernate.many2many.entity.Player"
table
="sxt_hibernate_player"
>
< id name ="id" length ="4" >
< generator class ="native" > </ generator >
</ id >
< property name ="name" length ="10" > </ property >
<!-- table="sxt_hibernate_user_role"含义,用来指定中间表 -->
< set name ="roles" table ="sxt_hibernate_player_role" cascade ="save-update" >
<!-- <key column="user_id">含义,指定中间表中用来指向本表的外键 -->
< key column ="player_id" > </ key >
<!-- column含义,用来指定中间表中用来指向另一端表的外键 -->
< many-to-many class ="com.sxt.hibernate.many2many.entity.Role" column ="role_id" > </ many-to-many >
</ set >
</ class >
< id name ="id" length ="4" >
< generator class ="native" > </ generator >
</ id >
< property name ="name" length ="10" > </ property >
<!-- table="sxt_hibernate_user_role"含义,用来指定中间表 -->
< set name ="roles" table ="sxt_hibernate_player_role" cascade ="save-update" >
<!-- <key column="user_id">含义,指定中间表中用来指向本表的外键 -->
< key column ="player_id" > </ key >
<!-- column含义,用来指定中间表中用来指向另一端表的外键 -->
< many-to-many class ="com.sxt.hibernate.many2many.entity.Role" column ="role_id" > </ many-to-many >
</ set >
</ class >
Role.hbm.xml
<
class
name
="com.sxt.hibernate.many2many.entity.Role"
table
="sxt_hibernate_role"
>
< id name ="id" length ="4" >
< generator class ="native" > </ generator >
</ id >
< property name ="name" length ="10" > </ property >
< set name ="players" table ="sxt_hibernate_player_role" cascade ="save-update" >
<!-- 注意这里指定的外键要和另一端映射文件中的相对应 -->
< key column ="role_id" > </ key >
<!-- 注意这里指定的外键要和另一端映射文件中的相对应 -->
< many-to-many class ="com.sxt.hibernate.many2many.entity.Player" column ="player_id" > </ many-to-many >
</ set >
</ class >
< id name ="id" length ="4" >
< generator class ="native" > </ generator >
</ id >
< property name ="name" length ="10" > </ property >
< set name ="players" table ="sxt_hibernate_player_role" cascade ="save-update" >
<!-- 注意这里指定的外键要和另一端映射文件中的相对应 -->
< key column ="role_id" > </ key >
<!-- 注意这里指定的外键要和另一端映射文件中的相对应 -->
< many-to-many class ="com.sxt.hibernate.many2many.entity.Player" column ="player_id" > </ many-to-many >
</ set >
</ class >
5.hibernate配置文件:
参照上一篇。
6. 测试方法:
public
static
void main(String[] args) {
Session session = HibernateUtils.getSession();
Transaction t = session.beginTransaction();
try {
/**
* 测试插入数据
*/
/* Player player1=new Player();
player1.setName("姚明");
Player player2=new Player();
player2.setName("詹姆斯");
Player player3=new Player();
player3.setName("科比");
Role role1=new Role();
role1.setName("中锋");
Set<Player> players1=new HashSet<Player>();
players1.add(player1);
players1.add(player2);
role1.setPlayers(players1);
Role role2=new Role();
role2.setName("后卫");
Set<Player> players2=new HashSet<Player>();
players2.add(player2);
players2.add(player3);
role2.setPlayers(players2);
session.save(role1);
session.save(role2);*/
/**
* 测试加载数据
*/
Role role=(Role)session.load(Role. class, 4);
System.out.println(role);
for(Iterator<Player> players=role.getPlayers().iterator();players.hasNext();){
System.out.println(players.next());
}
t.commit();
} catch (HibernateException e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}
}
Session session = HibernateUtils.getSession();
Transaction t = session.beginTransaction();
try {
/**
* 测试插入数据
*/
/* Player player1=new Player();
player1.setName("姚明");
Player player2=new Player();
player2.setName("詹姆斯");
Player player3=new Player();
player3.setName("科比");
Role role1=new Role();
role1.setName("中锋");
Set<Player> players1=new HashSet<Player>();
players1.add(player1);
players1.add(player2);
role1.setPlayers(players1);
Role role2=new Role();
role2.setName("后卫");
Set<Player> players2=new HashSet<Player>();
players2.add(player2);
players2.add(player3);
role2.setPlayers(players2);
session.save(role1);
session.save(role2);*/
/**
* 测试加载数据
*/
Role role=(Role)session.load(Role. class, 4);
System.out.println(role);
for(Iterator<Player> players=role.getPlayers().iterator();players.hasNext();){
System.out.println(players.next());
}
t.commit();
} catch (HibernateException e) {
e.printStackTrace();
t.rollback();
} finally {
HibernateUtils.closeSession(session);
}
}
}
本文转自NightWolves 51CTO博客,原文链接:
http://blog.51cto.com/yangfei520/276187
,如需转载请自行联系原作者