使用spring中HibernateTemplate实现多对多,三张表:两张基表,一张关系表,保存时OK,可查询时会把关系表中的数据删除,不知道为什么,望高手们赐教,代码片段如下://*****_zh和_en是国际化用的,可以忽略*****
public class Right {
private Integer rightId;
private int type;
private String description;
private Right_zh right_zh;
private Right_en right_en;
private Set roles = new HashSet();
}public class Role {
private Integer roleId;
private int degree;
private String description;
private Role_zh role_zh;
private Role_en role_en;
private Set rights = new HashSet();
}//*****HIBERNATE映射文件*****
<class name="Right" table="RIGHT_">
<id name="rightId" column="rightId" unsaved-value="null">
<generator class="increment">
<!--param name="sequence">h_sq</param-->
</generator>
</id>
<property name="type" column="type"></property>
<one-to-one name="right_zh" cascade="all"></one-to-one>
<one-to-one name="right_en" cascade="all"></one-to-one>
<set name="roles" table="role_right" cascade="none" >
<key column="right_id"></key>
<many-to-many class="Role" column="role_id"></many-to-many>
</set>
</class>
<class name="Right" table="RIGHT_">
<id name="rightId" column="rightId" unsaved-value="null">
<generator class="increment">
<!--param name="sequence">h_sq</param-->
</generator>
</id>
<property name="type" column="type"></property>
<one-to-one name="right_zh" cascade="all"></one-to-one>
<one-to-one name="right_en" cascade="all"></one-to-one>
<set name="roles" table="role_right" cascade="none" >
<key column="right_id"></key>
<many-to-many class="Role" column="role_id"></many-to-many>
</set>
</class>//****实现与测试*****:
public class RoleManagerImpl {
HibernateTemplate ht = null;
public RoleManagerImpl(SessionFactory sf) {
super();
this.ht = new HibernateTemplate(sf);
}
public RoleManagerImpl() {
super();
// TODO Auto-generated constructor stub
}
// 查询所有的角色信息
public Collection queryAll(){
Collection roleList= null;
roleList =ht.find("from com.amw.model.Role");
return roleList; 
}

//  根据角色ID查角色
public Role queryRoleById(int id){
Role role = (Role)ht.get(com.amw.model.Role.class, id);
return role; 
}

// 增加一个角色
public void addRole(Role role){
ht.save(role);
}

// 修改角色信息
public void modify(Role role){
ht.update(role);
}

// 删除一个角色
public void delete(Role role){
ht.delete(role);
}

public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("com/amw/model/applicationContext.xml");
RoleManagerImpl rmi = (RoleManagerImpl) context.getBean("service_role_proxy");
RightManagerImpl rm = (RightManagerImpl) context.getBean("service_right_proxy");//      增加一个角色
Role role = new Role(1);
Role_zh role_zh = new Role_zh("超级管理员");
Role_en role_en = new Role_en("Adminstrator");

role.setRole_zh(role_zh);
role.setRole_en(role_en);

role_zh.setRole(role);
role_en.setRole(role);

Right right = rm.queryRightById(1);
role.getRights().add(right);

rmi.addRole(role); role = rmi.queryRoleById(1);
System.out.println(role);

}建表语句:
//******权限建表******
create table right_(
rightId int primary key,
type int
); //******角色建表******
create table role_(
roleId int primary key,
degree int
);

//******角色权限表*****
create table role_right(
role_id int,
right_id int,
primary key(role_id,right_id)
);