2013-06-18 46 views
5

我有一個存儲的代理層次結構的表:Grails的/格姆映射非標準-命名外鍵

create table agent (
    agent_id int not null, 
    agent_name varchar(255), 
    agent_parent_id, 
    constraint pk_agent primary key (agent_id)); 

alter table agent 
    add constraint fk_agent_agent foreign key (agent_parent_id) references (agent_id); 

我仿照它:

class Agent { 
    String agentName 
    Agent agentParent 
    static mapping = { 
    id column: 'agent_id' 
    id generator: 'sequence', params: [sequence: 'agent_id_seq'] 
    } 
} 

每劑可有許多特性:

create table agent_property (
    agent_property_id int not null, 
    agent_property_name varchar(255), 
    agent_id int, 
    constraint pk_agent_property primary key (agent_property_id)); 

alter table agent_property (
    add constraint fk_agent_property_agent foreign key (agent_id) references agent(agent_id); 

我建模,作爲:

class AgentProperty { 
    String agentPropertyName 
    static hasOne = [agent: Agent] 
    static mapping = { 
    id column: 'agent_property_id' 
    id generator: 'sequence', params: [sequence: 'agent_property_id_seq'] 
    } 
} 

我創建了一個視圖輕鬆查看代理商的層次結構:

create view pathogen as 
    select c.agent_id as id, a.agent_name as genus, b.agent_name as species, c.agent_name as strain, d.agent_name as toxin 
    from agent a 
    left join agent b on a.agent_id = b.agent_parent_id 
    left join agent c on b.agent_id = c.agent_parent_id 
    left join agent d on c.agent_id = d.agent_parent_id 
    where a.agent_parent_id is null; 

我的問題是在模擬病原體視圖。我已經這樣做了:

class Pathogen { 
    String genus 
    String species 
    String strain 
    String toxin 
    static hasMany = [agentProperties: AgentProperty] 
} 

這意味着agent_property表中有一個外鍵'pathogen_id'。但事實並非如此。 外鍵是agent_id。 我想AgentProperty涉及到病菌對AGENT_ID好像有約束:

alter table agent_propery 
    add constraint fk_agent_property_pathogen foreign key (agent_id) references pathogen (id); 

我試圖暗示財產agentProperties映射在我的Pathgeon類AGENT_ID,是這樣的:

static mapping = { 
    agentProperties column: agent_id // or AgentProperty.agent 
} 

但那沒用。

我該如何告訴GORM使用agent_property.agent_id作爲外鍵?

回答

6

解決我最初的問題是我沒有把agent_id放在引號中。

agentProperties column: 'agent_id' 

這現在工作:

class Pathogen { 
    String genus 
    String species 
    String strain 
    String toxin 

    static hasMany = [agentProperties: AgentProperty] 

    static mapping = { 
    // use agent_id to releate to AgentProperty 
    agentProperties column: 'agent_id' 
    } 
} 

class AgentProperty { 
    String agentPropertyName 

    static belongsTo = [agent: Agent] 
    static hasOne = [pathogen: Pathogen] 

    static mapping = { 
    id column: 'agent_property_id' 
    id generator: 'sequence', params: [sequence: 'agent_property_id_seq'] 
    // use agent_id to relate to Pathogen 
    pathogen column: 'agent_id', insertable: false, updateable: false 
    } 
} 
0

你的域類需要修改的點點粘到你的數據庫有設計,

class Agent { 
    String agentName 
    Agent agentParent 

    //agent_id Foreign Key to AgentProperty. Agent has many AgentProperties 
    static hasMany = [agentProperties: AgentProperty] 

    static mapping = { 
    id column: 'agent_id' 
    id generator: 'sequence', params: [sequence: 'agent_id_seq'] 
    } 
} 

class AgentProperty { 
    String agentPropertyName 

    //AgentProperty belongs to an Agent. Cascade delete is enabled 
    static belongsTo = [agent: Agent] 
    static mapping = { 
    id column: 'agent_property_id' 
    id generator: 'sequence', params: [sequence: 'agent_property_id_seq'] 
    } 
} 

class Pathogen { 
    String genus 
    String species 
    String strain 
    String toxin 

    //like foreign key pathogen_id in agent table 
    static hasMany = [agents: Agent] 
} 

您可以從Pathogen通過Agent得到AgentProperty舉行。

如果我正確地讀你的問題,那麼這就是你需要的。

Pathogen hasMany Agents 
Agent hasMany AgentProperty 
+0

感謝dmahapatro。代理表中沒有pathogen_id,我無法更改。另外,病原體對代理不是一對多的。如果有的話,它會是1:1。病原體是數據庫中的一個視圖。你可以說病原體有一個代理病原體.id == agent_id,但每個代理沒有一個病原體。 –

+0

@MarkAnderson抱歉,我沒有意識到'Pathogen'是一個數據庫'視圖'。你是否真的需要這個視圖,因爲你可以在'Agent'和'AgentProperty'上使用標準查詢來獲得你想要的。視圖中使用的選擇查詢可以轉換爲[標準](http://grails.org/doc/2.2.1/ref/Domain%20Classes/createCriteria.html)。如果您要使用視圖[這裏是你如何訪問它](http://stackoverflow.com/questions/425294/sql-database-views-in-grails)。 – dmahapatro

+0

爲了模型,我可以做'類Pathogn {字符串屬;絃樂種類;弦應變;毒素;代理商};靜態映射= [列代理:'agent_id']} –