Spring not loading data even with FetchType.EAGER set

做~自己de王妃 提交于 2019-12-05 16:00:57

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!