3

I have one entity A which has some properties, and reference to another entity B

A{
....
   @ManyToOne(fetch=FetchType.LAZY)
   B b;
...
}

same for B(@onetomany for A in B). Its a bidirectional relation. While calling the web service for entity A it gives link to the B entity instead of whole object in response json. I want the whole object in web service response JSON instead of link to that related entity. How can i do that? I am using springboot + spring data jpa + hibernate as jpa provider + spring 4

4
  • what is "link to the B"? Commented Mar 18, 2015 at 9:50
  • @dit : spring data rest provides url(link) for the nested objects . in this case it will provide url for B instead of object in json. Commented Mar 18, 2015 at 9:57
  • is it option to change the fetch type to FetchType.EAGER ? Commented Mar 18, 2015 at 10:11
  • @dit : i changed to eager, but still B is not included in the A's web service response. Commented Mar 18, 2015 at 10:28

2 Answers 2

7

In order to have nested related objects in the output JSON you have to use projections.

public class User {
   private String name;
   private Address address;
}

public class Address {
   private String street;
}

public interface UsersRepository extends JpaRepository<User, Long> {
   // Custom methods
}

will give you

{
   "name": "John",
   "_links": {
      "self": "http://localhost:8080/users/1",
      "address": "http://localhost:8080/addresses/1"
   }
}

BUT with projection:

@Projection(name = "inlineAddress", types = { User.class })
public interface UserInlineAddressProjection {
   String getName();
   Address getAddress();
}

@RepositoryRestResource(excerptProjection = UserInlineAddressProjection.class)
public interface UsersRepository extends JpaRepository<User, Long> {
   // Custom methods
}

result will be the following:

{
    "name": "John",
    "address": {
       "street": "Stormtroopers str. 1"
    },
    "_links": {
       "self": "http://localhost:8080/users/1",
       "address": "http://localhost:8080/addresses/1"
    }
}

This is the only way how you can implement it

Sign up to request clarification or add additional context in comments.

Comments

0

It is unclear from your question that you are using a Collection on one side of the relationship (the "Many" in OneToMany).

There are two ways you can achieve this:

1) If you do not define a Repository for the child type, a link will not be generated, and it will instead be embedded in the parent object. Of course, this only is useful if you don't need a Repository for that object.

2) FetchType.EAGER will eagerly load the child object. Since the object is already loaded, there is no reason to provide a link to it, so spring-data-rest will embed the object. Make sure to put this on the side with the single object, not the Collection

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.