@Guillaume
It was (again) a typo .. With the example by Dirk, I could find the
right syntax.. I was wrong specifying role when creating person, but
should be other way around (at the owning entity).
@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:
jo...@john.com
lang: es
created: 2007-01-01
#neighborhoods: [coghlan, villalugano]
User(frank):
name: "Frank Franken"
email:
fr...@frank.com
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]
On Jul 29, 10:15 am, dirk <
australiandevelo...@gmail.com> wrote:
> I think you also need an @Entity annotation on the model classes, eg:
> @Entity
> public class Role {
> ...
>
> }
>
> Play uses SnakeYAML. The documentation includes some examples:
http://code.google.com/p/snakeyaml/wiki/Documentation
>
> Note that Play's syntax is slightly different. Instead of using &identifier,
> you put the identifier name in brackets after the model's class name and
> then you can just refer to it directly.
> Here's an example that includes both one-to-many (City-Neighborhood) and
> many-to-many (User-Neighborhood) relationships:
>
> City(capitalfederal):
> name: Capital Federal
> state: Buenos Aires
>
> Neighborhood(villalugano):
> name: Villa Lugano
> city: capitalfederal
> tags: "shopping, cafes"
>
> Neighborhood(coghlan):
> name: Coghlan
> city: capitalfederal
> tags: "cafes, bars"
>
> User(john):
> name: "John Smith"
> email:
j...@john.com
> lang: es
> created: 2007-01-01
> neighborhoods: [coghlan, villalugano]
>
> User(frank):
> name: "Frank Franken"
> email:
fr...@frank.com
> lang: en
> created: 2007-08-05
> neighborhoods: [villalugano]
>
> > Guillaume Bort,
http://guillaume.bort.fr
> > write
guillaume.b...@gmail.com