Hibernate的Jackson属性映射问题

时间:2018-11-07 12:07:46

标签: java hibernate jackson jackson-databind jackson-modules

我已经做了很多研究,但是我尝试实现的所有解决方案都没有起作用……我在下面的类中有很多@JoinColumn @ManyToOne关系:

public class Consultas implements Serializable {

    @Basic(optional = false)
    @Column(name = "status")
    private boolean status;

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idconsultas")
    private Integer idconsultas;
    @Basic(optional = false)
    @Column(name = "data_consulta")
    @Temporal(TemporalType.DATE)
    private Date dataConsulta;
    @Basic(optional = false)
    @Column(name = "hora_consulta")
    @Temporal(TemporalType.TIME)
    private Date horaConsulta;
    @JoinColumn(name = "idforma_pagamento", referencedColumnName = "idforma_pagamento")
    @ManyToOne(optional = false)
    private FormaPagamento idformaPagamento;
    @JoinColumn(name = "idfuncionario", referencedColumnName = "idfuncionario")
    @ManyToOne(optional = false)
    private Funcionario idfuncionario;
    @JoinColumn(name = "idpaciente", referencedColumnName = "idpaciente")
    @ManyToOne(optional = false)
    private Paciente idpaciente;
    @JoinColumn(name = "idplano", referencedColumnName = "idplano")
    @ManyToOne(optional = false)
    private Plano idplano;

当我尝试使用下面的代码(来自接口)映射此类的实例时:

JSON.generateJSON(this.consultas, Consultas.class);

generateJSON方法:

public static String generateJSON(Object o, Class valueType)
{ 
            if (valueType == Consultas.class) {
                                Consultas k = (Consultas) o;
                                mapper.writeValue(json, k);
}

输出文件还带来了@ JoinColumn @ ManyToOne关系中的每个属性(即,来自“ Plano”,“ Paciente”,“ FormaPagamento”和“ Funcionario”类的每个属性)。为什么要映射所有属性,而不是只映射Consultas类中包含的“ id”?

我的问题是我想只将Consultas类中的属性写为JSON,而从其关系中不包含任何其他属性。

也许它已经有了答案,但是我根本找不到合适的解决方案。预先感谢

0 个答案:

没有答案