0

It seems that it is impossible with Grails to map a domain class to a database table that has is a foreign key column named 'interface'. In my case there's a relationship with two tables INTERFACE and INTERFACE_DETAILS. They are legacy databases and column names can not be changed or added.

Put it simple INTERFACE_DETAILS has an FK column 'INTERFACE' referring to PK INTERFACE.ID

class Interface {
    String id;  

    static mapping = {
        table "INTERFACE"
        version false
        id generator: 'assigned'
        id column: 'id', sqlType:"varchar2(20)"
    }

class InterfaceDetails {

    Interface iface;    
    static belongsTo = [iface: Interface]

    static mapping = {
        table "INTERFACE_DETAILS"
        version false        
        id column: 'interface'
        iface column: 'INTERFACE', sqlType:"varchar2(20)", insertable: false, updateable: false
    }
}

I'm currently using H2 database. When I try to add a row to InterfaceDetails this error occurs:

Referential integrity constraint violation: "FK351396597E3917F9: PUBLIC.INTERFACE_DETAILS FOREIGN KEY(INTERFACE) REFERENCES PUBLIC.INTERFACE(ID)"; SQL statement: insert into INTERFACE_DETAILS (interface) values (null) [23506-164]

I wonder why hibernate adds null for 'interface' value ?

I do have to have line : "id column: 'interface'" because in other case Hibernate will generate an extra column: "iface_id" and this is not suitable. Existing database columns can not be changed.

This is quite puzzling. Please tell me that Grails can handle this situation.

stripped down schema:

create table interface (id varchar2(20) not null,primary key (id));
create table interface_details (interface varchar2(20) not null, name varchar(255) not null, primary key (interface));
alter table interface_details add constraint FK_INTERFACE foreign key (interface) references interface;
4

0 回答 0