【发布时间】:2010-09-27 19:51:00
【问题描述】:
我正在动态创建一些 grails 域对象,然后将它们添加到另一个 grails 域对象中声明的 SortedSet。我创建了一个 Project 类,填写了它的值,并检查了它是否有效。它是有效的,所以我想将此项目添加到员工。
我的代码基本上是这样的
Employee employee = Employee.get(session.empid)
...
//populate some Project objects
...
//add projects to employee
employee.addToProjects(project)
这里可能出了什么问题?如果我执行 project.validate(),然后检查错误,则只有一个说该项目没有与之关联的有效员工 - 但是一旦我执行了 employee.addToProjects,这应该会消失。 Employee hasMany Project 对象,它是这样声明的:
class Employee implements Comparable
{
static hasMany = [projects:Project]
static constraints =
{
}
static mapping = {
projects cascade:"all,delete-orphan", lazy:false
}
SortedSet<Project> projects = new TreeSet<Project>();
}
public class Project implements Comparable
{
static belongsTo = [employee:Employee]
static hasMany = [roles:Role]
static mapping = {
roles lazy:false, cascade:"all,delete-orphan"
}
@XmlElement
List<Role> roles = new ArrayList<Role>();
/*
* return sorted list. overwriting default getter was causing error upon saving multiple roles.
*
*/
def List getSortedRoles(){
Collections.sort(roles, new RoleComparator());
return roles;
}
String toString()
{
return name
}
// compare by latest date of roles, then by name + id
//if this is too intrusive, implement comparator with this logic and sort on rendering page
int compareTo(obj) {
if(obj == null){
return 1;
}
def myMaxRole = findMaxRole(roles);
def rhsMaxRole = findMaxRole(obj.roles);
def rcomparator = new RoleComparator();
System.out.println(myMaxRole.title + " " + rhsMaxRole.title + " " + rcomparator.compare(myMaxRole, rhsMaxRole));
return rcomparator.compare(myMaxRole, rhsMaxRole);
}
def List getExpandableRoleList()
{
return LazyList.decorate(roles, FactoryUtils.instantiateFactory(Role.class));
}
def setExpandableRoleList(List l)
{
return roles = l;
}
def Role findMaxRole(roles){
RoleComparator rc = new RoleComparator();
Role maxRole = roles.first();
for(role in roles){
if(rc.compare(maxRole, role) > 0){
maxRole = role;
}
}
return maxRole;
}
public class Role implements Comparable
{
static belongsTo = [project:Project]
static hasMany = [roleSkills:RoleSkill,roleTools:RoleTool]
static mapping = {
duties type:"text"
roleSkills cascade:"all,delete-orphan", lazy:false
roleTools cascade:"all,delete-orphan", lazy:false
}
static contraints = {
endDate(nullable: true)
}
boolean _deleted
static transients = ['_deleted']
@XmlElement
String title = ""
@XmlElement
String duties = ""
@XmlElement
int levelOfEffort
@XmlElement
Date startDate = new Date()
@XmlElement
Date endDate = new Date()
@XmlElement
Date lastModified = new Date()
@XmlElement
LocationType locationType = new LocationType(type: "Unknown")
@XmlElement
String rank
@XmlElement
List<RoleSkill> roleSkills = new ArrayList<RoleSkill>()
@XmlElement
List<RoleTool> roleTools = new ArrayList<RoleTool>()
String toString()
{
return title;
}
int compareTo(obj) {
return title.compareTo(obj.title)
}
def skills() {
return roleSkills.collect{it.skill}
}
def tools() {
return roleTools.collect{it.tool}
}
}
【问题讨论】:
-
域对象是什么样的?您的项目对象是否实现了 Comparable?
-
添加了相关信息 - 是的,它们实现了可比性
-
另外 - 我将再次指出,当我创建 Project 对象时,它返回的所有内容都有效,除了对员工没有价值,但是当我将项目添加到 Employee 中的 Set 时应该添加