grails with legacy database. foreign key as part of primery key

776 Views Asked by At

I have faced with problem of using legacy database. I have two tables connected with relation : one – to – many: Table Subscribers:

create table SUBSCRIBERS
(
  SUBSCRIBERCODE VARCHAR2(100) not null,
  SUBSCRIBERNAME VARCHAR2(100),
  ACTIVE         CHAR(1) default 'Y' not null
)
alter table SUBSCRIBERS
  add constraint SUBSCRIBERS_PK primary key (SUBSCRIBERCODE);

Table Subscriberscontacts:

create table SUBSCRIBERSCONTACTS
(
  SUBSCRIBERCODE VARCHAR2(100) not null,
  CONTACTTYPE    VARCHAR2(100) not null,
  ORDER_ID       NUMBER not null,
  CONTACTVALUE   VARCHAR2(100) not null,
  ACTIVE         CHAR(1) default 'Y' not null
)
alter table SUBSCRIBERSCONTACTS
  add constraint SUBSCRIBERSCONTAC1 primary key (SUBSCRIBERCODE, CONTACTTYPE, ORDER_ID);
alter table SUBSCRIBERSCONTACTS
  add constraint SUBSCRIBER_SUBS_FK foreign key (SUBSCRIBERCODE)
  references SUBSCRIBERS (SUBSCRIBERCODE);

Table SUBSCRIBERSCONTACTS has composite primary key – (SUBSCRIBERCODE, CONTACTTYPE, ORDER_ID). Also it has foreign key reference to SUBSCRIBERS (SUBSCRIBERCODE). SUBSCRIBERCODE is part of SUBSCRIBERSCONTACTS primary key.

I created two GORM domain classes:

package tmsconf

class Subscribers {

String id

String subscribercode
String subscribername
String active

static hasMany = [subscriberscontacts:Subscriberscontacts]

static mapping = {
version false
id generator: 'assigned', name: 'subscribercode'
}


static constraints = {
subscribercode(size:1..100, blank: false)
subscribername(size:0..100, nullable: true)
active(size:1..1, blank: false)
}

public void setSubscribercode( String subscribercode ){
this.subscribercode = subscribercode
this.id = subscribercode
}

String toString() {
subscribercode
}

}

package tmsconf

class Subscriberscontacts  implements Serializable{

String id

Subscribers subscribers
String contacttype
Long order_id
String contactvalue
String active

static mapping = {
version false

id composite: ["subscribercode", "contacttype","order_id"], generator: "assigned"

subscribers name : 'subscribercode', column : 'subscribercode', insertable: false, updateable: false

}

public String getSubscribercode()
{
    subscribers
}


static constraints = {
contacttype(size:1..100, blank: false)
contactvalue(size:1..100, blank: false)
active(size:1..1, blank: false)
}

public void setSubscribercode( String subscribercode ){
this.subscribercode = subscribercode
this.id = subscribercode
}


String toString() {
subscribercode
}


}

I generated all required: grails generate-all *.

Then I try to run new grails application and have exception:

Caused by PropertyNotFoundException: Could not find a getter for subscribercode in class tmsconf.Subscriberscontacts
->>  334 | innerRun  in java.util.concurrent.FutureTask$Sync
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    166 | run       in java.util.concurrent.FutureTask
|   1110 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    603 | run       in java.util.concurrent.ThreadPoolExecutor$Worker
^    722 | run . . . in java.lang.Thread

I don’t want to add subscribercode attribute to Subscriberscontacts domain class because if I do so, I will get subscribercode edit field on Create form.

1

There are 1 best solutions below

3
On

Add it, just hide from scaffolding:

class Subscriberscontacts {
  String subscribercode
  ...
  static constraints = {
    subscribercode display: false // or editable: false
  }
}