idursun
idursun

Reputation: 6335

Links to Embedded entities in Spring Data Rest

I have the following entities defined in my project:

Country

@Entity
@Data
public class Country {

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

    @Column(nullable = false)
    String name;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    List<City> cities = new ArrayList<City>();

}

City

@Entity
@Data
public class City {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;
    @Column(nullable = false)
    String name;
    @ManyToOne
    Country country;
}

Person

@Entity
@Data
public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long id;
    @Column
    String name;
    @Embedded
    Address address = new Address();
}

Address

@Data
public class Address {
    @Column
    String line;
    @ManyToOne
    Country country;
    @ManyToOne
    City city;
}

I have also repositories defined for Person, Country and City.

When I make a GET request to /persons/1 I get the following result:

{
   "name":null,
   "address":{
      "line":"Address1"
   },
   "_links":{
      "self":{
         "href":"http://localhost:8080/persons/1"
      },
      "city":{
         "href":"http://localhost:8080/persons/1/city"
      },
      "country":{
         "href":"http://localhost:8080/persons/1/country"
      }
   }
}

I suspect that since address is an embedded object, the generated links to country and city are wrong. They don't return anything although city and country values are present. What should the correct links be?

Are embedded objects not supported by Spring Data Rest?

Upvotes: 7

Views: 1972

Answers (1)

Andrey Usov
Andrey Usov

Reputation: 1587

Possible solutions:

  • move associations to the parent entity
  • promote the embeddable into a separate entity resource
  • add ResourceProcessor to remove those links
  • add a custom controller to handle those links

UPDATE: This seems to be already fixed in Spring-DATA-REST v2.1. See DATAREST-262.

Upvotes: 2

Related Questions