休眠延迟加载不起作用(@JoinColumnOrFormulas)

时间:2018-07-06 12:36:50

标签: java hibernate lazy-loading

我上这堂课

@javax.persistence.Entity
@Table(name = "OD_STOCK")
@Inheritance(strategy = InheritanceType.JOINED)
@PrimaryKeyJoinColumn(name = "OD_STOCKID")
@XmlRootElement(name = "Stock")
public class Stock extends Entity  {

 @ManyToOne(fetch = FetchType.LAZY)
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumnsOrFormulas({@JoinColumnOrFormula(formula = @JoinFormula(value = "STOCKGROUPCODE", referencedColumnName = "GD_CODEID")),
            @JoinColumnOrFormula(formula = @JoinFormula(referencedColumnName = "GD_CODETYPE", value = "'STOCKGROUP'"))})
    private StockGroup stockGroup;

}

但是当我加载Stock时,stockGroup总是被加载...延迟加载不起作用。

我在SQL日志中看到此SELECT来加载stockGroup

   select
        stockgroup0_.GD_CODEID as GD_CODEI2_52_0_,
        stockgroup0_.GD_CODETYPE as GD_CODET1_52_0_,
        stockgroup0_.DBUSERINS as DBUSERIN3_52_0_,
        stockgroup0_.DBUSERUPD as DBUSERUP4_52_0_,
        stockgroup0_.AU_USERIDINS as AU_USERI5_52_0_,
        stockgroup0_.AU_USERIDUPD as AU_USERI6_52_0_,
        stockgroup0_.INSERTDATE as INSERTDA7_52_0_,
        stockgroup0_.MODIFYDATE as MODIFYDA8_52_0_,
        stockgroup0_.VERSION as VERSION9_52_0_,
        stockgroup0_.GD_CODE as GD_CODE10_52_0_,
        stockgroup0_.ISACTIVE as ISACTIV11_52_0_,
        stockgroup0_.PRINTORDER as PRINTOR12_52_0_,
        stockgroup0_.XMLGD_CODE as XMLGD_C13_52_0_ 
    from
        GD_CODE stockgroup0_ 
    where
        stockgroup0_.GD_CODEID=? 
        and stockgroup0_.GD_CODETYPE=? 
        and stockgroup0_.GD_CODETYPE='STOCKGROUP'

更糟糕的是,当OD_STOCK中的GD_CODEID为NULL时,由于GD_CODEID为NULL而使SELECT不必要时,将完成加载对象的选择。

当GD_CODEID为NULL时,GD_CODEID绑定为NULL。

为什么?我该怎么办?

0 个答案:

没有答案