Is it possible to retrieve a custom Object with @Formula annotation? I would like to retrieve the code and the description of a specific table in one of my entities:
@Formula(value = "(SELECT s.cod_setor AS code, s.descricao_setor AS description"
" FROM service.sector s LEFT JOIN service.employee e "
" ON (s.cpf = e.cpf) "
" WHERE e.cpf=cpf)")
private SectorDTO sector;
CodePudding user response:
It can be done with @PostLoad
usage.
@Formula(value = "(SELECT concat(s.cod_setor, '_', s.descricao_setor)"
" FROM service.sector s LEFT JOIN service.employee e "
" ON (s.cpf = e.cpf) "
" WHERE e.cpf=cpf)")
// select multiple fields as one string where values are divided by '_'
private String sectorFormula;
@Transient
private SectorDTO sector;
@PostLoad
private void onl oad() {
String[] parts = sectorFormula.split("_");
this.sector = new SectorDTO(parts[0], parts[1]);
}