【问题标题】:In GORM How can be stored the value entered in a foreign key in a one to many association without use join table?在 GORM 中,如何在不使用连接表的情况下将外键输入的值存储在一对多关联中?
【发布时间】:2013-12-14 10:34:09
【问题描述】:

这些是我的域类

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 DB 的表是

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-) Primary Key 和 Foreing Key 是字符串,不是生成的(由用户在表单中输入) 在参数中 (pk=IdParametro) 在 ValorParametro (pk=IdValorParametro , Fk=Id_parametro)

3-) 关联是一对多(单向)通过外键(不是连接表) (见 JoinTable:false)

问题

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

但如果将它们删除,则会得到以下错误:

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

键入 [org.codehaus.groovy.grails.orm.hibernate.ConfigurableLocalSessionFactoryBean] 而

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

org.springframework.beans.factory.BeanCreationException: 用名称创建 bean 时出错

'(inner bean)#3': init 方法调用失败;嵌套异常是

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

列:id_parametro(应使用 insert="false" update="false" 映射)

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

映射配置有问题?

【问题讨论】:

    标签: grails grails-orm


    【解决方案1】:

    这是因为 Grails 已经有一个名为 id 的域类属性,并且您声明了 String 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'
      }
    
    }
    

    这样你就可以使用像Parametro.get("some id")这样的GORM方法。

    然后,当您访问域类实例时,请始终使用id

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 2011-08-26
      • 1970-01-01
      相关资源
      最近更新 更多