0

我正在制作一个 spring-boot 项目,目前我面临延迟获取的问题。我有三个类,一个是通过 Eager(Incident) 获取其子级,两个是通过惰性 (FileContent,FileStorage) 获取子级。结构是:

Incident(1) -> (m)FileContent(1) -> (m)FileStorage.

每当我从 Incidet 获取所有文件内容和所有文件存储时,也会获取。这不应该发生,不应该获取 fileStorages。如果有人能告诉我为什么代码会有这样的行为并帮助我修复它,那就太好了。

这些是类:

@Getter
@Setter
@Entity
public class Incident extends BaseEntity {

    @JsonIgnore
    @LazyCollection(LazyCollectionOption.FALSE)
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "incident", orphanRemoval = true)
    private List<FileContent> fileContent;
}

@Entity
@Getter
@Setter
public class FileContent extends BaseEntity {

    @Column
    private String fileName;

    @OneToOne(mappedBy = "fileContent", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    private FileStorage fileStorage;

    @JsonIgnore
    @ManyToOne
    @JoinColumn(name = "incident_id")
    @LazyCollection(LazyCollectionOption.TRUE)
    private Incident incident;
}

@Getter
@Setter
@Entity
public class FileStorage extends BaseEntity {

    @Lob
    @Column
    private byte[] content;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "file_content_id")
    private FileContent fileContent;
}

@Service(value = "ins")
public class IncidentService {

    public Page<Incident> findAll(Pageable pageable) {
        Page<Incident> incidents = incidentRepository.findAll(pageable);

        if (CollectionUtils.isEmpty(incidents.getContent())) {
            return null;
        }

        return incidents;
    }
}

这是 yml 属性文件

application.yml 
  - open-in-view: false

spring boot jpa java
4

1 回答 1

0

改变了@OneToOne(fetch = FetchType.LAZY), to @OneToOne(optional = false, fetch = FetchType.LAZY),它工作得很好。

于 2018-04-01T17:39:45.563 回答