GORM Optional Many-To-One Relationship

201 views
Skip to first unread message

Mike Calvo

unread,
Nov 14, 2007, 11:25:30 PM11/14/07
to Groovy Users of Minnesota
I'm having trouble defining a many to one relationship as being
optional (nullable). Here's the specific situation:

class EventRole {
static belongsTo = [Event, Position]

Event event
Position position
Volunteer volunteer
}

The volunteer is optional. I've tried adding:

static optionals = [ 'volunteer' ]

And I've also tried adding:

static constraints = {
volunteer(nullable: true)
}

Neither work. Can anyone point me in the right direction?

Scott Vlaminck

unread,
Nov 15, 2007, 12:28:13 AM11/15/07
to groo...@googlegroups.com
When you say that neither of those ways work, what happens? Is
validation failing on save() when the volunteer is null, or is it a
different error? What version of Grails are you using?

Have you tried adding Volunteer to the belongsTo?

Is the other side of the relationship mapped using hasMany? There are
times when I don't want to map both sides and I've had some odd things
happen (of course, I don't have an example off hand), so I've gotten
into the habit of mapping both sides of relationships.

Scott


--
-------------------------------------------------
Scott Vlaminck // sc...@refactr.com
Refactr LLC // http://refactr.com
mobile // 612-386-9382
-------------------------------------------------

Mike Calvo

unread,
Nov 15, 2007, 12:56:29 AM11/15/07
to Groovy Users of Minnesota
I'm using 1.0-RC1.

When I say it doesn't work, it requires me to select a volunteer on
the EventRole detail page saying that it's required. When I add
Volunteer to belong to and make the relationship mapped by on a
bidirectional, the nullable is honored and I can leave it unspecified
on the detail page.

It sounds like in GORM, the best practice is to use bidirectional
relationships?


On Nov 14, 11:28 pm, "Scott Vlaminck" <sc...@refactr.com> wrote:
> When you say that neither of those ways work, what happens? Is
> validation failing on save() when the volunteer is null, or is it a
> different error? What version of Grails are you using?
>
> Have you tried adding Volunteer to the belongsTo?
>
> Is the other side of the relationship mapped using hasMany? There are
> times when I don't want to map both sides and I've had some odd things
> happen (of course, I don't have an example off hand), so I've gotten
> into the habit of mapping both sides of relationships.
>
> Scott
>
> On 11/14/07, Mike Calvo <mjca...@gmail.com> wrote:
>
>
>
>
>
> > I'm having trouble defining a many to one relationship as being
> > optional (nullable). Here's the specific situation:
>
> > class EventRole {
> > static belongsTo = [Event, Position]
>
> > Event event
> > Position position
> > Volunteer volunteer
> > }
>
> > The volunteer is optional. I've tried adding:
>
> > static optionals = [ 'volunteer' ]
>
> > And I've also tried adding:
>
> > static constraints = {
> > volunteer(nullable: true)
> > }
>
> > Neither work. Can anyone point me in the right direction?
>
> --
> -------------------------------------------------
> Scott Vlaminck // sc...@refactr.com
> Refactr LLC //http://refactr.com
> mobile // 612-386-9382
> -------------------------------------------------

Scott Vlaminck

unread,
Nov 15, 2007, 8:51:59 AM11/15/07
to groo...@googlegroups.com
>
> It sounds like in GORM, the best practice is to use bidirectional
> relationships?
>

It's sometimes annoying, but that's been my experience so far. My
guess is that GORM can get a little confused if things aren't mapped
from both sides because it has to try to figure out what you want
without much information.

Your example with working and not-working solutions would be a good
one to post to the Grails User list as well, to see other people's
reaction.

Scott

--
-------------------------------------------------
Scott Vlaminck // sc...@refactr.com

Refactr LLC // http://refactr.com
mobile // 612-386-9382
-------------------------------------------------

Mike Hugo

unread,
Nov 15, 2007, 9:02:53 AM11/15/07
to groo...@googlegroups.com
Is it a GORM problem or is it something in the controller? E.g. can
you do this?

class EventRoleTests extends GroovyTestCase {

void testSaveEventRole() {
def e = new Event(name:'eventName').save()
def p = new PositionObj(name:'positionName').save()

def er = new EventRole(event:e, position:p)
er.save(flush:true)
assert er.id
}
}

I tried a simple example with volunteer(nullable:true) and the test
worked - but the generated scaffolding didn't. Maybe there's a bug
with def eventRole = new EventRole(params) in the controller?

class EventRole {
static belongsTo = [Event, PositionObj]

Event event
PositionObj position
Volunteer volunteer



static constraints = {
volunteer(nullable:true)
}
}

Simple example attached:

bug-report-15112007.zip

kabuski

unread,
Nov 29, 2007, 9:54:30 PM11/29/07
to Groovy Users of Minnesota
This should work for you.
Volunteer volunteer = new Volunteer()
Reply all
Reply to author
Forward
0 new messages