Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring-data-rest, can you provide full details of entity instead of (or with) link [duplicate]

Is there a way to return the full details of a joined entity instead of a link? In the example below I want to also return the details of the product, if I have list of 100 purchases, it would avoid having to make 100 calls to get the product details.

The repositories for Product, User and Purchase entities are all created using spring-data-jpa

{
  "_embedded" : {
    "purchase" : [ {
      "_links" : {
        "product" : {
          "href" : "http://localhost:8080/webapp/purchase/1/product"
        },
        "user" : {
          "href" : "http://localhost:8080/webapp/purchase/1/user"
        }
      },
      "purchasedOn" : "2014-02-23",
      "amount" : 1
    } ]
  }
}

Entities and Repositories;

@Entity
public class User {

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

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, targetEntity = Purchase.class, orphanRemoval = true)
    @JoinColumn(name = "user_id", updatable = false)
    private List<Purchase> purchases = new ArrayList<>();

}

@Entity
public class Product {

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

    private String name;

}

@Entity
public class Purchase implements Serializable {

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

    @ManyToOne
    @JoinColumn(name = "user_id", referencedColumnName = "id")
    private User user;

    @ManyToOne(fetch = FetchType.EAGER, targetEntity = Product.class)
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    private Product product;

    @Column(name = "purchase_date")
    private Date purchaseDate;

    private Integer amount;

}

@Repository
public interface PurchaseRepository extends JpaRepository<Purchase, Long> {}
like image 795
wenic Avatar asked Feb 23 '14 22:02

wenic


People also ask

What are the two details to be provided to spring through the JpaRepository interface for auto generation of common CRUD operations choose two?

If we create our repository by extending the CrudRepository interface, we have to provide two type parameters: The type of the entity that is managed by our repository. The type of the entity's id field.

What is difference between CrudRepository and JpaRepository interfaces in spring data JPA?

Each of these defines its own functionality: 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.

Is used for exposing spring data repositories over rest using spring data rest?

Spring Data REST can be used to expose HATEOAS RESTful resources around Spring Data repositories. Without writing a lot of code, we can expose RESTful API around Spring Data Repositories.

How does spring data rest work?

Spring Data REST builds on top of Spring Data repositories, analyzes your application's domain model and exposes hypermedia-driven HTTP resources for aggregates contained in the model.


1 Answers

It would seems there is already a New Feature request for this functionality;

https://jira.springsource.org/browse/DATAREST-221

https://jira.springsource.org/browse/DATAREST-243

I'll leave the question open until the feature has been implemented.

like image 77
wenic Avatar answered Sep 30 '22 20:09

wenic