Grails

NULL not allowed for column when saving one to one

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: None
  • Fix Version/s: 2.0.1
  • Component/s: Persistence
  • Labels:
  • Environment:
    Grails 2.0-BUILD-SNAPSHOT (18/11)

Description

I may found a bug with one-to-one GORM association, refer to : http://grails.org/doc/2.0.x/guide/GORM.html#gormAssociation -> one-to-one example B

This should work :

class BookController {

    def index() { 
        def book = new Book()
        book.name = "A new book"
        book.author = new Author(name:"Grails")
        book.save()
        render(text:book.id)
    }
}
class Book {

    String name
    Author author
    
    static constraints = {
    }
}
class Author {
    String name
    static belongsTo = [book:Book]
    
}
Error 500 : 
URI          /g2/book/index
Class        org.h2.jdbc.JdbcSQLException
Message   NULL not allowed for column "AUTHOR_ID"; SQL statement: insert into book (id, version, author_id, name) values (null, ?, ?, ?) [90006-147]

Issue Links

Activity

Hide
Graeme Rocher added a comment -

This works if you set both sides of the association and in fact that is what is happening in the example in the documentation.

Show
Graeme Rocher added a comment - This works if you set both sides of the association and in fact that is what is happening in the example in the documentation.
Hide
Mengu Kagan added a comment - - edited

Graeme, would you mind explaining how the associations are different from the example in the doc? Book is Face and Author is Nose. I fail to see the difference.

Show
Mengu Kagan added a comment - - edited Graeme, would you mind explaining how the associations are different from the example in the doc? Book is Face and Author is Nose. I fail to see the difference.
Hide
Mihael Koep added a comment -

In Grails 1.3.7 the above works. We have similar problems upgrading our application from 1.3.7 to 2.0.0. A workaround is setting the other side of the association like Graeme said, using the above example adding

book.author.book = book

to the controller code. I do not really like the solution, but it works for us. Nevertheless I would consider this a bug, since it worked in the versions before. The docs describe the way without setting the other side of the association, too.

Show
Mihael Koep added a comment - In Grails 1.3.7 the above works. We have similar problems upgrading our application from 1.3.7 to 2.0.0. A workaround is setting the other side of the association like Graeme said, using the above example adding
book.author.book = book
to the controller code. I do not really like the solution, but it works for us. Nevertheless I would consider this a bug, since it worked in the versions before. The docs describe the way without setting the other side of the association, too.
Hide
Ted Naleid added a comment -

So this isn't actually fixed then? This bug is affecting cascading saves in build-test-data, and is a regression from the behavior that was around in 1.3.X (and previously).

Is this really a "wontfix" instead of "fixed" or is this still actually considered broken behavior?

Show
Ted Naleid added a comment - So this isn't actually fixed then? This bug is affecting cascading saves in build-test-data, and is a regression from the behavior that was around in 1.3.X (and previously). Is this really a "wontfix" instead of "fixed" or is this still actually considered broken behavior?
Hide
Graeme Rocher added a comment -

It is fixed in terms of when you use the save() method to create the association is auto associates the other side. The key is to make sure both sides of the association are linked before saving. Hibernate was always very explicit about this being a requirement. It is a regression it is true, however the bug is in Hibernate not Grails. The relevant commit that fixes this issue is

https://github.com/grails/grails-core/commit/2deba492a1d68951914e03b4eed3529119b0f899

Show
Graeme Rocher added a comment - It is fixed in terms of when you use the save() method to create the association is auto associates the other side. The key is to make sure both sides of the association are linked before saving. Hibernate was always very explicit about this being a requirement. It is a regression it is true, however the bug is in Hibernate not Grails. The relevant commit that fixes this issue is https://github.com/grails/grails-core/commit/2deba492a1d68951914e03b4eed3529119b0f899
Hide
Ted Naleid added a comment -

Thanks Graeme, that makes sense from Grails point of view, I agree that this is a hibernate issue. It makes things a little harder, but it's still doable with some code changes.

Show
Ted Naleid added a comment - Thanks Graeme, that makes sense from Grails point of view, I agree that this is a hibernate issue. It makes things a little harder, but it's still doable with some code changes.

People

Vote (2)
Watch (4)

Dates

  • Created:
    Updated:
    Resolved:
    Last Reviewed: