2

How to model composite key in legacy database in grails

I have a legacy db with the three tables modeled by the Grails domain objects

class Foo {

    static hasMany = [bars: Bar]

    static mapping = {
        version false

        columns {
            id column: "FooId"
            fooProp column: "FooProp"           
        }
    }
}

class Bar implements Serializable{

    Baz baz

    static hasMany = [foos: Foo]

    static belongsTo = [Foo]

    static mapping = {
        version false

        columns {
            id composite: ["FooId", "BazId"]
            baz column: "BazId"
            barProp column: "BarProp"
        }
    }
}

class Baz {

    static hasMany = [bars: Bar]

    static mapping = {
        version false

        columns {
            id column: "BazId"
            bazProp column: "BazProp"
        }
    }
}

But when i try to deploy this I get the following exception

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is org.hibernate.HibernateException: Missing column: id in app.Bar

I would rather not have the composite key but I have strict orders not to touch the DB so right now I'm doing my best to model it but composite keys has proven somewhat difficult...

Spade
  • 310
  • 3
  • 21
  • I'm pretty sure you need to define FooId and BazId as attributes in the Bar class. See the grails docs section 6.5.2.5, where you'll see it defines a firstName and lastName as Strings, and then references those for the composite key. You're defining two fields that aren't defined as your composite key. http://grails.org/doc/latest/guide/single.html#identity – Todd Jan 31 '13 at 20:39

1 Answers1

0

It will be usefull this thread

Dynamic finders with Grails many-to-many relationship

I had the same problem. It's work for me.

Community
  • 1
  • 1
raffaeleambrosio
  • 235
  • 2
  • 11