Build List mapped with @Manytoone only with a few objects

Asked

Viewed 349 times

0

It is possible to mount 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, only contain candidates who have a rating, in this case this Select includes what I need, just do not know how to load these candidates on the course list.

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

Thank you very much

1 answer

0

Dude, you can map the ranking on 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 candidates of the course. I think this solves.

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

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

You wouldn’t need all these joins because rating is @Onetoone with Candidate and candidate is @Manytoone with Course, both are fetch type Ager which means that when Hibernate loads the Candidate, it will carry along the corresponding course (if any), no need to spell this out with Join.

Some links to better explain fetch type:

http://www.devmedia.com.br/lazy-e-eager-loading-com-hibernate/29554 https://stackoverflow.com/questions/26601032/default-fetch-type-for-one-to-one-many-to-one-and-one-to-many-in-hibernate

Browser other questions tagged

You are not signed in. Login or sign up in order to post.