有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

具有distinct的条件的java hibernate连接将不起作用

当我运行时,我得到了预期实际查询的结果查询

实际查询(待生成、期望查询) 不同的[ID]

select distinct rc.* from ratecodeparam rcp , ratecodes rc where rc.travelfrom <= '2011-04-22' and rc.travelto >= '2011-04-25' and rc.id = rcp.p_id;

select distinct rc.* from ratecodeparam rcp join ratecodes rc on rc.travelfrom <= '2011-04-22' and rc.travelto >= '2011-04-25' and rc.id = rcp.p_id;

我想要

distinct rc.*

代码

我通过了密码,但距离rc*

  session.createCriteria(RateCode.class)
            .add(Restrictions.le("travelFrom", from.getTime()))
            .createCriteria("rateCodeParams", "rcp")
    list = crit.list();

费率代码。哈佛商学院。xml

<class catalog="hermes" name="com.RateCode"  table="ratecodes">
        <id name="rateCodeId" type="java.lang.Integer">
            <column name="id"/>
            <generator class="native"/>
        </id>
        <property name="code" type="string">
            <column length="32" name="code" unique="true"/>
        </property>
        <set name="rateCodeParams" cascade="all, delete-orphan" order-by="param">
            <key>
                <column name="p_id"  />
            </key>
            <one-to-many class="com.RateCodeParam" />
         </set>
</class>

RateCodeParam。哈佛商学院。xml

<class catalog="hermes" name="com.RateCodeParam" table="ratecodeparam">
        <id name="id" type="java.lang.Integer">
            <column name="id"/>
            <generator class="identity"/>
        </id>       
        <many-to-one  class="com.RateCode" name="rateCode" insert="false" fetch="join" update="false" > 
            <column name="p_id" />
        </many-to-one>
</class>

共 (1) 个答案

  1. # 1 楼答案

    您的查询缺少联接,因为您没有向查询中添加联接。设置获取模式(setFetchMode)只会为现有连接添加更多细节。但在你的查询中,它并不存在

    正确的查询可能如下所示:

    Criteria crit = session.createCriteria(RateCode.class)
        .add(Restrictions.le("travelFrom", from.getTime()))
        .createCriteria("rateCodeParams", "rcp");
    list = crit.list();