简体   繁体   中英

Hibernate many-to-many query

I need to do this query with hibernate SQL

SELECT * 
FROM objectif AS ob, objectifs_intervenants AS oi
WHERE ob.collaborateurId =2
AND ob.objectifId = oi.objectifId
AND oi.personneId =1

Ojectif class

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "objectifId")
private int objectifId;

@ManyToMany(mappedBy = "objectifs")
private List<Intervenant> intervenants;

@ManyToOne
@JoinColumn(name = "collaborateurId")
private Collaborateur collaborateur;

Intervenant class

    @Entity
@DiscriminatorValue("intervenant")
public class Intervenant extends Personne 
{
    @ManyToMany
    @JoinTable(name="objectifs_intervenants", joinColumns={@JoinColumn(name = "personneId")},
                inverseJoinColumns = {@JoinColumn(name = "objectifId")})
    private List<Objectif> objectifs;

I would suggest:

select ob from Objectif ob join Intervenant i
where ob.collaborateur.collaborateurId = :collaborateurId
and i.personneId = :personneId

This will return a list of Objectif objects and you can use getIntervenants() to get the additional info about intervenants that you had in your SQL query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM