khchan khchan - 1 month ago 12
Java Question

Spring not loading data even with FetchType.EAGER set

I have two models I'm trying to get data back from a REST API (Pet and Media). I'm trying to get the oneToMany relationship between pet and media eagerly loaded via the FetchType.EAGER annotation, but the data doesn't appear when I write the MediaRepository. If I don't implement that file, the media relationship and data comes back in the response.

With MediaRepository.java Implemented, GET /pets returns:

{
"id": 72,
"name": "Spot",
"description": "Annoying as hell",
"media": [], <-- why is this here only if I don't implement MediaRepository?
...
}


Without MediaRepository.java Implemented, GET /pets returns:

{
"id": 72,
"name": "Spot",
"description": "Annoying as hell",
... (No media array in response)
}


Pet.java

@Entity
public class Pet implements Serializable {

private static final long serialVersionUID = 1L;

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

@Column(name="name")
private String name;

@Column(name="description")
private String description;

@OneToMany(cascade=CascadeType.ALL, mappedBy="pet", FetchType.EAGER, orphanRemoval=true)
private List<Media> media;

@ManyToOne
private Category category;

@Enumerated(EnumType.STRING)
private Status status;
}


Media.java

@Entity
public class Media implements Serializable {
private static final long serialVersionUID = 1L;

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

@Column(name="url")
private String url;

@Column(name="title")
private String title;

@ManyToOne
private Pet pet;
}


PetRepository.java

import org.springframework.data.jpa.repository.JpaRepository;

public interface PetRepository extends JpaRepository<Pet, Long> {
}


MediaRepository.java

import org.springframework.data.jpa.repository.JpaRepository;

public interface MediaRepository extends JpaRepository<Media, Long> {
}

Answer

This has nothing to do with Hibernate fetch strategy.

The behavior you are seeing is how Spring Data Rest is designed to work. When you have defined a Repository for Media then you will see that a link is provided in the response for clients to retrieve the associated Media items. Without the repository then the association has to be in-lined in the response as there is of course no means to retrieve the collection independently.

If you wish to selectively in-line collections in a response then you can do by defining a Projection.

@Projection(name = "inlineData", types=Pet.class)
public interface PetProjection{

    Long getId();
    String getName();
    String getDescription();
    List<Media> getMedia();
}

You can have this projection applied automatically to a collection resource:

@RepositoryRestResource(excerptProjection = PetProjection.class)
public interface PetRepository extends JpaRepository<Pet, Long> {}

For item resources client would typically specify that they want this data in-line:

e.g.

http://example.com/api/pets/1?projection=inlineData

http://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts