gpt4 book ai didi

java - Spring Boot 获取惰性 - 无法写入 JSON : could not initialize proxy - no Session

转载 作者:行者123 更新时间:2023-12-01 16:22:19 26 4
gpt4 key购买 nike

首先,我知道有几个带有解决方案的线程,但我不理解它们,而且它们似乎根本没有帮助我。我知道问题出在哪里了。由于 session 已关闭而导致懒惰。我认为 @Transactional 会有帮助,但事实并非如此。而且我知道我不应该在 Controller 中使用注释。当我解决这个问题后我会改变它。我还将创建一个 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...

我需要响应中的所有信息(ActivationFunction、图层),但我真的不想获取 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 注释。没有 toString 的传入值始终为 null

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

关于java - Spring Boot 获取惰性 - 无法写入 JSON : could not initialize proxy - no Session,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62236342/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com