St.Antario
St.Antario

Reputation: 27375

Why does Hibernate put inner join in the case of one-to-one relationship?

I have two entities: UnsubscribedPartner for unsubscribed from mailing partners

@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 partner's class

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

I constructed the following criteria query:

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"));

But the result SQL query is

    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

Inner join is not appropriate here, because the unsubscribed_partner tables may not any partner from the partner table, therefore I need LEFT OUTER JOIN instead. How can I fix that?

Upvotes: 1

Views: 185

Answers (1)

Predrag Maric
Predrag Maric

Reputation: 24403

The documentation states that createCriteria(String, String) is functionally equivalent to createCriteria(String, String, int) using CriteriaSpecification.INNER_JOIN for the joinType.

So, try with createCriteria("unsubscribedPartner", "unsbcr", CriteriaSpecification.LEFT_JOIN) instead.

Upvotes: 1

Related Questions