简体   繁体   中英

Hibernate OneToMany with OrderBy generates invalid SQL

I have a one-to-many relationship as follows:

public class Player {
    private int           id;
    private String        name;
    private List<Payment> payments;

    ... accessors etc
}

public class Payment {
    private int        id;
    private Date       when;
    private BigDecimal amount;

    ... accessors etc
}

orm to describe these:

<entity-mappings ... version="2.0">

    <entity class="domain.Player" access="FIELD">
        <attributes>
            <id name="id">
                <generated-value strategy="AUTO" />
            </id>

            <basic name="name" optional="false" />

            <one-to-many name="payments" fetch="EAGER">
                <order-by>when</order-by>
                <cascade><cascade-all/></cascade>
            </one-to-many>
        </attributes>
    </entity>

    <entity class="domain.Payment" access="FIELD">
        <attributes>
            <id name="id">
                <generated-value strategy="AUTO" />
            </id>

            <basic name="when"   optional="false" />
            <basic name="amount" optional="false" />
        </attributes>
    </entity>

</entity-mappings>

This works fine with Hibernate 4.2.2.Final, but if I update to 4.3.4.Final it fails whenever I try to load objects of type player. The SQL generated by 4.3.4.Final is along the lines of :

select ... 
from Player player0_ 
left outer join Player_Payment payments1_ on player0_.id=payments1_.Player_id 
left outer join Payment payment2_ on payments1_.payments_id=payment2_.id 
where player0_.id=? 
order by payments1_.when

ie the ORDER BY is on the join table, not the target table.

Any ideas - am I doing something wrong here or does this look like a Hibernate bug ?

Mapping is correct. This Hibernate bug reported in HHH-8834 . Attached test case in bug report uses annotations, but otherwise it matches to the one in question.

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