Hibernate 3.0.5
1、Usergroup.hbm.xml 用户组表
<set name="users" table="Groupandusers" lazy="true" inverse="false" cascade="save-update" sort="unsorted">
<key column="GROUPID"/>
<many-to-many class="cn.com.commnet.model.systems.Users" column="USERID" outer-join="auto"/>
</set>
2、Users.hbm.xml 用户表
<set name="groups" table="Groupandusers" lazy="true" inverse="false" cascade="save-update" sort="unsorted" >
<key column="USERID"/>
<many-to-many class="cn.com.commnet.model.systems.Usergroup" column="GROUPID" outer-join="auto"/>
</set>
3、Model Usergroup.java
private Set users = new HashSet();
public Set getUsers() {
return users;
}
public void setUsers(Set users) {
this.users = users;
}
public void addUser(RgUsers user) {
users.add(user);
}
public void updateUsers(Set newUserset){
//get intersection elements 取交集
this.getUsers().retainAll(newUserset);
//get the refresh set in new set 新的集合中取补集
newUserset.removeAll( this.getUsers());
//get the refreshed data 最终的集合
this.getUsers().addAll(newUserset);
}
4、Model Users.java
private Set groups = new HashSet();
public Set getGroups() {
return groups;
}
public void setGroups(Set groups) {
this.groups = groups;
}
public void addGroup(RgUsergroup group) {
groups.add(group);
}
public void updateGroups(Set newGroupset){
//get intersection elements 取交集
this.getGroups().retainAll(newGroupset);
//get the refresh set in new set 新的集合中取补集
newGroupset.removeAll( this.getGroups());
//get the refreshed data 最终的集合
this.getGroups().addAll(newGroupset);
//父接点一侧的配置应为 cascade="all-delete-orphan" ,
//否则子接点只能修改和新增,集合中删除掉的子接点,在数据库中并没有被删除掉。
}
5、Service 层 UserManager.java
a). add 方法:
String[] groupList = form.getGroupList();
for(int i=0;i<groupList.length;i++) {
RgUsergroup group = new UserGroupDAO().getGroupById(Long.valueOf(groupList[i]),false);
user.addGroup(group);
}
dao.save(user);
b). update 方法:
Set newGroupset = new HashSet();
String[] groupList = form.getGroupList();
for(int i=0;i<groupList.length;i++) {
RgUsergroup group = new UserGroupDAO().getGroupById(Long.valueOf(groupList[i]),false);
newGroupset.add(group);
}
user.updateGroups(newGroupset); //更新多对多对应表
dao.update(user);
另外, 多对多, 及一对多的关联查询时, 通过外键属性的条件进行查询, 如:
String strSql = "from Users t where t.groups.groupid =?"
1、Usergroup.hbm.xml 用户组表
<set name="users" table="Groupandusers" lazy="true" inverse="false" cascade="save-update" sort="unsorted">
<key column="GROUPID"/>
<many-to-many class="cn.com.commnet.model.systems.Users" column="USERID" outer-join="auto"/>
</set>
2、Users.hbm.xml 用户表
<set name="groups" table="Groupandusers" lazy="true" inverse="false" cascade="save-update" sort="unsorted" >
<key column="USERID"/>
<many-to-many class="cn.com.commnet.model.systems.Usergroup" column="GROUPID" outer-join="auto"/>
</set>
3、Model Usergroup.java
private Set users = new HashSet();
public Set getUsers() {
return users;
}
public void setUsers(Set users) {
this.users = users;
}
public void addUser(RgUsers user) {
users.add(user);
}
public void updateUsers(Set newUserset){
//get intersection elements 取交集
this.getUsers().retainAll(newUserset);
//get the refresh set in new set 新的集合中取补集
newUserset.removeAll( this.getUsers());
//get the refreshed data 最终的集合
this.getUsers().addAll(newUserset);
}
4、Model Users.java
private Set groups = new HashSet();
public Set getGroups() {
return groups;
}
public void setGroups(Set groups) {
this.groups = groups;
}
public void addGroup(RgUsergroup group) {
groups.add(group);
}
public void updateGroups(Set newGroupset){
//get intersection elements 取交集
this.getGroups().retainAll(newGroupset);
//get the refresh set in new set 新的集合中取补集
newGroupset.removeAll( this.getGroups());
//get the refreshed data 最终的集合
this.getGroups().addAll(newGroupset);
//父接点一侧的配置应为 cascade="all-delete-orphan" ,
//否则子接点只能修改和新增,集合中删除掉的子接点,在数据库中并没有被删除掉。
}
5、Service 层 UserManager.java
a). add 方法:
String[] groupList = form.getGroupList();
for(int i=0;i<groupList.length;i++) {
RgUsergroup group = new UserGroupDAO().getGroupById(Long.valueOf(groupList[i]),false);
user.addGroup(group);
}
dao.save(user);
b). update 方法:
Set newGroupset = new HashSet();
String[] groupList = form.getGroupList();
for(int i=0;i<groupList.length;i++) {
RgUsergroup group = new UserGroupDAO().getGroupById(Long.valueOf(groupList[i]),false);
newGroupset.add(group);
}
user.updateGroups(newGroupset); //更新多对多对应表
dao.update(user);
另外, 多对多, 及一对多的关联查询时, 通过外键属性的条件进行查询, 如:
String strSql = "from Users t where t.groups.groupid =?"