Thursday, 27 November 2008

grails: many-to-many Backref exception

I ran into a rather interesting problem recently when attempting to save a many-to-many association between 2 domain objects. I've managed to reproduce the problem in a sanitised test case which I'll present here. The app was developed using grails 1.03.

Consider the domain classes, Project and User defined as follows



and



Note that I've used a List to store the objects on the other side of the many-to-many relationship. Next I create test data in the BootStrap class as follows,




Upon deploying the application, I get the following exception

Caused by: org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: Project._User_projectsBackref; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: Project._User_projectsBackref

This is rather puzzling. The exception suggests an object referencing issue, but it's not clear what this is. Equally baffling is the solution to this problem. The domain classes above use a List to store their many-to-many collaborators. By simply reverting to the default collection type, i.e. a Set, the problem disappears. So for the code above remove line 7 in both classes and the app should deploy without any problems.

I recently came across this JIRA ticket which suggests this is a problem the grails team are aware of.

3 comments:

keep said...

doh... Grails sometimes make me mad.

Unknown said...

Here is a JIRA ticket with the solution (workaround) to the problem:

http://jira.codehaus.org/browse/GRAILS-3783

Imran Salroo said...
This comment has been removed by the author.