【发布时间】:2012-10-16 21:35:24
【问题描述】:
有没有办法使用 id 而不是 POJO 对象来保持 LAZY 加载和反序列化对象。
我有 2 个通过多对多关系连接的班级。
类似的东西
public class User {
@Id
@JsonProperty
public long id;
@ManyToMany(
fetch = FetchType.EAGER,
)
@JoinTable(
name = "User_EntityType",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "type_id")
)
@JsonProperty
public Set<Type> types;
}
public class Type {
@Id
@JsonProperty
public long id;
@ManyToMany(
fetch = FetchType.EAGER,
mappedBy = "types",
targetEntity = User.class
)
@JsonProperty
public Set<User> users;
}
数据类型工作得很好。我可以毫无问题地使用休眠进行读写。
但是,我希望能够使用 REST API 返回一个 User 对象,因此我使用 Jackson 来反序列化它。问题是当我这样做时,它会反序列化 User 对象中的每个 Type,其中包括其他 Type 对象,这会造成巨大的混乱。
是否可以只返回 Set of Long 类型 ID 而不是 Set of Type?
【问题讨论】: