Spring 引导获取延迟 - 无法写入 JSON:无法初始化代理 - 无会话



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

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

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...

我需要响应中的所有信息(激活函数,层(,我真的不想获取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

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

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

最新更新