简体   繁体   中英

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM