Jackson attribute mapping problem with Hibernate

72 Views Asked by At

I've researched a lot, but none of the solutions I tried to implement worked... I have the class below, which has many @JoinColumn @ManyToOne relations:

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;

When I try to map an instance of this class with the code below, coming from an interface:

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

The generateJSON method:

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

The output file also brings EVERY attribute from the @JoinColumn@ManyToOne relations (i.e every attribute from the class "Plano","Paciente","FormaPagamento" and "Funcionario"). Why is it mapping all the attributes instead of only the "id" contained inside the Consultas class?

My problem is that I want to write as JSON simply the attributes from the Consultas class, without any additional attributes from its relations.

Maybe it already has an answer, but I couldn't find a proper solution at all. Thanks in advance

0

There are 0 best solutions below