Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1104115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:26:22+00:00 2026-05-17T01:26:22+00:00

I am working on creating some grails domain objects dynamically and then adding them

  • 0

I am working on creating some grails domain objects dynamically and then adding them a SortedSet declared in another grails domain object. I have created a Project class, filled in its values, and checked to make sure it is valid. It is valid, so I want to add this Project to an Employee.

My code essentially goes like this

Employee employee = Employee.get(session.empid)
...
//populate some Project objects
...
//add projects to employee
employee.addToProjects(project)

What could be going wrong here? If I do a project.validate(), and then check for errors, the only one says that project has no valid employee associated with it – but that should go away once I do the employee.addToProjects. Employee hasMany Project objects, and it is declared like so:

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}
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:26:22+00:00Added an answer on May 17, 2026 at 1:26 am

    went back to basics and wrote an integration test using your objects and it all works fine, you error must be in how you are saving the objects

    test snippet

    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()
    }
    

    my objects..

    public class Project implements Comparable
    {  
        static belongsTo = [employee:Employee]
    
        String name;
    
        static mapping = {
              roles lazy:false, cascade:"all,delete-orphan"
        }
    
    
        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;
               }
    
    
               return this.name.compareTo(obj.name);
           }
    
    }
    
    class Employee implements Comparable
    {
        static hasMany = [projects:Project]
    
        String first, last
        static constraints = 
        {
        }
    
        static mapping = {
            projects cascade:"all,delete-orphan", lazy:false
        }
    
        SortedSet<Project> projects = new TreeSet<Project>();
    
        int compareTo(obj) {
            if(obj == null){
                return 1;
            }
               return this.name.compareTo(obj.name);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a task where I am creating some input control dynamically
I am working on dynamically creating some JavaScript that will be inserted into a
I have a problem creating some form of hierarchy with different object types. I
I am working on creating a jQuery plugin and I have some questions regarding
I'm working on creating a datagrid that has a checkbox column. I have some
I am working in WPF and I am creating some userControl which some of
I am creating a process to do some working. But the process is starting
I'm having troubles with creating a simple list (some expandable lists are already working).
I was working on creating some tables in database foo , but every time
I am creating some tabs using labels, then im going to add some simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.