How do I make Grails GORM respect the DB schema?

I have two areas:

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 these models I get tables:
- code_set,
- cartridge,
- and table: code_set_cartridge (two fields: code_set_cartridges_id, cartridge_id)

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

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

GORM mapping can be done without staging table?

+2


a source to share


2 answers


It works if you change the applyTo declaration. Rather than just referring to the CodeSet id, you can name the instance in the belongsTo attribute and you will get a 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'
   }
}

      

+5


a source


I have used one-to-many bidirectional mode, but I can also use unidirectional mode.

So, for the CodeSet domain, 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 am I still confused with bi-directional and unidirectional mode?
Can anyone show me a good example (to help me understand)? Thanks to

+1


a source







All Articles