0
votes

I'm creating a (theoretically) simple hasMany relationship within a domain class. I have two tables with a foreign key relationship between the two. Table 1's domain object is as follows:

Functionality{
String id
static hasMany = [functionalityControllers:FunctionalityController]
static mapping = 
 {
   table 'schema.functionality'
   id column:'FUNCTIONALITY_NAME', type:'string', generator:'assigned'
   version false
 }
}

and domain object 2

FunctionalityController
{
  String id
  String functionalityName
  String controllerName

  static mapping = 
  {
     table 'schema.functionality_controller'
     id column:'id', type:'string', generator:'assigned'
     version:false
  }
}

The issue I am having is that when I have the hasMany line inside of the Functionality domain object, the app won't start (both the app and the integration tests). The error is org.springframework.beans.factory.BeanCreationException leading to Invocation of init method failed; nested exception is java.lang.NullPointerException.

Any help would be appreciated. UPDATE: *Working Domains*:

class Functionality {

String id
static hasMany = [functionalityConts:FunctionalityCont]
static mapping =
{
    table 'schema.functionality'
    id column:'FUNCTIONALITY_NAME', type: 'string', generator: 'assigned'
    functionalityConts( column:'functionality_name')
    version false;
}
}

and

class FunctionalityCont {

String id
String functionalityName
String controllerName
static belongsTo = [functionality: Functionality]
static contraints = {

}
static mapping =
{
    table 'schema.functionality_controller'
    id column:'id', type: 'string', generator: 'assigned'
    functionality(column:'FUNCTIONALITY_NAME')
    version false;
}
}
2

2 Answers

3
votes

Try adding

static belongsTo = [functionality: Functionality]

to your FunctionalityController class. I suspect there is more to your error than what you've shown, but generally a hasMany needs an owning side to it. Since that is where the foreign key actually lives.

3
votes

Well 2 things...

1.I'm not sure but I guess that your domain class with the prefix 'Controller' maybe is the responsible, this is because grails is convention over configuration and by convention the controller class ends with Controller prefix and are in the controller folder, in this case is a lil' confusing

2.In GORM and in this case the relationship between objects can be unidirectional or bidirectional, is your decision to choose one, but in both cases there are different implementations, the class Functionality(btw is missing the 'class' word) has the right relationship to FunctionalityController through hasMany, but FunctionalityController doesn't knows about the relationship, so you can implement:

// For unidirectional
static belongsTo = Functionality
// For bidirectional
static belongsTo = [functionality:Functionality]
// Or put an instance of Functionality in your domain class,
// not common, and you manage the relationship
Functionality functionality

So check it out and tell us pls... Regards