gpt4 book ai didi

java - hibernate Join 与不同的条件将不起作用

转载 作者:行者123 更新时间:2023-12-01 15:52:21 25 4
gpt4 key购买 nike

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

实际查询(待生成、期望查询)不同的[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();

RateCode.hbm.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.hbm.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>

最佳答案

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

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

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

关于java - hibernate Join 与不同的条件将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5763369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com