Andy N
Andy N

Reputation: 412

Spring Roo OneToMany relationship not showing in JSON

I am trying to get a OneToMany relationship using Hibernate, roo, and JSON.

I was able to see the children of the parent via the jspx page by adding

 <field:display field="children" id="s_com_example_domain_Parent_children" object="${parent}" z="Etv5+hIcaVaq9A6jzjMOK0xoJMI="/>

to /src/main/webapp/WEB-INF/parents/show.jspx

However, I cannot see them when I perform a GET (type=application/json) Here is my code:

Parent.java:

 @RooJavaBean
 @RooToString
 @RooJpaActiveRecord
 @RooJson
 public class Parent {

private String name;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent")
private Set<Child> children = new HashSet<Child>();
 }

Child.java

 @RooJavaBean
 @RooToString
 @RooJpaActiveRecord
 @RooJson
 public class Child {

private String name;

@ManyToOne
private Parent parent;
 }

Roo shell:

jpa setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
entity jpa --class ~.domain.Parent 
entity jpa --class ~.domain.Child
field set --fieldName children --type ~.domain.Child --class ~.domain.Parent --mappedBy     parent --cardinality ONE_TO_MANY --notNull false 
field reference --fieldName parent --type ~.domain.Parent --cardinality MANY_TO_ONE --class ~.domain.Child --notNull false
web mvc setup
web mvc all --package ~.web
web mvc json all 

I have also added

     <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.12</version>
    </dependency>

To my maven project.

If I go through the web (jspx) interface, I can add a parent p, and child c. I can see that the child has a parent p, and the parent has children c, but here is the output of my GET request for the JSON

 Request Url: http://localhost:8080/HibernateTest/children/1
 Request Method: GET
 {
     "id": 1,
     "name": "c",
     "parent": {
         "id": 1,
         "name": "p",
         "version": 0
     },
     "version": 0
 }

 Request Url: http://localhost:8080/HibernateTest/parents/1
 Request Method: GET
 {
     "id": 1,
     "name": "p",
     "version": 0
 }

How can I get my children to show up under the parent in a JSON GET request?

Upvotes: 2

Views: 2209

Answers (2)

rwyland
rwyland

Reputation: 1647

Use deepSerialize on the RooJson annotation to go deeper in Flexjson without having to mess with the aspect:

@RooJavaBean
@RooJpaActiveRecord
@RooJson(deepSerialize = true)
public class Office {

    @NotNull
    private String name;

    @OneToMany(targetEntity = Person.class, cascade = CascadeType.ALL, mappedBy = "office")
    private Set<Person> people = new HashSet<Person>();

    @NotNull
    @ManyToOne(targetEntity = Company.class)
    private Company company;
}

Upvotes: 0

CodeChimp
CodeChimp

Reputation: 8154

In order to return cascading relations, you to overwrite your Entity's toJson() method (push it in from your .aj), doing something like this in your parent:

public String toJson() {
  return new JSONSerializer().include("*.children").serialize(this);
}

More info can be found here. I think there is also an annotation you can use, like @Json or something like that, in which you can instruct which members to include/exclude on serialization, but I couldn't remember it nor find it via Google.

Upvotes: 2

Related Questions