Grails JIRA

  • Log In Access more options
    • Online Help
    • GreenHopper Help
    • Agile Answers
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What’s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
  • Agile
Grails
  • Grails
  • GRAILS-3396 Top level task: GORM Improvements
  • GRAILS-1217

Persistence of embedded items in subclasses

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: Sub-task Sub-task
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 0.5
  • Fix Version/s: 1.1-beta3
  • Component/s: Persistence
  • Labels:
    None
  • Environment:
    Grails 0.5, Derby 10.2

Description

I have the following domain classes:

class SiteUser {
String login
String password
String nick
String status = "active"
Date signUpDate = new Date()
}

class Customer extends SiteUser {
String firstName
String lastName
String telephone
Address address

static constraints =

{ address(nullable:true) }

static embedded = ['address']
}

class Address {
String street
String zipcode
String country
}

When I execute the following on the console I get two strange things:

def newuser = new SiteUser(login:'base@site.com', password:'aasa132',nick:'tester')
if (newuser.save())
{
println 'Success'
def all = SiteUser.list()
print all
}
else {
newuser.errors?.allErrors.each

{ println it.DefaultMessage }

}

First, an exception is raised and the user is not saved, but even so, newuser.save() is returning TRUE.

Second, the exception is "Caused by: java.sql.SQLException: Column 'ADDRESS_COUNTRY' cannot accept a NULL value.", when Address is not a field of SiteUser but one of its subclasses.

Reviewing the database, both SiteUser and its subclasses are stored on the same table, but the address fields (ADDRESS_COUNTRY, ADDRESS_ZIPCODE, ADDRESS_STREET) are declared as NOT NULL.

Creation of SiteUser objects is failing because of one of its subclasses, even though the field on that subclass is nullable. It seems that this is because the fields on the Address class are not nullable, and they're being used for the declaration regardless of the fact that the address field is nullable itself.

PS: if I declare the following subclass:

class AdminUser extends SiteUser {
String adminValue

static constraints =

{ adminValue(blank:false) }

}

The table field adminValue is correctly declared as nullable, probably to avoid problems like the one above.

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • Work Log
  • History
  • Activity
  • Git Commits
Hide
Permalink
Ricardo J. Méndez added a comment - 01/Jun/07 9:07 PM

Here's a simpler test case. While subclassing exacerbates this issue, it is a problem with single classes as well:

class Address

{ String street String zipcode String country }

class TestClass {
String firstName
String lastName
String telephone
Address address

static constraints =

{ address(nullable:true) }

static embedded = ['address']
}

The ADDRESS_* fields will be declared as NOT NULL in the database, probably since the definition comes from the Address class, even though the address member as a whole is indeed nullable.

Show
Ricardo J. Méndez added a comment - 01/Jun/07 9:07 PM Here's a simpler test case. While subclassing exacerbates this issue, it is a problem with single classes as well: class Address { String street String zipcode String country } class TestClass { String firstName String lastName String telephone Address address static constraints = { address(nullable:true) } static embedded = ['address'] } The ADDRESS_* fields will be declared as NOT NULL in the database, probably since the definition comes from the Address class, even though the address member as a whole is indeed nullable.
Hide
Permalink
Ricardo J. Méndez added a comment - 31/Dec/07 11:00 AM

While the original environment in which I tested was Derby 10.2, this seems to be a general issue - it appears with MySQL 5.0.27 as well.

Show
Ricardo J. Méndez added a comment - 31/Dec/07 11:00 AM While the original environment in which I tested was Derby 10.2, this seems to be a general issue - it appears with MySQL 5.0.27 as well.

People

  • Assignee:
    Graeme Rocher
    Reporter:
    Ricardo J. Méndez
Vote (3)
Watch (4)

Dates

  • Created:
    31/May/07 8:36 AM
    Updated:
    18/May/11 3:20 PM
    Resolved:
    22/Jan/09 3:40 AM

Agile

  • View on Board
  • Atlassian JIRA (v5.2.1#813-sha1:277a546)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Grails project. Try JIRA - bug tracking software for your team.