views:

28

answers:

1

I have a legacy DB with rather simple structure. I have "rolls" that contain "rollTotals". Roll has a primary key of "rollID" and RollTotals have a composite key on "rollID" and "category".

So in Grails, I have:

class Roll {
    Integer id
    ...
    static hasMany = [ rollTotals: RollTotal ]
    static mapping = {
        table('rolls')
        id(column:'rollID')
        version false
        ...
    }
}

and

class RollTotal implements Serializable {
    Integer rollId
    Integer category
    ...
    static belongsTo = [ Roll ]

    static mapping = {
        table('rolltotals')
        id composite:['rollId', 'category']
        version false
        rollId(column:'rollID')
        category(column:'category')
     ...
}

These mappings work fine individually but when I put the hasMany, it says:

Missing table: rolls_rolltotals

So I figured it needed to know which column to use as foreign key, so I added to Roll:

static mappedBy = [rollTotals: "rollId" ]

It says:

org.codehaus.groovy.grails.exceptions.GrailsDomainException: Non-existent mapping property [rollId] specified for property [rollTotals] in class [class Roll]

I tried with rollID (as the column name) but get the same result.

Any idea as to how I can link these tables ?

+1  A: 

Try changing belongsTo for RollTotal to:

static belongsTo = [roll: Roll]

and add to mapping for RollTotal

roll(column:'rollId')
Micor
This worked and I understand why. Thanks.
Eric Darchis

related questions