Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jackson-hibernate实体序列化_Java_Json_Hibernate_Jackson - Fatal编程技术网

Java Jackson-hibernate实体序列化

Java Jackson-hibernate实体序列化,java,json,hibernate,jackson,Java,Json,Hibernate,Jackson,我想将包含其他实体的hibernate实体序列化并反序列化为平面JSON格式。 假设我有以下实体: 关键: 键类型: @Entity public class KeyType implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "KeyTypeID") private Long id; @Column(name = "Name", nullable = f

我想将包含其他实体的hibernate实体序列化并反序列化为平面JSON格式。 假设我有以下实体:

关键:

键类型:

@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实体的键对象。
这是否可以使用Jackson实现,或者我需要实现自定义代码?

注意:我是专家组的负责人和成员

我不确定Jackson是否支持这个用例,但下面是一个示例,说明如何使用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;

}
按键类型

利用
@xmlement
注释映射到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

要将MOXy指定为JAXB提供程序,您需要在与域模型相同的包中包含一个名为
JAXB.properties
的文件,其中包含以下条目:

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

下面的演示代码包含将JSON文档转换为域模型或从域模型转换为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);
    }

}
了解更多信息


对于这种特定情况,您需要使用自定义序列化/反序列化器
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);
    }

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