2
我想將包含其他實體的hibernate實體序列化並反序列化爲扁平的JSON格式。 所以我們可以說,我有以下實體:Jackson - hibernate實體序列化
重點:
@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對象。 這是可能使用傑克遜還是我需要實現自定義代碼?
對於這個特定的情況下,你需要使用串行/解串器自定義HTT電話號碼://wiki.fasterxml.com/JacksonHowToCustomSerializers –