0

I have defined two JPARepository with spring: "Person" and "Address". I also specified a relation between Person and Address.

I can fetch all persons with: http://localhost:8080/person/

and all address with: http://localhost:8080/address/

Also I can get the address of a single person with http://localhost:8080/person/1/address

Now I'd like to get the address to every person as a nested address when I get request: http://localhost:8080/person/

How can I include the relations in the response?

My classes as requested by @nicolasl

@Entity
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String firstName;
    private String lastName;

    @OneToOne
    @JoinColumn(name = "address_id")
    private Address address;


    //Getter/Setter here


}

@Entity
public class Address {

    @Id
    @GeneratedValue
    private long id;

    @Column(nullable = false)
    private String location;

    @OneToOne(mappedBy = "address")
    private Person person;

    //Getter/Setter here
}
Franken
  • 419
  • 1
  • 4
  • 14

1 Answers1

0

Well, I guess you just can't, not using the automated rest engine provided by spring. The rest API will return links to retrieve the related entities in the "_links" section of your JSON response (you can read more about that in http://stateless.co/hal_specification.html). You'd have to call those links to retrieve your Addresses.

Another way to solve this, is implementing your own repository and assembling your own JSON response.

SAMUEL
  • 8,098
  • 3
  • 42
  • 42
nicolasl
  • 421
  • 3
  • 16