即使设置了FetchType.EAGER,Spring也不会加载数据 [英] Spring not loading data even with FetchType.EAGER set
问题描述
我有两个模型,我正在尝试从REST API(宠物和媒体)获取数据.我试图通过FetchType.EAGER批注获取宠物和急切加载的媒体之间的oneToMany关系,但是在编写MediaRepository时没有出现数据.如果我没有实现该文件,则媒体关系和数据会在响应中返回.
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.
实现MediaRepository.java后,GET/pets返回:
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?
...
}
没有实现MediaRepository.java,GET/pets返回:
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> {
}
推荐答案
这与Hibernate提取策略无关.
This has nothing to do with Hibernate fetch strategy.
您看到的行为是Spring Data Rest设计的工作方式.为媒体定义存储库后,您将看到响应中提供了一个链接,供客户端检索关联的媒体项.没有存储库,则必须在响应中插入关联,因为当然没有办法独立地检索集合.
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:
例如
http://example.com/api/pets/1?projection=inlineData
http://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts
这篇关于即使设置了FetchType.EAGER,Spring也不会加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!