gpt4 book ai didi

java - 为什么Hibernate在一对一关系的情况下要放inner join?

转载 作者:太空宇宙 更新时间:2023-11-04 06:21:11 24 4
gpt4 key购买 nike

我有两个实体:UnsubscribedPartner 用于从邮寄合作伙伴取消订阅

@Entity
@Table(schema = "mailing", name = "unsubscribed_partner")
public class UnsubscribedPartner {

@Id
@Column(name = "partner_id")
private int partnerId;

@Column(name = "unsubscription_date")
private Date date;

@OneToOne(targetEntity = Partner.class, fetch = FetchType.EAGER)
@JoinColumn(name = "partner_id")
private Partner partner;

//GET, SET
}

Partner 合作伙伴的类

@Entity
@Table(schema = "partner", name = "partner")
public class Partner {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;

@Column(name = "email")
private String email;

@OneToOne(fetch = FetchType.EAGER, mappedBy = "partner")
private UnsubscribedPartner unsubscribedPartner;

//GET, SET
}

我构造了以下条件查询:

String email;
//...
Criteria criteria = getSession().createCriteria(Partner.class);
if(!(email == null)){
criteria.add(Restrictions.eq("email", email));
}
Criteria unsubscribedCrieria = criteria.createCriteria("unsubscribedPartner", "unsbcr");
unsubscribedCrieria.add(Restrictions.isNull("unsbcr.reason"));

但是SQL查询的结果是

    select
count(*) as y0_
from
partner.partner this_
inner join
mailing.unsubscribed_partner unsbcr1_
on this_.id=unsbcr1_.partner_id
where
unsbcr1_.unsubscription_reason_id is null

内部联接在这里不合适,因为unsubscribed_pa​​rtner表可能不是partner表中的任何合作伙伴,因此我需要LEFT OUTER JOIN来代替。我该如何解决这个问题?

最佳答案

documentation声明 createCriteria(String, String) 在功能上等同于 createCriteria(String, String, int) 使用 CriteriaSpecification.INNER_JOIN 作为 joinType

因此,请尝试使用 createCriteria("unsubscribedPartner", "unsbcr", CriteriaSpecification.LEFT_JOIN) 来代替。

关于java - 为什么Hibernate在一对一关系的情况下要放inner join?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27402837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com