【问题标题】:Hibernate many-to-many query休眠多对多查询
【发布时间】:2015-11-30 21:20:11
【问题描述】:

我需要用 hibernate 做这个查询 SQL

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

对象类

@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;

干预类

    @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;

【问题讨论】:

  • 你遇到了什么错误?
  • 我很想做,但每次我想要的结果都不一样,我做不到
  • 你能显示Intervenant类吗?
  • 显示两个类
  • 首字母大写;语法;降噪。

标签: java hibernate jakarta-ee jpa


【解决方案1】:

我建议:

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

这将返回Objectif 对象列表,您可以使用getIntervenants() 获取有关您在SQL 查询中拥有的干预的附加信息。

【讨论】:

  • 你能说得更清楚些吗?您收到错误消息吗?或者您没有得到预期的结果?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-17
  • 2013-02-13
  • 1970-01-01
  • 1970-01-01
  • 2021-05-31
相关资源
最近更新 更多