2012-12-11 114 views
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對象。 這是可能使用傑克遜還是我需要實現自定義代碼?

+1

對於這個特定的情況下,你需要使用串行/解串器自定義HTT電話號碼://wiki.fasterxml.com/JacksonHowToCustomSerializers –

回答

0

備註:我是EclipseLink JAXB (MOXy)的領導者和JAXB (JSR-222)專家組的成員。

我不確定這個用例是否在傑克遜支持,但下面是如何使用MOXy的@XmlPath擴展來做到這一點的一個例子。

密鑰

通過指定參考對象的@XmlPath(".")內容被拉昇到對應於所述源對象中的節點。

import java.io.Serializable; 

import javax.persistence.*; 
import javax.xml.bind.annotation.*; 
import org.eclipse.persistence.oxm.annotations.XmlPath; 

@Entity 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Key implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @Id 
    @Column(name = "KeyID") 
    @XmlElement(name="keyID") 
    private Long id; 

    @Column 
    private String description; 

    @ManyToOne(cascade = CascadeType.ALL) 
    @JoinColumn(name = "KeyTypeID", nullable = false) 
    @XmlPath(".") 
    private KeyType keyType; 

} 

關鍵字類型

@XmlElement註釋被利用,以映射到JSON密鑰。你需要

import java.io.Serializable; 
import javax.persistence.*; 
import javax.xml.bind.annotation.XmlElement; 

@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) 
    @XmlElement(name="keyTypeName") 
    private String name; 

} 

jaxb.properties

要指定莫西爲您的JAXB提供者包括在同一封裝稱爲jaxb.properties爲您的域模型具有以下項文件:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory 

演示

演示代碼下面將您的JSON文檔轉換爲/從您的域模型。

import java.util.*; 
import javax.xml.bind.*; 
import javax.xml.transform.stream.StreamSource; 
import org.eclipse.persistence.jaxb.JAXBContextProperties; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     Map<String, Object> properties = new HashMap<String, Object>(); 
     properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json"); 
     properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false); 
     JAXBContext jc = JAXBContext.newInstance(new Class[] {Key.class}, properties); 

     Unmarshaller unmarshaller = jc.createUnmarshaller(); 
     StreamSource json = new StreamSource("src/forum13819583/input.json"); 
     Key key = unmarshaller.unmarshal(json, Key.class).getValue(); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(key, System.out); 
    } 

} 

輸入。JSON /輸出

{ 
    "keyID": 1,  
    "description": "key 1", 
    "keyTypeName": "Type 5" 
} 

更多信息