我想将包含其他实体的休眠实体序列化和反序列化为平面 JSON 格式。假设我有以下实体:
钥匙:
@Entity
public class Key implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Column(name = "KeyID")
private Long id;
@Column
private String description;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "KeyTypeID", nullable = false)
private KeyType keyType;
public Long getId() {
return id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public KeyType getKeyType() {
return keyType;
}
public void setKeyType(KeyType keyType) {
this.keyType = keyType;
}
}
密钥类型:
@Entity
public class KeyType implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "KeyTypeID")
private Long id;
@Column(name = "Name", nullable = false, unique = true)
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我想将 Key 类的对象序列化为:
{
"keyID": 1,
"description": "key 1",
"keyTypeName": "Type 5" //this is Key.keyType.name
}
我还希望能够将上面的 JSON 反序列化为包含 KeyType 实体的 Key 对象。这可能使用杰克逊还是我需要实现自定义代码?