首先,我知道解决方案有多个线程,但是我不理解它们,它们似乎也根本没有帮助我。我知道问题出在哪里。由于会话已关闭,因此延迟获取。我以为@Transactional会有所帮助,但没有帮助。而且我知道我不应该在控制器中使用注释。解决此问题后,我将对其进行更改。我还将创建一个DTO而不是对实体进行注释。

我得到了实体MlpConfig,我想将alle配置发送到前端。但是与其他实体的连接产生了以下问题:

Could not write JSON: could not initialize proxy [com.project.data.ActivationFunction#4] - no Session; nested exception is com.fasterxml.jackson.databind.JsonMappingException: could not initialize proxy...


我需要响应中的所有信息(ActivationFunction,Layers),而我真的不想获取EAGER。即使我在MlpConfig中将其设置为EAGER,ActivationFunction也会抱怨相同的问题(由于与MlpConfig的连接)

实体MlpConfig(响应在所有异物上均带有@JsonIgnore):

@Entity
@Data
@NoArgsConstructor
public class MlpConfig {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @NotBlank(message = "Name for the MlpConfig is required")
    @Column(name = "name", nullable = false, length = 255)
    private String name;

    private String description;

    @NotNull(message = "Batch Size is required")
    private int batchSize;

    @NotNull(message = "Epoch Number is required")
    private int epochNumber;

    @EqualsAndHashCode.Exclude
    @ToString.Exclude
    @ManyToOne(fetch = FetchType.LAZY)
    private ActivationFunction activationFunction;

    @JsonIgnore
    @OneToMany(mappedBy = "mlpConfig", orphanRemoval = true)
    private Set<Layer> layers = new HashSet<>();

    @JsonIgnore
    @EqualsAndHashCode.Exclude
    @ToString.Exclude
    @ManyToOne(fetch = FetchType.LAZY)
    private User user;

    private Timestamp lastUpdated;

    public MlpConfig(String name, String description, int batchSize, int epochs, Set<Layer> layers,
            ActivationFunction activationFunction, User user) {
        this.name = name;
        this.description = description;
        this.batchSize = batchSize;
        this.epochNumber = epochs;
        this.activationFunction = activationFunction;
        this.layers = layers;
        this.lastUpdated = Timestamp.from(Instant.now());
        this.user = user;
    }


方法的调用:

@Transactional
@GetMapping("/getAllConfigs")
public ResponseEntity<Object> getAllMlpConfig(@RequestHeader HttpHeaders headers, Authentication authentication) {
    User user = userService.findByUsername(authentication.getName());
    List<MlpConfig> configs = mlpConfigService.findAllByUser_id(user.getId());

    return ResponseEntity.status(HttpStatus.OK).body(configs);
}


实体ActivationFunction

@Entity
@Data
@NoArgsConstructor
@Table(name = "activationFunctions")
public class ActivationFunction {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Enumerated(EnumType.STRING)
    @Column(length = 20)
    private EActivationFunction type;

    @ToString.Exclude
    @OneToMany(mappedBy = "activationFunction")
    private Set<MlpConfig> mlpConfig;

    public ActivationFunction(EActivationFunction type) {
        this.type = type;
    }
}


那么如何处理呢?我刚刚发现这种针对Spring MVC的解决方案似乎很旧。

最佳答案

一整天后,我实际上可以解决此问题。

根据对此答案https://stackoverflow.com/a/37840526/10565504的评论:

对于Gradle,我将此行添加到了我的build.gradle

compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-hibernate5', version: '2.11.0'


为了使其正常工作,我添加了:

@Bean
public Module datatypeHibernateModule() {
    return new Hibernate5Module();
}


之后,我不得不怀疑我的代码中存在无限递归。我可以使用以下答案进行修复:https://stackoverflow.com/a/18288939/10565504

另一个问题是,我在@ToString.Exclude实体中的字段activationFunction上使用了MlpConfig批注。输入的值始终为null,而没有toString。

//@EqualsAndHashCode.Exclude
//@ToString.Exclude
@ManyToOne(fetch = FetchType.LAZY)
private ActivationFunction activationFunction;

10-08 04:16