Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 386
idursun Avatar asked Jan 16 '15 10:01

idursun


People also ask

What is difference between JpaRepository and CrudRepository?

CrudRepository provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sort records. JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.

Why is Spring data rest not recommended in real world applications?

It is not recommended in real-world applications as you are exposing your database entities directly as REST Services. While designing RESTful services, the two most important things that we consider are the domain model and the consumers. But, while using Spring Data REST, none of these parameters are considered.

What is @RepositoryRestController?

@RepositoryRestResourceIf you want to write a custom handler for a specific resource taking advantage of Spring Data REST's settings, message converters, exception handling and more, you can use @RepositoryRestController (instead of the standard Spring MVC @Controller or @RestController annotations).

What is @RepositoryRestResource?

This is an interface that allows you to perform various operations with WebsiteUser objects. We also defined a custom query that will provide a list of users based on a given name. The @RepositoryRestResource annotation is optional and is used to customize the REST endpoint.


1 Answers

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.

like image 158
aux Avatar answered Sep 29 '22 12:09

aux