Can someone please help me to figure out how to create domain classes for simple one-to-one mapping in grails !
let say we have 2 tables (oracle):
create table table_a(long_common_id_name number(5) primary key using index,
notes varchar2(10 byte),
update_seq number(3)not null );
create table table_b (long_common_id_name number(5) primary key using index,
extra_notes varchar2(200 byte),
update_seq number(3) not null);
alter table table_b add (constraint table_b_fk foreign key (long_common_id_name)
references table_a (long_common_id_name));
I created 2 domain classes:
class TableA {
static mapping = {
table 'table_a'
columns {
id column:'LONG_COMMON_ID_NAME'
data column:'NOTES'
version column:'UPDATE_SEQ'
}
}
String data
TableB extraData
}
class TableB {
static mapping = {
table 'table_b'
columns {
id column:'LONG_COMMON_ID_NAME'
data column:'EXTRA_NOTES'
version column:'UPDATE_SEQ'
}
}
String data
}
This particular definition is not correct. Grails (or Hibernate) builds an incorrect SQL for TableA :
select this_.LONG_COMMON_ID_NAME as LONG1_66_0_, this_.UPDATE_SEQ as UPDATE2_66_0_, this_.NOTES as NOTES66_0_, this_.extra_data_id as extra4_66_0_ from table_a this_
I’ve tried many things: belongsto , hasManey with unique but nothing seems to work.
Thanks in advance.
Answering my own question, maybe someone will find it useful …
I’m writing a quick web-ui against our legacy database, so instead of writing lots of SQLs to answer user questions I want automate it. I can not modify data structure and access through UI will be read only.
Here is solution/workaround that I can live with:
domain/TableA.groovy
domain/TableB.groovy
the form in “views/tableA/create.gsp” has input fields for TableA and TableB properties, so I will get data for TableA and TableB instances
In the “controllers/TableAController.groovy” I manually update/delete/load TableB instance