n92
n92

Reputation: 7592

How to save objects having hasOne and belongsTo relationship in grails

I have defined three classes.

1)

class Office extends Organization {

   String name
   DistanceChart distanceChart 
   static constraints = {
      name(nullable : false)
      distanceChart(nullable:true)
   }
   static mapping = {
      officeRoles(cascade:'all')
   }

   public String toString() {
      name
   }
}

2)

class DistanceChart {
   VehicleClassification classification1
   VehicleClassification classification2
   VehicleClassification classification3
   VehicleClassification classification4
   VehicleClassification classification5
   List<PointDistanceMapping> pointDistanceMapping =new ArrayList<PointDistanceMapping>()

   static contraints ={
      classification1(nullable:true);
      classification2(nullable:true)
      classification3(nullable:true)
      classification4(nullable:true)
      classification5(nullable:true)
      pointDistanceMapping()
   }
   static belongsTo = [office:Office]
   static hasMany = [pointDistanceMapping : PointDistanceMapping]
   static mapping = {
      pointDistanceMapping cascade: "all"
   }
   public String toString() {
      id
   }   
}

3)

class PointDistanceMapping {
   Point point
   float distance1
   float distance2
   float distance3
   float distance4
   float distance5

   static  constraints = {
      point()
      distance1(nullable:true)
      distance2(nullable:true)
      distance3(nullable:true)
      distance4(nullable:true)
      distance5(nullable:true)
   }
   static belongsTo = [distanceChart : DistanceChart]
   public String toString() {
      id
   }
}

I have two doubts :

1) Do i need to specify the relation in both the classes i.e hasOne or hasMany and belongsTo(Bi-Directional) .so what is the good practice to define the relationships in grails .

2) My second question does grails automatically saves the child objects . in this context .I am getting the pointDistanceMapping object as array ,does the grails saves the pointDistanceMapping array values to database and will take care of setting the foreign key distance_chart_id in the point_distance_mapping

Does grails sets the distance_chart_id in the office table when i save the DistanceChart Object

If grails does not save the associated objects means when i save the DistanceChart ,pointdistancemapping gets saved .Then how to do this .

How to save the distance_chart_id in the office table when the distanceChart gets saved.

Upvotes: 1

Views: 2707

Answers (1)

Victor Sergienko
Victor Sergienko

Reputation: 13475

  1. Whatever is more convenient for you to iterate. Generally, if your code doesn't need a relationship property (YAGNI) - don't create one. If the collection is very big, don't create a collection property anyway, better use dynamic findAll*() methods to operate on relationship.

  2. Grails cascading is controlled by belongsTo and cascade mapping DSL:

    "cascade - [...] Can be one or more (comma-separated) of all, merge, save-update, delete, lock, refresh, evict, replicate or all-delete-orphan (one-to-many associations only). By default GORM configures a cascading policy of all in the case where one entity belongsTo another"

Upvotes: 1

Related Questions