0
votes

I am having a problem setting up a List as a hasMany relationship to a belongsTo withing an extended class.

With a new Grails 2.4.4 app I have these 3 Domains:

An Author class having many books.

class Author {
    List books
    static hasMany = [books: Book]
}

A Book class which extends Content

class Book extends Content {
    String foo
}

And Content, which has a number of common properties.

class Content {
    String title
    static belongsTo = [author: Author]
    static constraints = {
        sort: 'title'
    }
}

When running this application I get the following exception:

Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: test.relationships.Book column: author_id (should be mapped with insert="false" update="false")

I only get this exception when I make books a List.

1
Do you need to define books as a list? I think it's treated as a list by default from the hasMany notation such that you don't need to define it as a separate variable. Hence your "repeated column." Just a guess.Marc
Actually by default, books would be a set. I think specifying the type like this is the correct way to ensure that the books collection is mapped as a list.rcgeorge23

1 Answers

0
votes

I think the problem is probably something to do with the fact you're saying that Author has many Books, but the superclass of Book, Content, belongs to an Authur.

What happens if you change your Content and Book classes to this:

class Content {
    String title
    static constraints = {
        sort: 'title'
    }
}

class Book extends Content {
    static belongsTo = [author: Author]
    String foo
}

or, alternatively, change your Author class to this:

class Author {
    List books
    static hasMany = [contents: Content]
}