awfulHack
awfulHack

Reputation: 875

Grails relationships with static mapped tables

For the life of me I cannot seem to get relationships to work on mapped tables with Grails. I have two domains I am trying to join, Resources and Cassettes. A Resource can have many Cassettes.

If i run the code below using scaffolding I get an error "Unknown column 'this_.cassette_id' in 'field list'". If i try to define the cassette_id in the mapping I get a fatal error upon compilation.

Can any wise Grails wizard set me on the correct path, I am new to this and have tried practically tried every method I can find to make this valid.

//resource definition

package edu.place.project

class Resource {
    String title
    String number
    String type
    Cassette cassette

    static hasMany  = [cassette : Cassette ]

    static mappedBy = [cassette : "hvt"]

    static mapping = {
            table "Resources"
    version false
            columns {
                    id column : "resourceIdentifier2"
                    title column: "title"
                    number column: "extentNumber"
                    type column: "extentType"
            }  
    }

    static constraints = {
    }
}

//Cassette definition

package edu.place.project

class Cassette {
    String id
    String type
    String numCode
    String hvt

    static belongsTo = Resource

    static mapping = {
            table "ArchDescriptionInstances"
            version false
            columns {
                    id column : "barcode", type : String
                    type column : "userDefinedString2"
                    numCode column : "container1AlphaNumIndicator"
                    hvt column : "userDefinedString1"
            }
    }

     static constraints = {
            barcode(unique : true)
     }
}

Upvotes: 1

Views: 3885

Answers (1)

Rob Hruska
Rob Hruska

Reputation: 120286

It's probably because you have:

class Resource {
    Cassette cassette
    static hasMany = [cassette: Cassette]
}

These two definitions conflict with each other. The hasMany implicitly tries to create a Set (collection) called cassette on your domain, but you've explicitly defined it as a Cassette.

Try removing the Cassette cassette and see what happens. I don't think you need it for the model you've described.

As an aside, you might also consider renaming the field to cassettes since it's a collection, rather than a singular object.

After these, your domains (the relationship parts, anyway) might look like this:

class Resource {
    static hasMany = [cassettes: Cassette]
}

class Cassette {
    Resource resource
    static belongsTo = Resource
}

Upvotes: 2

Related Questions