Entity attribute based on a field in another table - Hibernate

1

I have a microservice raised using Spring, RestFul and Hibernate. I would like to know how I can modify an attribute of this json microservice, based on a condition, obtained through a query to the bank.

Below, a piece of code from my Entity .

@Entity
@Table(name = "funcoes")
@EntityListeners(AuditingEntityListener.class)
@JsonIgnoreProperties(allowGetters = true)
public class Funcoes {

@Id
@Column(name = "FUNCOD")
private String funcod;


@Column(name = "FUNFOR")
private String funfor;


@Column(name = "SISTEMA")
private String sistema;

}
}

I would like to compare the sistema attribute, if, based on a query, I display a x value or a y value in my json.

    
asked by anonymous 28.11.2018 / 17:18

1 answer

1

You need more flexibility to do what you want. And you're having this problem because you're using the JPA entity also to represent Json. In general, it is best to avoid mixing these two responsibilities.

What I point out to you is to create a new class that will be used just to represent the information in Json. We can call it FuncoesJson or FuncoesDto :

public class FuncoesDto {

    private String funcod;
    private String funfor;
    private String sistema;

    //gets e sets

}

For you to change what goes in the sistema variable, you can make this decision before it is popular with the Funcoes entity information. Here's an example:

Funcoes funcoes = em.findOne(123L, Funcoes.class);

FuncoesDto dto = new FuncoesDto();
dto.setFuncod(funcoes.getFuncod());
dto.setFunfor(funcoes.getFunfor());

if (funcoes.getSistema().equals("valorConsulta")) {
    dto.setSistema("X");
} else {
    dto.setSistema("Y");
}

return dto;
    
10.12.2018 / 15:52