gpt4 book ai didi

java - Hibernate 上 @JsonIgnore 的 Jsonparsing 错误

转载 作者:行者123 更新时间:2023-12-02 12:24:17 25 4
gpt4 key购买 nike

当 Jackson 尝试将我的数据解析为 Json 时,我收到此异常:

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: packagename.Thing.Stuffs, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->packagename.Stuff[“thing"]->packagename.Thing[“stuffs"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: packagename.Thing.Stuffs, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->packagename.Stuff[“thing"]->packagename.Thing[“stuffs"])

我有以下实体(名称已替换为东西和事物):

东西:

@Entity
@Table(name = "stuff")
@SQLDelete(sql = "UPDATE stuff SET deleted = 1 WHERE id = ?")
@Where(clause = "deleted = 0")
public class Stuff implements Serializable {

private Long id;
private Thing thing;
private String stuffName;

@Id
@Column(name = "id", unique = true, nullable = false)
@GeneratedValue
public Long getId() {
return this.id;
}

@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, optional = false)
@JoinColumn(name = "thing_id", nullable = false)
public Thing getThing() {
return thing;
}

@Transient
public String getStuffName() {
return stuffName;
}

// Setters and constructor(s) omitted

}

事情:

@Entity
@Table(name = "thing")
public class Thing implements Serializable {

private Long id;
private String name;
private List<Stuff> stuffs;


@Id
@Column(name = "id", unique = true, nullable = false)
@GeneratedValue
public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

@Column(name = "name", unique = false, nullable = false, length = 45)
public String getName() {
return this.name;
}

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "thing_id")
@JsonIgnore
public List<Stuffs> getStuffs() {
return stuffs;
}


// Setters and constructor(s) omitted
}

我一直在谷歌上搜索此类错误。当 json 解析器尝试解析由于延迟加载而未加载的对象时,似乎会发生这种情况。延迟加载似乎默认处于启用状态。我想避免将所有内容设置为 eager,所以我改为使用 @JsonIgnore。对我来说没有任何改变。我们将不胜感激,这让我发疯。

编辑:

在两个类中添加 @JsonIgnore 和 @Eager 给我带来了另一个问题。一个异常看起来像这样:

org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.doResolveException Handling of [org.springframework.http.converter.HttpMessageNotWritableException] resulted in Exception java.lang.IllegalStateException: Cannot call sendError() after the response has been committed

在 stackoverflow 上搜索它给我这个:Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed)

该链接基本上是说我应该添加 @JsonIgnore,而且我已经这样做了。

最佳答案

根据评论,您说您正在使用 Jackson 1.9.10,这将允许您通过添加 READ_ONLY< 来使用 @JsonProperty 注释的新语法WRITE_ONLY 访问类型属性。

所以你可以使用:

@JsonProperty(access = Access.WRITE_ONLY)

使用您的字段定义。

欲了解更多详情,您可以查看Only using @JsonIgnore during serialization, but not deserialization 讨论。

注意:

对于旧版本,您可以在类成员 getter 上使用 @JsonIgnore 并在其 setter 上使用 @JsonProperty

关于java - Hibernate 上 @JsonIgnore 的 Jsonparsing 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45563904/

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