简体   繁体   中英

Why is Hibernate generating a query that joins on the wrong column?

I'm using Java 1.6, Hibernate 3.1, MySQL 5.5, XML mapping. I have two tables, partner and partner_http_account. I'll just show you the relevant fields.

partner:

partnerid dec(22,0) [primary key]

partner_http_account:

partnerhttpacctid dec(22,0) [primary key]
partnerid dec(22,0) [foreign key]

Here's the XML mapping:

<class name="com.rc.model.partner.Partner" table="partner" mutable="true">
    <id name="partnerId" type="int">
        <column name="partnerid" scale="10" precision="0" not-null="true" unique="true" sql-type="int unsigned"/>
        <generator class="com.rc.model.jdbc.sequence.MexpIdentifierGenerator">
            <param name="sequence">seq_partnerid</param>
            <param name="idDataType">int</param>
        </generator>
    </id>
    ...
    <one-to-one name="partnerHTTPAccount" class="com.rc.model.partner.PartnerHTTPAccount" lazy="false"
                 foreign-key="partnerid" cascade="all"/>
</class>

<class name="com.rc.model.partner.PartnerHTTPAccount" table="partner_http_account">
    <id name="partnerHttpAcctId" type="int">
        <column name="partnerhttpacctid" scale="10" precision="0" not-null="true" unique="true" sql-type="int unsigned"/>
        <generator class="com.rc.model.jdbc.sequence.MexpIdentifierGenerator">
            <param name="sequence">seq_partnerhttpacctid</param>
            <param name="idDataType">int</param>
        </generator>
    </id>

    <many-to-one name="partner" class="com.rc.model.partner.Partner" column="partnerid"
                 foreign-key="partnerid" cascade="none"/>
</class>

Here is my Java code:

public List<Partner> getPartnersByDomainStartsWith(String domainStartsWith) {

    Query hQuery = sessionManager.getSession().createQuery("from Partner p " +
            "left outer join fetch p.partnerHTTPAccount " +
            "where p.domain like :domainStartsWith||'%'");

    hQuery.setString("domainStartsWith", domainStartsWith);

    return hQuery.list();
}

And here is what Hibernate is generating:

select partner0_.partnerid as partnerid0_,
partnerhtt1_.partnerhttpacctid as partnerh1_1_,
...
partnerhtt1_.partnerid as partnerid104_1_,
...
from partner partner0_
left outer join partner_http_account partnerhtt1_ on partner0_.partnerid=partnerhtt1_.partnerhttpacctid 
where partner0_.domain like concat(?, '%')

My problem is the generated sql query of these two tables is joining on the wrong field. It should join on partner0.partnerid=partnerhtt1.partnerid instead. Would really appreciate some insight. Thanks.

This is really well described in the Hibernate documentation :

<class name="Person">
    <id name="id" column="personId">
        <generator class="native"/>
    </id>
    <many-to-one name="address" 
        column="addressId" 
        unique="true"
        not-null="true"/>
</class>

<class name="Address">
    <id name="id" column="addressId">
        <generator class="native"/>
    </id>
   <one-to-one name="person" 
        property-ref="address"/>
</class>

Notice how property-ref is used, to indicate that the one-to-one is the inverse side of the association mapped by the address property.

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