Referencing a composite foreign key in a domain class

View: New views
3 Messages — Rating Filter:   Alert me  

Referencing a composite foreign key in a domain class

by eas :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have a domain class Person with a composite primary key
I have a one to many relationship between Person and Address.

static hasMany = [address: Address]
static mapping = {
table "Person"
version false
id composite:["firstName", "lastName"]
columns{
...
...
}
}


In the address class, how do I map the composite primary key from Person as a foreign key. In the database, the foreign key columns in the Address table are fk_first_name and fk_last_name.

static belongsTo = [person:Person]
static mapping = {
table "Address"
version false
columns {
person column: ["fk_first_name", "fk_last_name"]
//The mapping above for the person column does not work
}
}

I'm not able to find any documentation regarding this in the grails user guide. I would appreciate any help that you can give me.

Thanks,
eas


Re: Referencing a composite foreign key in a domain class

by BluSynergy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Try something like
static belongsTo = [person:Person]
static mapping = {
   person type: Person, {
       column name: "fk_first_name"
       column name "fk_last_name"
      }
 }
}


eas wrote:
I have a domain class Person with a composite primary key
I have a one to many relationship between Person and Address.

static hasMany = [address: Address]
static mapping = {
table "Person"
version false
id composite:["firstName", "lastName"]
columns{
...
...
}
}


In the address class, how do I map the composite primary key from Person as a foreign key. In the database, the foreign key columns in the Address table are fk_first_name and fk_last_name.

static belongsTo = [person:Person]
static mapping = {
table "Address"
version false
columns {
person column: ["fk_first_name", "fk_last_name"]
//The mapping above for the person column does not work
}
}

I'm not able to find any documentation regarding this in the grails user guide. I would appreciate any help that you can give me.

Thanks,
eas
http://www.BluSynergy.com
Solutions for Subscription Billing and Payment Processing

Re: Referencing a composite foreign key in a domain class

by eas :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for the help. I tried it, but it didn't work. I got the error message shown below.

Here are the two classes

class RuleExecution implements Serializable{
   
    Integer processExecutionId
    Double processStepSeq    
    Integer recordsProcessed
    String exceptionMessage
    String exceptionStatus
    Integer exceptionCount
    RuleActual ruleActual
     
    static hasMany = [ruleExecutionComment: RuleExecutionComment]
   
    static mapping = {
    table "NIDQA_RULE_EXECUTION_DETAILS"
    version false
    id composite:["processExecutionId", "processStepSeq"]
    columns {
            ruleActual column: "RULE_ACTUAL_ID"
                processExecutionId column: "PROCESS_EXECUTION_ID"
                processStepSeq column: "PROCESS_STEP_SEQ"
                recordsProcessed column: "NUM_RECORDS_PROCESSED"
                exceptionMessage column: "EXCEPTION_MESSAGE"
                exceptionStatus column: "EXCEPTION_STATUS"
                exceptionCount column: "NUM_EXCEPTIONS"
                }
    }
}


class RuleExecutionComment implements Serializable{

    String comments
    Date createdDate
    String createdBy    
   
    static belongsTo = [ruleExecution:RuleExecution]
   
    static mapping = {
    table "NIDQA_RULE_EXECUTION_COMMENTS"
    version false
    id generator:"sequence", params:     [sequence:"NIDQA_RULE_EXEC_COMM_SEQ"]

    ruleExecution type: RuleExecution, {
        column name: "PROCESS_EXECUTION_ID"
        column name "PROCESS_STEP_SEQ"
       }
    columns {        
            id column: "RULE_EXEC_COMMENT_ID"
                comments column: "COMMENTS"
                createdDate column: "CREATED_DATE"
                createdBy column: "CREATED_BY"
               
    }
    }
   
 }

Sample Controller code

def ruleExecutionInstance = RuleExecution.findWhere(processExecutionId:100, processStepSeq:1)
if (ruleExecutionInstance )
{
def ruleExecutionCommentList = RuleExecutionComment.findAllByRuleExecution(ruleExecutionInstance, [sort:"createdDate", order:"asc"])
}

I get the error below. The hibernate query is also shown below.As you can see, the hibernate query is generating the column with a default name = rule_execution_process_execution_id  which is not what I specified(I specified PROCESS_EXECUTION_ID ). The default name is longer than 30 characters and causes an Oracle error.

Hibernate:
    select
        this_.RULE_EXEC_COMMENT_ID as RULE1_13_0_,
        this_.COMMENTS as COMMENTS13_0_,
        this_.CREATED_BY as CREATED3_13_0_,
        this_.CREATED_DATE as CREATED4_13_0_,
        this_.rule_execution_process_execution_id as rule5_13_0_,
        this_.rule_execution_process_step_seq as rule6_13_0_
    from
        EASWARS1.NIDQA_RULE_EXECUTION_COMMENTS this_
    where
        (
            this_.rule_execution_process_execution_id=?
            and this_.rule_execution_process_step_seq=?
        )
    order by
        this_.CREATED_DATE asc
2009-11-08 21:38:38,825 [33367135@qtp0-0] ERROR util.JDBCExceptionReporter  - ORA-00972: identifier is too long

2009-11-08 21:38:38,856 [33367135@qtp0-0] ERROR errors.GrailsExceptionResolver  - org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
org.codehaus.groovy.runtime.InvokerInvocationException: org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query