Addition to documentation : YAML for bidirectional relationship

Bug #611428 reported by Bsr203
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
play framework
Fix Committed
Undecided
Unassigned

Bug Description

This is created to tack the below mailing list conversation, as suggested by Guillame.
http://groups.google.com/group/play-framework/browse_thread/thread/373fb035403df630

The relevant part: (you may use the below classes from the attached file)
-------------------
@Dirk.
  I can't thank enough, as I have been struggling with it. thanks for
the detailed example. For further reference, I keep the example below.
2 notes
1. Used @Table(name = "`user`") annotation as User is a reserved word
in PostgreSql
2. I had to reverse the many to many mapping as I considered
Neighborhood as the owning entity
~~~~~~~~~ Code ~~~~~~~~~~~~
Neighborhood.java
-----------------
package models;
import java.util.List;
import javax.persistence.*;
import play.db.jpa.Model;
@Entity
public class Neighborhood extends Model {
    public String name;
    @ManyToOne
    public City city;//bi-directional one-to-many, owning side
    public String tags;
    @ManyToMany //owning side
    public List<User> users;
}

User.java
------------
package models;
import java.util.*;
import javax.persistence.*;
import play.db.jpa.Model;
@Entity
@Table(name = "`user`")
public class User extends Model {
    public String name;
    public String email;
    public String lang;
    public Date created;
    @ManyToMany(mappedBy="users") //inverse side of the relation
    public List<Neighborhood> neighborhoods;
}

City.java
---------
package models;
import java.util.*;
import javax.persistence.*;
import play.db.jpa.Model;
@Entity
public class City extends Model {
    public String name;
    public String state;
    @OneToMany(mappedBy = "city", cascade = CascadeType.ALL) //bi-
directional one-to-many, inverse side
    public List<Neighborhood> neighborhoods;
}

YAML
----
City(capitalfederal):
    name: Capital Federal
    state: Buenos Aires
User(john):
    name: "John Smith"
    email: <email address hidden>
    lang: es
    created: 2007-01-01
    #neighborhoods: [coghlan, villalugano]
User(frank):
    name: "Frank Franken"
    email: <email address hidden>
    lang: en
    created: 2007-08-05
    #neighborhoods: [villalugano]
Neighborhood(villalugano):
    name: Villa Lugano
    city: capitalfederal
    tags: "shopping, cafes"
    users: [john, frank]
Neighborhood(coghlan):
    name: Coghlan
    city: capitalfederal
    tags: "cafes, bars"
    users: [john]

Revision history for this message
Bsr203 (bsr203) wrote :
Revision history for this message
dirk (australiandeveloper) wrote :

I've created a documentation page including the example above:
http://bazaar.launchpad.net/~australiandeveloper/play/1.1-dev/revision/981

Revision history for this message
Peter Hilton (pedro-lunatech) wrote :

I've just committed Dirk's documentation - http://bazaar.launchpad.net/~australiandeveloper/play/1.1-dev/revision/981 - to the Play 1.0 branch.

Changed in play:
status: New → Fix Committed
tags: added: documentation
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Bug attachments

Remote bug watches

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