微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 使用Grails SortedSet时,无法从空列表中访问first()元素

我正在努力动态创建一些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有很多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,lazy:false
        roletools cascade:"all,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}
    }
}

解决方法

回到基础并使用你的对象编写了一个集成测试,一切正常,你必须在如何保存对象时出错

测试片段

void testSomething() {
    def emp = new Employee(first:"Aaron",last:"Saunders")
    emp.save()

    emp =  Employee.get(1)

    emp.addToProjects(new Project(name:"Project 3"))
    emp.addToProjects(new Project(name:"Project 1"))
    emp.addToProjects(new Project(name:"Project 2"))

    emp.save()

    println Employee.get(1)

    println Employee.get(1).projects.first()
}

我的对象..

public class Project implements Comparable
{  
    static belongsTo = [employee:Employee]

    String name;

    static mapping = {
          roles lazy:false,delete-orphan"
    }


    String toString()
    {
        return name
    }


    // compare by latest date of roles,implement comparator with this logic and sort on rendering page
       int compareto(obj) {
           if(obj == null){
               return 1;
           }


           return this.name.compareto(obj.name);
       }

}

class Employee implements Comparable
{
    static hasMany = [projects:Project]

    String first,last
    static constraints = 
    {
    }

    static mapping = {
        projects cascade:"all,lazy:false
    }

    SortedSet<Project> projects = new TreeSet<Project>();

    int compareto(obj) {
        if(obj == null){
            return 1;
        }
           return this.name.compareto(obj.name);
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐