复合密钥和grails 2.2.5中的映射与遗留数据库

时间:2015-08-12 10:13:13

标签: postgresql grails groovy composite-key legacy-database

我有4张桌子。 osiguranje_paket,atribut,tip_unosa,razna_polja。 osiguranje_paket,atribut,tip_unosa是razna_polja表的父母。 razna_polja表具有复合键,它由两个主键组成(osgp_id = osiguranje_paket表+ atr_id = atribut表)。它们之间的关系是一对多双向的,我正在使用Legacy PostgreSQL数据库和动态脚手架,我无法对数据库或表或任何东西进行任何更改。如何映射我的类以使用复合键,我需要在域中添加或更改哪些内容?任何帮助,将不胜感激。

CREATE TABLE revoco.osiguranje_paket
  (
  osgp_id serial NOT NULL,
  osg_id integer NOT NULL,
  osgp_napomena character varying(500),
  tpo_id integer NOT NULL,
  osgp_link character varying(155),
  osgp_oznaka character varying(10),
  CONSTRAINT osgp_pk PRIMARY KEY (osgp_id),
  CONSTRAINT osg_osgp_fk FOREIGN KEY (osg_id)
      REFERENCES revoco.osiguranje (osg_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT tpo_osgp_fk FOREIGN KEY (tpo_id)
      REFERENCES revoco.tip_osiguranja (tpo_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
) 

CREATE TABLE revoco.atribut
(
  atr_id serial NOT NULL,
  atr_naziv character varying(155) NOT NULL,
  lab_id integer,
  atr_rbr integer,
  CONSTRAINT atr_pk PRIMARY KEY (atr_id),
  CONSTRAINT atr_lab_labela_fk FOREIGN KEY (lab_id)
      REFERENCES common.labela (lab_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

CREATE TABLE common.tip_unosa
(
  tpu_id serial NOT NULL,
  tpu_val character varying(32) NOT NULL,
  CONSTRAINT tpu_pk PRIMARY KEY (tpu_id),
  CONSTRAINT tpu_vrijednost_unique UNIQUE (tpu_val)
)

CREATE TABLE common.razna_polja
(
  osgp_id integer NOT NULL,
  atr_id integer NOT NULL,
  tpu_id integer NOT NULL,
  rap_odjel integer NOT NULL DEFAULT 0,
  rap_vidljiv boolean NOT NULL DEFAULT true,
  CONSTRAINT rap_pk PRIMARY KEY (osgp_id, atr_id),
  CONSTRAINT rap_atr_atribut_fk FOREIGN KEY (atr_id)
      REFERENCES revoco.atribut (atr_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT rap_osgp_paket_fk FOREIGN KEY (osgp_id)
      REFERENCES revoco.osiguranje_paket (osgp_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT rap_tpu_tip_unosa_fk FOREIGN KEY (tpu_id)
      REFERENCES common.tip_unosa (tpu_id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT rap_ispravan_odjel_ck CHECK (rap_odjel >= 0 AND rap_odjel <= 1)
)

这是我的域类

OsiguranjePaket.groovy

import common.RaznaPolja

   class OsiguranjePaket {

    Integer id
    String osgp_napomena
    String osgp_link
    String osgp_oznaka


    static belongsTo = [osg: Osiguranje, tpo: TipOsiguranja]
    static hasMany = [raznaPolja: RaznaPolja]

        String toString(){
            "${osgp_oznaka}"
        }

    static fetchMode = [raznapolja: 'eager']


    static constraints = {

        id(unique: true)
        osgp_link (nullable: true, blank: false, size: 0..155)
        osgp_napomena (nullable: true, blank: false, size: 0..500)
        osgp_oznaka (nullable: true, blank: false, size: 0..10)     
    }

    static mapping = {
        table name: 'osiguranje_paket', schema: 'revoco'
        version false       
        id generator :'identity', column :'osgp_id', type:'integer'
    }   
}

Atribut.groovy

import common.RaznaPolja
import common.Labela

class Atribut {

Integer id
String atr_naziv
Integer atr_rbr



static hasMany = [raznaPolja: RaznaPolja]
static belongsTo = [lab: Labela]

static fetchMode = [raznaPolja: 'eager']


String toString(){
    "${atr_naziv}"
}

static mapping = {
    table name: "atribut", schema: "revoco"
    version false
    id generator :'native', column :'atr_id'
}

static constraints = {

    id(blank: false, unique: true)
    atr_naziv (blank: false, size: 0..155)
    atr_rbr (nullable: true)
    }
}

TipUnosa.groovy

class TipUnosa {

    Integer id
    String tpu_val


    static hasMany = [raznaPolja: RaznaPolja]

    static fetchMode = [raznaPolja: 'eager']

        String toString(){
            "${tpu_val}"
        }

    static constraints = {
        id (blank:false, size: 0..10)
        tpu_val (blank:false, unique:true, size:0..32)
    }

    static mapping = {
        table name: "tip_unosa", schema: "common"
        version false
        id generator :'identity', column :'tpu_id', type:'integer'
}

}

RaznaPolja.groovy

import java.io.Serializable;

import revoco.Atribut
import revoco.OsiguranjePaket

class RaznaPolja implements Serializable  {


    Integer rap_odjel
    Boolean rap_vidljiv     

//without this getting common.RaznaPolja(unsaved) 
    String toString(){
        "${id}" //Getting null
    }   

    static belongsTo = [atr: Atribut, osgp: OsiguranjePaket, tpu: TipUnosa]

    static mapping = {
        table name: 'razna_polja', schema: 'common'

        id composite: ['osgp', 'atr']
//      cache usage:'read-only'
        version false
        rap_odjel column: 'rap_odjel', type: 'integer'
        rap_vidljiv column:'rap_vidljiv', type: 'boolean'
}
}

1 个答案:

答案 0 :(得分:0)

复合键

要设置组合键,您需要使用域类的映射属性,并且您的域类必须实现Serializable接口。以下是Grails documentation中的一个示例。

import org.apache.commons.lang.builder.HashCodeBuilder
class Person implements Serializable {

    String firstName
    String lastName

    boolean equals(other) {
        if (!(other instanceof Person)) {
            return false
        }

        other.firstName == firstName && other.lastName == lastName
    }

    int hashCode() {
        def builder = new HashCodeBuilder()
        builder.append firstName
        builder.append lastName
        builder.toHashCode()
    }

    static mapping = {
        id composite: ['firstName', 'lastName']
    }
}

数据库映射

域类映射属性还用于更改域类映射到的数据库表和列,如您所见here

协会

至于表格之间的关系,documentation可以为您提供一些线索。您可能必须在此处添加一些映射域类以创建所需内容,但Grails关联应该能够满足您的需求。