在多对多关联</collection>中按<collection element's =“”property =“”>排序

时间:2013-07-29 16:07:35

标签: java hibernate hql

我有三张桌子。 OrderLocationOrder_Location其中Order_Location是包含多对多关系的表格。

OrderList<Location>Location拥有名为city的财产。使用HQL(Hibernate 3.6 for Java),我希望得到特定订单的所有位置,按city排序。

在hbm文件中,使用 idbag 映射List<Location>。虽然我得到了HQL,但生成的SQL查询已连接到LocationOrder_Location表两次,我认为这是开销。

我在这里做错了什么?

SELECT o.locationList FROM Order o
join o.locationList locList  
where o.orderId = 1
order by locList.city desc

转换为下面的内容

select
  order4_.LOC_ID as order1_355_,
  order4_.LOC_CODE as order2_355_,
  order4_.CITY as order3_355_,
  order4_.CITY_LONG_NAME as order4_355_ 
from
   sche.order order0_ 
   inner join
   sche.order_location order1_ 
   on order0_.ORDER_ID=order1_.ORDER_ID 
   inner join
   sche.location order2_ 
   on order1_.LOC_ID=order2_.LOC_ID 
   inner join
   sche.order_location order3_ 
   on order0_.ORDER_ID=order3_.ORDER_ID 
   inner join
   sche.location order4_ 
   on order3_.LOC_ID=order4_.LOC_ID 
 where
   order0_.ORDER_ID=1 
 order by
   order2_.city desc

=========编辑

Order.hbm.xml

  <?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"  

 >
<hibernate-mapping>
    <class name="collectionorderby.Order" table="ORDER">
        <id name="orderId" type="string">
            <column name="ORDER_ID" length="32" />
            <generator class="uuid" />
        </id>

        <idbag name="locationList" lazy="false" table="ORDER_LOCATION" fetch="select">
            <collection-id column="ORDER_LOCATION_ID" type="string">
                <generator class="uuid" />
            </collection-id>
            <key>
                <column name="ORDER_ID" length="32" not-null="true" />
            </key>
            <many-to-many column="LOC_ID" class="collectionorderby.Location"
                fetch="join" />
        </idbag>

    </class>
</hibernate-mapping>


Location.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
    <class name="collectionorderby.Location" table="LOCATION">
        <id name="locId" type="string">
            <column name="LOC_ID" length="50" />
        </id>

        <property name="locCode" type="string">
            <column name="LOC_CODE" length="50" />
        </property>
        <property name="city" type="string">
            <column name="CITY" length="50" />
        </property>
        <property name="cityLongName" type="string">
            <column name="CITY_LONG_NAME" length="500" />
        </property>

    </class>
</hibernate-mapping>

====编辑 注意到当我们提供order by时,翻译的查询从表的第一个实例获取select,其中使用表的第二个实例完成order by。我想,如果我们避免使用这些表的重复实例,就不会发生这种情况。

1 个答案:

答案 0 :(得分:0)

以下按预期工作。别名locList用于选择而不是o.locationList

SELECT locList  FROM Order o join o.locationList locList  where o.orderId = 1 order by locList.city desc