Bidirectional relationship in yaml

511 views
Skip to first unread message

bsr

unread,
Jul 27, 2010, 11:05:43 PM7/27/10
to play-framework
Hello,
Anyone successful in defining a bidirectional relationship in yaml.
I am not sure the correct syntax for it. Please see the domain model,
and the yaml.

public class Person {
String fullname;
@OneToMany(mappedBy="person",cascade=CascadeType.ALL)
public List<Role> roles;
}

public class Role {
public RoleType roleType;
public String description;
public Person person;
}

public class RoleType {
public String roleName;
public String description;
}


YAML1:

RoleType(rt1):
roleName: ADMINISTRATOR
description: administrator

Role(prole1):
roleType: rt1
description: description

Person(person1):
fullname: test user
roles:
- role1

YAML2:

RoleType(rt1):
roleName: ADMINISTRATOR
description: administrator

Person(person1):
fullname: test user
roles:
- roleType: rt1
description: description
person: person1

YAML3:

RoleType(rt1):
roleName: ADMINISTRATOR
description: administrator

Person: &1
fullname: test user
roles:
- roleType: rt1
description: description
person: *1


None of it worked, giving transient error or "No previous reference
found for object of type ..." . Any pointer, highly appreciated.
thanks.

dirk

unread,
Jul 28, 2010, 10:49:47 AM7/28/10
to play-fr...@googlegroups.com
Try adding an annotation for the other side of the relationship as well:


public class Role {
     public RoleType roleType;
     public String description;

     // You're missing this annotation:
     @ManyToOne
     public Person person;
}



--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.


bsr

unread,
Jul 28, 2010, 2:48:53 PM7/28/10
to play-framework
I am sorry.. I missed when I wrote the mail, it was already present..
Please let me know, in your experience whether u could define
bidirectional rel in yaml.. if you could show the syntax of yaml,
itwould help me.. thanks again..
> > play-framewor...@googlegroups.com<play-framework%2Bunsubscribe@go oglegroups.com>
> > .

Guillaume Bort

unread,
Jul 28, 2010, 2:52:00 PM7/28/10
to play-fr...@googlegroups.com
Role(prole1):

It should be

Role(role1):

right?

> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.


> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>
>

--
Guillaume Bort, http://guillaume.bort.fr

For anything work-related, use g...@zenexity.fr; for everything else,
write guillau...@gmail.com

dirk

unread,
Jul 29, 2010, 10:15:45 AM7/29/10
to play-fr...@googlegroups.com
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:   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]

bsr

unread,
Jul 29, 2010, 1:06:11 PM7/29/10
to play-framework
@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
>
> > For anything work-related, use g...@zenexity.fr; for everything else,
> > write guillaume.b...@gmail.com

dirk

unread,
Jul 29, 2010, 2:35:19 PM7/29/10
to play-fr...@googlegroups.com
I'm glad you got it working. Thanks for posting your sample code :)
I didn't realize you would need to include a "mappedBy" parameter for the @ManyToMany annotation. It seems like it should be able to figure that out automatically.

Guillaume, maybe this sample could be included in a Play/YAML documentation page?



To unsubscribe from this group, send email to play-framewor...@googlegroups.com.

Guillaume Bort

unread,
Jul 29, 2010, 2:45:20 PM7/29/10
to play-fr...@googlegroups.com
Yes sure can someone add it to the bug tracker with the attached sample?

bsr

unread,
Jul 29, 2010, 2:54:26 PM7/29/10
to play-framework
Dirk,
In the case of oneToMany, mappedBy is required to make the
relationship bi-directional. That is you can access the related entity
from both side. If you do not specify, the relation would be uni-
directional and a join table is automatically created. This is because
you prevent JPA from creating foreign key in the owning table.
In ManyToMany, mappedBy is not optional (http://download-
llnw.oracle.com/javaee/5/api/javax/persistence/ManyToMany.html). It is
required to specify the owning side, through which you persist the
relation.
thanks again.
Babu.
> > > > Guillaume Bort,http://guillaume.bort.fr
>
> > > > For anything work-related, use g...@zenexity.fr; for everything else,
> > > > write guillaume.b...@gmail.com
>
> > > > --
> > > > You received this message because you are subscribed to the Google
> > Groups
> > > > "play-framework" group.
> > > > To post to this group, send email to play-fr...@googlegroups.com.
> > > > To unsubscribe from this group, send email to
> > > > play-framewor...@googlegroups.com<play-framework%2Bunsubscribe@go oglegroups.com>

bsr

unread,
Jul 29, 2010, 3:08:30 PM7/29/10
to play-framework
Done... https://bugs.launchpad.net/play/+bug/611428

the program also attached.

On Jul 29, 2:45 pm, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> Yes sure can someone add it to the bug tracker with the attached sample?
>
> Le 29 juil. 2010 à 20:35, dirk <australiandevelo...@gmail.com> a  
> >    email:   j...@john.com
> >    lang:    es
> >    created: 2007-01-01
> > %2Bunsubscribe@go oglegroups.com>
> > > > .
> > > > > For more options, visit this group at
> > > >http://groups.google.com/group/play-framework?hl=en.
>
> > > > --
> > > > Guillaume Bort,http://guillaume.bort.fr
>
> > > > For anything work-related, use g...@zenexity.fr; for everything  
> > else,
> > > > write guillaume.b...@gmail.com
>
> > > > --
> > > > You received this message because you are subscribed to the  
> > Google Groups
> > > > "play-framework" group.
> > > > To post to this group, send email to play-fr...@googlegroups.com
> > .
> > > > To unsubscribe from this group, send email to
> > > > play-framewor...@googlegroups.com<play-framework
> > %2Bunsubscribe@go oglegroups.com>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/play-framework?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google  
> > Groups "play-framework" group.
> > To post to this group, send email to play-fr...@googlegroups.com.
> > To unsubscribe from this group, send email to play-framewor...@googlegroups.com
> > .
> > For more options, visit this group athttp://groups.google.com/group/play-framework?hl=en
> > .
>
> > --
> > You received this message because you are subscribed to the Google  
> > Groups "play-framework" group.
> > To post to this group, send email to play-fr...@googlegroups.com.
> > To unsubscribe from this group, send email to play-framewor...@googlegroups.com

dirk

unread,
Aug 2, 2010, 7:50:37 PM8/2/10
to play-fr...@googlegroups.com
I've created a manual page. I included a link to my bzr branch in the bug report
https://bugs.launchpad.net/play/+bug/611428
http://bazaar.launchpad.net/~australiandeveloper/play/1.1-dev/revision/981

Please let me know if you have any suggestion and I'll modify it. I haven't worked with multiple yaml files before so perhaps there could be some information about that in there as well.

dirk

unread,
Aug 2, 2010, 7:52:43 PM8/2/10
to play-fr...@googlegroups.com
I also posted a link to the fix for a problem that play has when parsing dates in a YAML file:
https://bugs.launchpad.net/play/+bug/611428
http://bazaar.launchpad.net/~australiandeveloper/play/1.1-dev/revision/979
Reply all
Reply to author
Forward
0 new messages