Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Left Outer Join

Tags:

java

hibernate

I have a Hibernate service method as such: "SELECT sc FROM SecurityContact sc WHERE sc.securityId=:securityId2". securityId2 is passed in by the user. Each SecurityContact has a many to one relationship with a Contact, so Hibernate automatically calls a join when this query runs. However, the join that Hibernate always runs is an inner join, which will not work properly for my purposes. Is there any way to force Hibernate to internally generate a left outer join instead? Here is the code for the SecurityContact class:

/**
 * The persistent class for the SecurityContact database table.
 * 
 */
@Entity
@FXClass(kind=FXClassKind.REMOTE)
public class SecurityContact implements Serializable {
    private static final long serialVersionUID = 1L;
    @Transient private String uid;
    @FXIgnore
    public String getUid() {
        if (uid == null) {
            uid = "" + securityContactId;
        }
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="securityContact_id")
    private Long securityContactId;

    @Column(name="security_id")
    private String securityId;

    @Column(name="create_date")
    private String createDate;

    @Column(name="create_user")
    private String createUser;

    @Column(name="modify_date")
    private String modifyDate;

    @Column(name="modify_user")
    private String modifyUser;

    //bi-directional many-to-one association to AgentContact

    @ManyToOne
    @JoinColumn(name="agent_id", referencedColumnName="contact_id")
    private AgentContact agentContact;

    //bi-directional many-to-one association to AuditContact
    @ManyToOne
    @JoinColumn(name="audit_id", referencedColumnName="contact_id")
    private AgentContact auditContact;

    public SecurityContact() {
    }
    @FXKeyColumn
    public Long getSecurityContactId() {
        return this.securityContactId;
    }

    public void setSecurityContactId(Long securityContactId) {
        this.securityContactId = securityContactId;
    }

    public String getSecurityId() {
        return this.securityId;
    }

    public void setSecurityId(String securityId) {
        this.securityId = securityId;
    }

    public String getCreateDate() {
        return this.createDate;
    }

    public void setCreateDate(String createDate) {
        this.createDate = createDate;
    }

    public String getCreateUser() {
        return this.createUser;
    }

    public void setCreateUser(String createUser) {
        this.createUser = createUser;
    }

    public String getModifyDate() {
        return this.modifyDate;
    }

    public void setModifyDate(String modifyDate) {
        this.modifyDate = modifyDate;
    }

    public String getModifyUser() {
        return this.modifyUser;
    }

    public void setModifyUser(String modifyUser) {
        this.modifyUser = modifyUser;
    }
    @FXManyToOne(parent="parent", property="contactId")
    public AgentContact getAgentContact() {
        return this.agentContact;
    }

    public void setAgentContact(AgentContact agentContact) {
        this.agentContact = agentContact;
    }
    @FXManyToOne(parent="parent", property="contactId")
    public AgentContact getAuditContact() {
        return this.auditContact;
    }

    public void setAuditContact(AgentContact auditContact) {
        this.auditContact = auditContact;
    }

} 
like image 297
John Roberts Avatar asked Aug 01 '11 21:08

John Roberts


2 Answers

Try fetch=select on the many-to-one matching

Also see http://community.jboss.org/wiki/AShortPrimerOnFetchingStrategies and http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html

like image 174
dfb Avatar answered Sep 25 '22 05:09

dfb


According to the Hibernate documentation the hibernate query language should support this. (At least in version 3.3)

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-joins

Try to create your query like this:

Query query = entityManager.createQuery("Select sc from SecurityContact as sc " +
                                        "left outer join sc.agentContact as c " +
                                        "where sc.securityId=:securityId2";

EDIT: Changed the contact property of sc to the agentContact property you supplied in your question details.

like image 34
david Avatar answered Sep 24 '22 05:09

david