Grails 在GORM中,如何在不使用联接表的情况下将输入外键的值存储在一对多关联中?

Grails 在GORM中,如何在不使用联接表的情况下将输入外键的值存储在一对多关联中?,grails,gorm,Grails,Gorm,这些是我的域类 class Parametro { String idParametro String atributo String descripcion String tipoParametro String estadoParametro static hasMany =[valorParametro : ValorParametro] static constraints = { idParametro() atributo() d

这些是我的域类

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
}
}
属于传统MYsql数据库的表是

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;
目标是:

1-)PK和FK等表和列的名称是自定义的

2-)主键和外键为字符串,不生成(由用户在表单中输入) 在ValorParametro(pk=IdValorParametro,Fk=Id_Parametro)的参数中(pk=IdParametro)

3-)通过foring键(非联接表)关联为一对多(单向) (参见JoinTable:false)

问题

在与ValorParametro关联的表单中,不保存FK(IdParametro)的值 正是因为选项idParametro insertable:false idParametro updateable:false

但如果将其删除,则会出现以下错误:

HTTP状态500-创建名为org.grails.internal.SESSION_FACTORY_HOLDER的bean时出错:无法创建的内部bean'(内部bean)'

键入[org.codehaus.groovy.grails.orm.hibernate.ConfigurableLocalSessionFactoryBean],同时

设置bean属性“sessionFactory”;嵌套异常是

org.springframework.beans.factory.BeanCreationException:创建名为的bean时出错

“(内部bean)#3”:调用init方法失败;嵌套异常是

org.hibernate.MappingException:实体:crm.ValorParametro的映射中重复列

列:id\u参数(应使用insert=“false”update=“false”进行映射)

我能做些什么来拯救FK实现目标?


该配置与映射错误?

这是因为Grails已经有一个名为
id
的域类属性,并且您声明了
字符串idParametro
。正确的方法是在所有域类中使用
id
,只需更改列名:

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'
  }

}
这样,您就可以使用GORM方法,如
Parametro.get(“some id”)

然后,在访问域类实例时,始终使用
id

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