Add mappedBy attribute to bidirectional relationships

Bug #722964 reported by David Coles
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
MUGLE
Fix Released
High
Prageeth Silva

Bug Description

Relationships that should be accessible from both sides should include a mappedBy attribute (see: http://code.google.com/appengine/docs/java/datastore/jdo/relationships.html ).

In particular, most of our one-to-many relations will need to be bidirectional to allow traversal from either object.

David Coles (dcoles)
Changed in mugle:
status: New → Triaged
importance: Undecided → High
Revision history for this message
Matt Giuca (mgiuca) wrote :

Prageeth raises the point that we put the "mappedBy" attribute on the singleton variable, not the collection, whereas in the Google examples, it is on the collection.

For example, in http://bazaar.launchpad.net/~mugle-dev/mugle/trunk/revision/28 we have got:

Achievement.java:
@Persistent(mappedBy="achievements")
private Game game;

Game.java:
@Persistent
private Set<Achievement> achievements;

It probably should be the other way around. I'm not sure if it makes a difference. Also I'm not sure why you can't put the mappedBy on a many-to-many relation. Perhaps mappedBy only works if the thing you are mapping (the value of mappedBy="") is a singleton.

Revision history for this message
Prageeth Silva (prageethsilva) wrote :

I've set all the "mappedBy" attributes on the singleton. For example:

Achievement.java:
@Persistent
private Game game;

Game.java:
@Persistent(mappedBy="game")
private Set<Achievement> achievements;

In my opinion this makes more sense; for every entry in the set "achievements", make sure it's field "game" is linked to "this" instance of the Game class.

Changed in mugle:
status: Triaged → Fix Committed
Revision history for this message
Matt Giuca (mgiuca) wrote :

I think you mean you've set them all on the collection.

I don't think mappedBy means "for every entry in the set "achievements", make sure it's field "game" is linked to "this" instance of the Game class." Rather the other way around:

For every Achievement, if the 'game' is set to a particular Game object, make sure its 'achievements' set contains this Achievement." Am I correct about this?

David Coles (dcoles)
Changed in mugle:
milestone: none → 0.1.1
Matt Giuca (mgiuca)
Changed in mugle:
assignee: nobody → Prageeth Silva (prageethsilva)
Matt Giuca (mgiuca)
tags: added: datastore
removed: database
Matt Giuca (mgiuca)
Changed in mugle:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.