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 6885477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:38:23+00:00 2026-05-27T05:38:23+00:00

I have the following domain classes: Holiday : class Holiday { String justification User

  • 0

I have the following domain classes:

Holiday:

class Holiday {
    String justification
    User user
    //static belongsTo = User
        static constraints = {
        }
}

User:

class User {
    String login
    String password
    static hasMany = [ holidays : Holiday ]
        static constraints = {
    }
}

I have created a one-many relationships between Holiday and User. Note that I haven’t included belongsTo on the Holiday class. Now I have written the following integration test:

void testWithoutBelongsTo() {       
        def user1 = new User(login:"anto", password:"secret")
        user1.save()
        def holiday1 = new Holiday(justification:"went to trip")
        holiday1.save()
        user1.addToHolidays(holiday1)
        assertEquals 1, User.get(user1.id).holidays.size()
        user1.delete()
        assertFalse User.exists(user1.id)
        assertFalse Holiday.exists(holiday1.id)
    }

Clearly in the above test case, I’m deleting only the user1 instance, but when I run with assert statements I can see that GORM have implicitly deleted holiday1, too. And my Test cases have PASSED! How this can happen, even though I haven’t given belongsTo keyword in Holiday class?

I’m using Grails version 1.3.7.

  • 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-27T05:38:24+00:00Added an answer on May 27, 2026 at 5:38 am

    holiday1 was never saved as it does not validate: property user is neither set nor nullable.

    Here is how your code in Holiday.groovy should look like:

    class Holiday {
        String justification
        User user
        //static belongsTo = User
        static constraints = {
            user(nullable: true)
        }
    }
    

    And your test, with property user set properly in holiday1:

    void testWithoutBelongsTo()
    {
        def user1 = new User(login:"anto", password:"secret")
        user1.save(failOnError: true)
        def holiday1 = new Holiday(justification:"went to trip", 
                                   user: user1) // Set user properly
        holiday1.save(failOnError: true)
        user1.addToHolidays(holiday1)
        assert 1, User.get(user1.id).holidays.size()
        holiday1.user = null  // Unset user as otherwise your DB
                              // won't be happy (foreign key missing)
        user1.delete()
        assert ! User.exists(user1.id)
        assert Holiday.exists(holiday1.id)
     }
    

    In order to eliminate validation errors quickly in your tests, always use save(failOnError: true). It will throw an exception if your object do not validate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have the following domain classes class User { static hasMany = [books:Book]
i have the following (simplified) domain classes class Filter { String name static hasMany
I have a structure with the following domain classes: class Service{ static hasMany=[serviceRequirements:ServiceRequirement]} And
I have two grails domain classes Class MultipleChoiceQuestion { String question static constraints =
Hello I have the follwing domain classes. class Student { int age static hasMany
I have the following relationship between two domain classes: class Emp { String name
I have the following domain: public class FileInformation { public String FileName; public String
I have the following Grails domain class: class Product { String name Float basePrice
In my domain model I have following Classes.A 'UserProfile' has one 'SecurityPrincipal' class SecurityPrincipal{
I have two following classes: public class User { public virtual Guid Id {

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.