0
votes

these are my domain class

class Parametro {  
String idParametro
String atributo 
String descripcion
String tipoParametro
String estadoParametro 

static hasMany =[valorParametro : ValorParametro]

static constraints = {        
    idParametro() 
    atributo()
    descripcion()
    tipoParametro inList: ['S','U']
    estadoParametro inList:['A','I']        
  }
  static mapping ={
   table 'parametros'
   version false
   id column:'id_parametro', generator:'assigned', name:'idParametro', type:'string'
   valorParametro column:'id_parametro',joinTable:false
   }
  }


class ValorParametro {

String idValorParametro
String idParametro
String valor
String orden
String estadoValorParametro

static belongsTo =Parametro

static constraints = {
    idValorParametro() 
    idParametro()
    valor()
    orden()
    estadoValorParametro inList:['A','I']   
}

static mapping = {
 table 'valor_parametros'  
 version false
 id generator:'assigned',name:'idValorParametro',column:'id_valor_parametro',type:'string'
 idParametro insertable:false
 idParametro updateable:false
}
}

The tables belong to legacy MYsql DB are:

Create table parametros (

    id_parametro Varchar(10) NOT NULL,
tipo_parametro Char(1) COMMENT 'S=sistema U=Usuario',
atributo Varchar(50),
descripcion Varchar(100),
estado_parametro Char(1),
    Primary Key (id_parametro)) ENGINE = InnoDB;

Create table valor_parametros (
  id_valor_parametro Varchar(10) NOT NULL,
  id_parametro Varchar(10) NOT NULL,
  orden Varchar(5),
  valor Varchar(300),
  estado_valor_parametro Char(1),
      Primary Key (id_valor_parametro)) ENGINE = InnoDB;
Alter table valor_parametros add Foreign Key (id_parametro) references parametros  (id_parametro) on delete  restrict on update  restrict;

The objectives are:

1-) the names of tables and columns as PK and FK are custom

2-) Primary Key and Foreing Key are string and not generated (are entered by the user in form) in Parametro (pk=IdParametro) in ValorParametro ( pk=IdValorParametro , Fk=Id_parametro)

3-) the association is one-to-many (unidirectional) via foreing Key (not Join table) (see JoinTable:false)

THE PROBLEM

in the form associated with ValorParametro not save the value of FK (IdParametro) precisely because the options idParametro insertable: false  idParametro updateable: false

but if they are removed, the following error is obtained:

HTTP Status 500 - Error creating bean with name org.grails.internal.SESSION_FACTORY_HOLDER': Cannot create inner bean '(inner bean)' of

type [org.codehaus.groovy.grails.orm.hibernate.ConfigurableLocalSessionFactoryBean] while

setting bean property 'sessionFactory'; nested exception is

org.springframework.beans.factory.BeanCreationException: Error creating bean with name

'(inner bean)#3': Invocation of init method failed; nested exception is

org.hibernate.MappingException: Repeated column in mapping for entity: crm.ValorParametro

column: id_parametro (should be mapped with insert="false" update="false")

That I can do to make to save the FK fulfilling the objectives?

That configuration is wrong with the mapping?

1

1 Answers

0
votes

That's because Grails already have a domain class attribute called id and you declared the String idParametro. The correct is to use id in all your domain classes and just change the column name:

class Parametro {  
  String id
  String atributo 
  String descripcion
  String tipoParametro
  String estadoParametro 

  static hasMany =[valorParametro : ValorParametro]

  ...

  static mapping = {
    //assigned means that you will set the id before saving new values.
    id column: 'id_parametro', generator: 'assigned'
  }

}

With that you will be able to use GORM methods like Parametro.get("some id").

Then, when you access your domain class instances, always use id:

Parametro par = new Parametro()
par.id = 'my key'