Circular relations

111 views
Skip to first unread message

Simon Edström

unread,
Oct 6, 2010, 9:29:27 AM10/6/10
to play-framework
Hi!

Im getting circular realtions when rendering my database content as
json. How can I prevent this from happening.

This is an example of how the error occurs:

I have an Entity, User.

@Entity
public class User extends Model{

@ManyToMany
public List<User> friends;

}

So, a User can have several friends and a friend can i turn have the
same User as a friend. When Im trying to get a User object from the
server i simply do;

public static void getUser(Long id){

User u = User.findById(id);

renderJSON(u);
}

And this is what causes an error:

IllegalStateException occured : Circular reference found: User[3]

Thanks!
/Simon


Erwan Loisant

unread,
Oct 6, 2010, 9:33:44 AM10/6/10
to play-fr...@googlegroups.com
You need to write a custom serializer:
http://sites.google.com/site/gson/gson-user-guide#TOC-Writing-a-Serializer

You can then pass your serializer(s) to renderJson().

2010/10/6 Simon Edström <si.ed...@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.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>
>

--
Erwan Loisant

damian

unread,
Oct 6, 2010, 7:51:26 PM10/6/10
to play-framework
You have two options here:

1. Create a Bean that is a simplified representation of your Entity
and render that instead.

2. Extend play.mvc.results.Result as per below and use the @Expose
annotations to tell GSON what to render. Eg:

/** Almost identical copy of Play's RenderJson class but with
excludeFieldsWithoutExposeAnnotation
* so that only fields marked with @Expose are serialized to JSON.
*
* @author dharvey
*
*/
public class RenderJsonWithExclusion extends Result {

String json;

public RenderJsonWithExclusion(Object o) {
GsonBuilder gson = new
GsonBuilder().excludeFieldsWithoutExposeAnnotation();
json = gson.create().toJson(o);
}

public RenderJsonWithExclusion(Object o, JsonSerializer<?>...
adapters) {
GsonBuilder gson = new
GsonBuilder().excludeFieldsWithoutExposeAnnotation();
for(Object adapter : adapters) {
Type t = getMethod(adapter.getClass(),
"serialize").getParameterTypes()[0];;
gson.registerTypeAdapter(t, adapter);
}
json = gson.create().toJson(o);
}

public RenderJsonWithExclusion(String jsonString) {
json = jsonString;
}

public void apply(Request request, Response response) {
try {
setContentTypeIfNotSet(response, "application/json;
charset=utf-8");
response.out.write(json.getBytes("utf-8"));
} catch (Exception e) {
throw new UnexpectedException(e);
}
}

//

static Method getMethod(Class<?> clazz, String name) {
for(Method m : clazz.getDeclaredMethods()) {
if(m.getName().equals(name)) {
return m;
}
}
return null;
Reply all
Reply to author
Forward
0 new messages