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

  • Home
  • SEARCH
  • 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 6072301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:08:42+00:00 2026-05-23T10:08:42+00:00

I have two domain classes Users and Projects like following Users{ String firstName String

  • 0

I have two domain classes Users and Projects like following

Users{
    String firstName
    String lastName
    String emailAddress
    static hasMany = [projects:Projects]
}



class Projects {
    String projectName
    String description
    Users projectLead
    Date completionDate
    static belongsTo = Users
}

Here completionDate == null means the project has not yet been completed.

Now I want to send an email reminder to each user about their incomplete projects, How can write a query to retrieve incomplete projects per user?

I was thinking on following lines but am still not able to go ahead. In order to send an email I will need users emailid, all incomplete projects and names of them

def users = Users.list()
       for(user in users){
           user.projects.find{it.completionDate==null}
       }

Is it possible to use createCriteria in such a case?

  • 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-23T10:08:42+00:00Added an answer on May 23, 2026 at 10:08 am

    I think this should work:

    def usersAndIncompleteProjects = Users.withCriteria {
        projects {
            isNull( completionDate )
        }
    }
    

    That should just return you the Users with incomplete projects, and the projects property for each User will only contain the incomplete projects. If you want the Users to have all of their projects loaded, I believe you need to use an alias


    Testing…

    Given the User class:

    package criteriatest
    
    class User {
        String name
    
        static hasMany = [ projects: Project ]
    }
    

    And the Project class:

    package criteriatest
    
    class Project {
        String name
        Date completionDate
    
        static belongsTo = User
    
        static constraints = {
            completionDate( nullable:true )
        }
    }
    

    This integration test passes (hopefully the asserts explain it)

    package criteriatest
    
    import grails.test.*
    
    class UserTests extends GroovyTestCase {
        protected void setUp() {
            super.setUp()
            User.withSession { session ->
                def tim = new User( name:'tim' )
                def dave = new User( name:'dave' )
    
                [ tim, dave ]*.save()
    
                def project1 = new Project( name:'project 1', completionDate:null )
                def project2 = new Project( name:'project 2', completionDate:new Date() )
    
                tim.addToProjects project1
                tim.addToProjects project2
    
                [ project1, project2 ]*.save()
    
                session.flush()
                session.clear()
            }
        }
    
        protected void tearDown() {
            super.tearDown()
        }
    
        void testQuery() {
            def usersAndIncompleteProjects = User.withCriteria {
                projects {
                    isNull 'completionDate'
                }
                order 'name', 'asc'
            }
    
            // We get two users back (users with no projects get returned as well)
            assert usersAndIncompleteProjects.size() == 2
    
            // First user (dave) has no projects
            assert usersAndIncompleteProjects[0].projects.size() == 0
    
            // Second user (tim) has one project (with a null completionDate)
            assert usersAndIncompleteProjects[1].projects.size() == 1
    
            // Check it's the right project
            assert usersAndIncompleteProjects[1].projects*.name == [ 'project 1' ]
        }
    }
    

    (this is the sql the criteria query executes in this instance):

    select
        this_.id as id1_1_,
        this_.version as version1_1_,
        this_.name as name1_1_,
        projects3_.user_projects_id as user1_3_,
        projects_a1_.id as project2_3_,
        projects_a1_.id as id0_0_,
        projects_a1_.version as version0_0_,
        projects_a1_.completion_date as completion3_0_0_,
        projects_a1_.name as name0_0_ 
    from
        user this_ 
    left outer join
        user_project projects3_ 
            on this_.id=projects3_.user_projects_id 
    left outer join
        project projects_a1_ 
            on projects3_.project_id=projects_a1_.id 
    where
        (
            projects_a1_.completion_date is null
        ) 
    order by
        this_.name asc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following relationship between two domain classes: class Emp { String name
I have two domain classes: class A { String Name ... } class B
I have two classes, and want to include a static instance of one class
I have two following classes: public class User { public virtual Guid Id {
I have two threads, one needs to poll a bunch of separate static resources
I'm struggling to get association right on Grails. Let's say I have two domain
I have two tables: books (id, name, desc, instance_id) instances (id, domain) A user
I have two tables in a legacy database... tblParentTable (int id, string specialIdentifier, ...)
I have two websites that declare an RSS feed, like so: <link rel=alternate type=application/rss+xml
I have two tables Users (name, email, password, instance_id, etc...) example: james bond, james@abc.com,

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.