views:

56

answers:

1

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)
     }
}
+2  A: 

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
}
Rob Hruska
ok, that worked, the resource domain loads now, but when I select a resource I am confronted with a new error: "Exception Message: Table 'fortunoff.Resources_ArchDescriptionInstances' doesn't exist" The ArchDescriptionInstances table is the table being mapped by the cassette table. If i wasn't perplexed before I am now.
awfulHack
Since you've set up a `hasMany` between the two, Grails needs a mapping table to store the relationships between the objects. I haven't tried to do anything like this before, so I'm probably not much help here. You may have to explicitly declare your join table in your mapping. However, the database model you're trying to work with seems a bit nonstandard, so I'm not sure how well it will work. http://www.grails.org/doc/latest/ref/Database%20Mapping/joinTable.html
Rob Hruska

related questions