Question

New to Groovy. So I have a superclass

class AbstractClass {
User user
}

and a subclass

class Category extends AbstractClass {
String name
}

when I try to create object in the BootStrap.groovy (I'm using Grails) like:

User user1 = new User(...).save(failOnError: true)
// I know user1 is properly created
def category1 = new Category(User: user1, name: 'alice').save(failOnError: true)

Well, my problem is user field is not being set. It's null. This has been changed: previously, instead of saving the User, I just saved its id (Long id) and it was working. Is there any magic I'm missing here?

When I change the code to:

def category1 = new Category(User: user1, name: 'alice')
category1.setUser user1
category1.save(failOnError: true)

it works nicely, so I guess there must be something I'm missing here.

Thanks for any help!

Was it helpful?

Solution

In

class AbstractClass {
  User user
}

the class has a property whose name is user (small u) and whose type is User (capital U), therefore

new Category(User: user1, name: 'alice')

should be

new Category(user: user1, name: 'alice')

to match the property name. Remember Groovy, like Java, is case sensitive.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top