我想在HQL中编写以下SQL查询-

select * from ITPMAST
inner join STKMAST on ITPMAST.ITPITP=STKMAST.stkstk where STKMAST.STKSIT='S101'


Itpmast类

public class Itpmast implements java.io.Serializable {

    private String itpitp;
    private Sizmast sizmast;
    private User usdmastByItpaddusd;
    private User usdmastByItpamdusd;
    private Prmmast prmmast;
    private Sctmast sctmast;
    private String itpnam;
    private short itpfcn;
    private short itptcn;
    private BigDecimal itpcpr;
    private BigDecimal itpspr;
    private String itpactyn;
    private String itpbscode;
    private String itpcolor;
    private BigDecimal itpspr1;
    private BigDecimal itpspr2;
    private BigDecimal itpspr3;
    private Date itpsactddt1;
    private Date itpsactddt2;
    private Date itpsactddt3;
    private Date itpaddddt;
    private String itpaddtime;
    private Date itpamdddt;
    private String itpamdtime;
    private Short itpbscodeord;
    private String itptrnactno;
    private String itpactcode;
    private Date itpsactddt;
    private String itpprtflg;
    private Set<Skumast> skumasts = new HashSet<Skumast>(0);
    private Set<Igpmast> igpmasts = new HashSet<Igpmast>(0);
    private Set<Dismastb> dismastbs = new HashSet<Dismastb>(0);
    private Set<Dismastd> dismastds = new HashSet<Dismastd>(0);
    private Set<Rbttran> rbttrans = new HashSet<Rbttran>(0);
    private Set<Lytmast> lytmasts = new HashSet<Lytmast>(0);
    private Set<Stkmast> stkmasts = new HashSet<Stkmast>(0);
    private Set<Sslmast> sslmastsForSslitp = new HashSet<Sslmast>(0);
    private Set<Sslmast> sslmastsForSslssl = new HashSet<Sslmast>(0);
}


斯特卡斯特班

public class Stkmast implements java.io.Serializable {

    private StkmastId id;
    private User usdmastByStkamdusd;
    private User usdmastByStkaddusd;
    private Sctmast sctmast;
    private Sitmast sitmast;
    private Itpmast itpmast;
    private BigDecimal stksohqty;
    private BigDecimal stksohqty01;
    private BigDecimal stksohqty02;
    private BigDecimal stksohqty03;
    private BigDecimal stksohqty04;
    private BigDecimal stksohqty05;
    private BigDecimal stksohqty06;
    private BigDecimal stksohqty07;
    private BigDecimal stksohqty08;
    private BigDecimal stksohqty09;
    private BigDecimal stksohqty10;
    private BigDecimal stksohqty11;
    private BigDecimal stksohqty12;
    private BigDecimal stksohqty13;
    private BigDecimal stksohqty14;
    private BigDecimal stksohqty15;
    private BigDecimal stksohqty16;
    private BigDecimal stksohqty17;
    private BigDecimal stksohqty18;
    private BigDecimal stksohqty19;
    private BigDecimal stksohqty20;
    private Date stkaddddt;
    private String stkaddtime;
    private Date stkamdddt;
    private String stkamdtime;
    private String stkcat;
    private Date stkltddt;
    private Date stklsalddt;
    private Date stklpurddt;
    private Date stklsalrtddt;
    private Date stklpurrtddt;
    private Date stklshortddt;
    private Date stklexcessddt;
    private Date stkltransferin;
    private Date stkltransferout;
    private BigDecimal stkmedsohqty;
    private BigDecimal stkmedsohqty01;
    private BigDecimal stkmedsohqty02;
    private BigDecimal stkmedsohqty03;
    private BigDecimal stkmedsohqty04;
    private BigDecimal stkmedsohqty05;
    private BigDecimal stkmedsohqty06;
    private BigDecimal stkmedsohqty07;
    private BigDecimal stkmedsohqty08;
    private BigDecimal stkmedsohqty09;
    private BigDecimal stkmedsohqty10;
    private BigDecimal stkmedsohqty11;
    private BigDecimal stkmedsohqty12;
    private BigDecimal stkmedsohqty13;
    private BigDecimal stkmedsohqty14;
    private BigDecimal stkmedsohqty15;
    private BigDecimal stkmedsohqty16;
    private BigDecimal stkmedsohqty17;
    private BigDecimal stkmedsohqty18;
    private BigDecimal stkmedsohqty19;
    private BigDecimal stkmedsohqty20;
}


我最大的尝试是

from Stkmast s
join fetch s.itpmast i where s.sitmast.sitsit=:siteCode and i.itpitp like :code


但是此HQL查询会产生许多SQL查询来获取数据。谢谢。

最佳答案

您需要将获取类型设置为join,而不是映射中的默认select

Example

<hibernate-mapping>
    <class name="com.mkyong.common.Stock" table="stock">
        <set name="stockDailyRecords"  cascade="all" inverse="true"
            table="stock_daily_record" batch-size="10" fetch="join">
            <key>
                <column name="STOCK_ID" not-null="true" />
            </key>
            <one-to-many class="com.mkyong.common.StockDailyRecord" />
        </set>
    </class>
</hibernate-mapping>

10-06 12:35