difference between hasmany belongsTo , namedQuery belongsTo and hasmany instantiation grails domain

StackOverflow https://stackoverflow.com/questions/21802423

문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top