0
votes

In grails, I have two domain classes Question and QuestionOption.One question have many options.Each option has a question. I went through 4 different scenario. These scenarios has combination of instantiation-namedQuery , belongsTo-namedQuery , instantiation-hasMany and belongsTo-hasMany.

Scenario 1:

class Question
{
  String quesText
  static constraints={}
  static namedQueries={
    getAllQuestionOptions{question->
      return QuestionOption.findAllWhere(question:question)
    }
  }
}

class QuestionOption
{
  String optionText
  Question question
  static constraints={}
}

.

Scenario 2:
class Question
{
  String quesText
  static constraints={}
  static namedQueries={
    getAllQuestionOptions{question->
      return QuestionOption.findAllWhere(question:question)
    }
  }
}

class QuestionOption
{
  String optionText
  static constraints={}
  static belongsTo=[question:Question]
}

.

Scenario 3:
class Question
{
  String quesText
  static constraints={}
  static hasMany=[questionOption:QuestionOption] 
}

class QuestionOption
{
  String optionText
  Question question
  static constraints={}
}

.

Scenario 4:
class Question
{
  String quesText
  static constraints={}
  static hasMany=[questionOption:QuestionOption] 
}

class QuestionOption
{
  String optionText
  static constraints={}
  static belongsTo=[question:Question]
}

I used all these scenarios with mysql and all resulted with same schema

mysql> desc question;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| version     | bigint(20)   | NO   |     | NULL    |                |
| ques_text   | varchar(255) | NO   |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> desc question_option;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| version     | bigint(20)   | NO   |     | NULL    |                |
| option_text | varchar(255) | NO   |     | NULL    |                |
| question_id | bigint(20)   | NO   | MUL | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

What is difference between using these scenarios in the terms of cascading , unidirectional-bidirectional and aggregation-composition? Bidirectional means when both entity knows about each other.Unidirectional means first entity know about second but reverse is not true. To understand cascading I referred this: http://www.mkyong.com/hibernate/hibernate-cascade-example-save-update-delete-and-delete-orphan/ . Please help

1

1 Answers

0
votes

belongsTo and hasMany associations in Grails effect the cascading behavior when it relates to updating/deleting instances. It has no effect on the named queries or querying in general.