多对多的两个关联对象,那种方案更好?

06-04-13 Y04069
有两个对象 User Role,二者是多对多的关系:

第一种设计方案:

public class User extends BaseObject {

private static final long serialVersionUID = -4129505469133941097L;

protected long userID;

protected String username;

/**

* <code>flag</code> 用户具有的roles

*/

protected Set roles = new HashSet();

/**

* @hibernate.id column="userID" generator-class="native"

*/

public long getUserID() {

return userID;

}

public void setUserID(long userID) {

this.userID = userID;

}

/**

* @hibernate.property column="username" length="60"

*/

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

/**

* @return roles

* @hibernate.set table="user_role" cascade="save-update" lazy="false"

* @hibernate.collection-key column="userID"

* @hibernate.collection-many-to-many class="Role"

* outer-join="auto" column="roleID"

*/

public Set getRoles() {

return roles;

}

public void setRoles(Set roles) {

this.roles = roles;

}

}

public class Role extends BaseObject {

private static final long serialVersionUID = 949411947771767L;

/**

* <code>id</code> 角色id

*/

private String roleID;

/**

* <code>name</code> 角色名称

*/

private String rolename;

/**

* <code>des</code> 角色说明

*/

private String description;

/**

* <code>des</code> 拥有该角色的users

*/

private Set users = new HashSet();

/**

* @hibernate.id column="roleID" length="40" generator-class="assigned"

*/

public String getRoleID() {

return roleID;

}

public void setRoleID(String roleID) {

this.roleID = roleID;

}

/**

* @hibernate.property column="description" length="50"

*/

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

/**

* @hibernate.property column="rolename" length="30"

*/

public String getRolename() {

return rolename;

}

public void setRolename(String rolename) {

this.rolename = rolename;

}

/**

* @return Returns the users.

* @hibernate.set table="user_role" cascade="save-update" lazy="false"

* inverse="true"

* @hibernate.collection-key column="roleID"

* @hibernate.collection-many-to-many class="User"

* column="userID"

*/

public Set getUsers() {

return users;

}

public void setUsers(Set users) {

this.users = users;

}

}

第二种设计方案:

public class User extends BaseObject {

private static final long serialVersionUID = -4129505469133941097L;

protected long userID;

protected String username;

/**

* @hibernate.id column="userID" generator-class="native"

*/

public long getUserID() {

return userID;

}

public void setUserID(long userID) {

this.userID = userID;

}

/**

* @hibernate.property column="username" length="60"

*/

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

}

public class Role extends BaseObject {

private static final long serialVersionUID = 949411947771767L;

/**

* <code>id</code> 角色id

*/

private String roleID;

/**

* <code>name</code> 角色名称

*/

private String rolename;

/**

* <code>des</code> 角色说明

*/

private String description;

/**

* @hibernate.id column="roleID" length="40" generator-class="assigned"

*/

public String getRoleID() {

return roleID;

}

public void setRoleID(String roleID) {

this.roleID = roleID;

}

/**

* @hibernate.property column="description" length="50"

*/

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

/**

* @hibernate.property column="rolename" length="30"

*/

public String getRolename() {

return rolename;

}

public void setRolename(String rolename) {

this.rolename = rolename;

}

}

public class UserRole extends BaseObject{

private static final long serialVersionUID = 949445679834343L;

private long id;

/**

* <code>roleID</code> 角色id

*/

private String roleID;

/**

* <code>userID</code> 用户id

*/

private String userID;

/**

* @hibernate.id column="id" generator-class="increment"

*/

public String getId() {

return id;

}

public void setId(long ID) {

this.id = ID;

}

/**

* @hibernate.property column="roleID" length="40"

*/

public String getRoleID() {

return roleID;

}

public void setRoleID(String roleID) {

this.roleID = roleID;

}

/**

* @hibernate.property column="userID"

*/

public long getUserID() {

return userID;

}

public void setUserID(long id) {

this.userID = id;

}

}

以上两种方案,哪种比较好?多对多关联后的两个对象的添加和删除等操作是不是有些复杂?

    

banq
2006-04-17 15:48
多对多我一般不做关联,没有太大意义。

Y04069
2006-04-20 11:23
> 多对多我一般不做关联,没有太大意义。

能否具体解释下,请多指教!!!

banq
2006-04-20 11:35
具体解释不是一两句说完,而且和具体业务场景有关,关联关系其实取决于你对业务分析的结果,从另外一个角度我们来稍微解释一下,其实在一个业务域中,多个业务模型之间总是存在或多或少的关系,如果你就事情论事情,那么每个业务模型都会和其他业务模型有关系,而且极可能是多对多关系,这就造成了以前数据表杂乱无章的蜘蛛网关系图,这实际加剧了系统复杂性,这说明我们分析需求的方法出现问题了。

先在,使用四色图等这样分析方法,需求会比较清楚,我个人没有碰到过上述蜘蛛网的问题。

Y04069
2006-04-23 16:26
多谢,我再研究一下!!!

猜你喜欢