Postgresql Grails2.2.5中使用遗留数据库的组合键和映射

Postgresql Grails2.2.5中使用遗留数据库的组合键和映射,postgresql,grails,groovy,composite-key,legacy-database,Postgresql,Grails,Groovy,Composite Key,Legacy Database,我有四张桌子。osiguranje_paket、atribut、tip_unosa、razna_polja。osiguranje_paket、atribut、tip_unosa是razna_polja table的父母。 razna_polja表具有由两个主键组成的复合键(osgp_id=osiguranje_paket表+atr_id=atribut表)。它们之间的关系是一对多的双向关系,我使用的是带有动态支架的遗留PostgreSQL数据库,我不能对数据库、表或任何东西进行任何更改。我如何映

我有四张桌子。osiguranje_paket、atribut、tip_unosa、razna_polja。osiguranje_paket、atribut、tip_unosa是razna_polja table的父母。 razna_polja表具有由两个主键组成的复合键(osgp_id=osiguranje_paket表+atr_id=atribut表)。它们之间的关系是一对多的双向关系,我使用的是带有动态支架的遗留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)
)
但是,非常棒

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)
    }
}
蒂普诺萨

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'
}
}
复合键 要设置复合密钥,您需要使用域类的映射属性,并且域类必须实现可序列化接口。这里有一个来自Grails的示例

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

联想 至于表之间的关系,可以给你一些线索。您可能需要在这里或那里添加一些映射域类来创建所需的内容,但Grails关联应该能够满足您的需要。

Composite key 要设置复合密钥,您需要使用域类的映射属性,并且域类必须实现可序列化接口。这里有一个来自Grails的示例

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

联想 至于表之间的关系,可以给你一些线索。您可能需要在这里或那里添加一些映射域类来创建所需的内容,但是Grails关联应该能够满足您的需要

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