views:

179

answers:

2

I have two domains :

class CodeSet { 

  String id
  String owner
  String comments
  String geneRLF
  String systemAPF

  static hasMany = [cartridges:Cartridge]

    static constraints = {
      id(unique:true,blank:false)
    }

    static mapping = {
      table 'code_set'
      version false
      columns {
         id column:'code_set_id', generator: 'assigned'
         owner column:'owner'
         comments column:'comments'
         geneRLF column:'gene_rlf'
         systemAPF column:'system_apf'
      }
  }

and :

class Cartridge {

  String id
  String code_set_id
  Date runDate

  static belongsTo = CodeSet

    static constraints = {
      id(unique:true,blank:false)
    }

      static mapping = {
      table 'cartridge'
      version false
      columns {
         id column:'cartridge_id', generator: 'assigned'
         code_set_id column:'code_set_id'
         runDate column:'run_date'
      }
  }

Actually, with those models, I get tables :
- code_set,
- cartridge,
- and table : code_set_cartridge (two fields : code_set_cartridges_id, cartridge_id)

I would like to not have code_set_cartridge table, but keep relationship :
code_set --> 1:n --> cartridge

In other words, how can I keep association between code_set and cartridge without intermediate table ? (using code_set_id as primary key in code_set and code_set_id as foreign key in cartridge).

Mapping with GORM can be done without intermediate table?

A: 

I was using a bidirectional one-to-many mode, but I can also use an unidirectional mode.

So for domain CodeSet, the fix is :

class CodeSet { 

  String id
  String owner
  String comments
  String geneRLF
  String systemAPF

  Cartridge cartridge

    static constraints = {
      id(unique:true,blank:false)
    }

    static mapping = {
      table 'code_set'
      version false
      id column:'code_set_id', generator: 'assigned'
      columns {
         owner:'owner'
         comments:'comments'
         geneRLF:'gene_rlf'
         systemAPF:'system_apf'
      }
  }

But, I still confuse with bidirectional and unidirectional mode ?
Somebody, can show me a good example (to help me to understand) ? Thanks

Fabien Barbier
+2  A: 

It works if you change the belongsTo declaration. Rather than having just a reference to the CodeSet's id, you can name the instance in the belongsTo and you'll get the reference to the instance and avoid the join table. I also removed redundant mappings:

class Cartridge {

   String id
   Date runDate

   static belongsTo = [codeSet: CodeSet]

   static mapping = {
      version false
      id generator: 'assigned'
      codeSet column:'code_set_id'
   }
}

class CodeSet { 

   String id
   String owner
   String comments
   String geneRLF
   String systemAPF

   static hasMany = [cartridges:Cartridge]

   static mapping = {
      version false
      id generator: 'assigned'
      geneRLF column:'gene_rlf'
      systemAPF column:'system_apf'
   }
}
Burt Beckwith
By using unidirectional mode, there's no intermediate table also. But I'm confuse with bidirectional and unidirectional mode. In which case I should use bidirectional and unidirectional mode ? An Example ? Thanks
Fabien Barbier
The decision really comes down to if you want to have access to the mapped set and the reference, or just the reference. My preference is unidirectional since you can always find the CodeSet instances that reference a Cartridge via "CodeSet.findAllByCartridge(this)" or "CodeSet.findAllByCartridge(cartridge)".
Burt Beckwith
In your example, you are using an unidirectional mode ? Right ? By using "Cartridge cartridge" I have also an unidirectional mode. What is the difference ? Is unidirectional mode and bidirectional mode affect db table organization ?
Fabien Barbier
The hasMany on one side and the belongsTo on the other side create a bidirectional mapping (since each side can get to the other). Wit h "Cartridge cartridge" you're creating a bidirectional 1-1.Run 'grails schema-export' after trying various combinations to see what the different generated DDL looks like.
Burt Beckwith