Mount List mapped with @ManyToOne only with some objects

0

Is it possible to assemble a list in a class with only a few objects?

@NamedQuery(name = "Curso.Aprovados", query = "select cur from Classificacao cl JOIN cl.candidato ca JOIN ca.curso cur")               
public class Curso {

    @Id
    @Column(name = "id")
    private int id;
    @OneToMany(fetch = FetchType.LAZY)
    private List<Candidato> candidatoList;

    ...  
 }

public class Candidato {   

    @Id
    @Column(name = "id")
    private Integer idCa;
    @JoinColumn(name = "id_curso", referencedColumnName = "id")
    @ManyToOne(optional = false, cascade = CascadeType.DETACH)
    private Curso curso;

   ...
 }

public class Classificacao {

    @Id
    @Column(name = "id")
    private int id;
    @OneToOne
    @JoinColumn(name = "id_candidato", referencedColumnName = "id")
    private Candidato candidato;

  ...
  }

I would like the candidate list in the course class to only contain the candidates that have a rating, in case that Select contemplates what I need, I just do not know how to load those candidates into the course list.

select cur from Classificacao cl JOIN cl.candidato ca JOIN ca.curso cur

Thank you very much

    
asked by anonymous 25.05.2017 / 20:57

1 answer

0

Dude, you can map the classification in the candidate and make the query from Course, join with candidate. Something like:

public class Candidato {   

@Id
@Column(name = "id")
private Integer idCa;
@JoinColumn(name = "id_curso", referencedColumnName = "id")
@ManyToOne(optional = false, cascade = CascadeType.DETACH)
private Curso curso;
@OneToOne(mappedBy="candidato")
private Classificacao classificacao;

...  }

Select c From Curso c join c.candidatoList cl where cl.classificacao is not null

This will popular the list of course applicants. I think that solves it.

Edit1: And supposing your query had the correct logic for what you need:

select cur from Classificacao cl JOIN cl.candidato ca JOIN ca.curso cur

You would not need all of these joins because the class is @OneToOne with Candidate and candidate is @ManyToOne with Course, both are fetch type eager which means that when hibernate loads the Candidate, it will load along the corresponding course (if no need to explicitly do this with join.

Some links to explain better about fetch type:

link link

    
25.05.2017 / 23:49