为什么在休眠中生成额外的交叉连接?

时间:2012-05-29 17:16:42

标签: java hibernate java-ee hql

Hibernate查询

hql.append("FROM TBucket t1 JOIN FETCH t1.prd JOIN FETCH t1.psAct ");

配置

TBucket
@Table (name = "vw_f_t_bucket")

      @ManyToOne (cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.EAGER, targetEntity = Posprd.class)
      @JoinColumns({
          @JoinColumn(name = "prod_key", referencedColumnName="prod_key"),
          @JoinColumn(name = "prod_cycle_key", referencedColumnName="cycle_key"),
      })

      public IPosprd getprd() {
        return prd;
      }

prd ( 

@Table (name = "snp_prd")
public class Posprd implements IPosprd {

)


      @ManyToOne (cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.EAGER, targetEntity = Pospsact.class)
      @JoinColumn (name = "ps_act_key", nullable = false)
      public IPospsact getpsact() {
            return psAct;
      }      

psAct (
    @Table (name = "vw_ps_act_firm")
    public class PosPsact implements IPospsact {
)

生成查询

FROM vw_f_t_bucket tbucke0_
         INNER JOIN snp_prd posprd1_
            ON tbucke0_.prod_cycle_key = posprd1_.cycle_key
               AND tbucke0_.prod_key = posprd1_.prod_key

        JOIN FETCH t1.psAct                 

         INNER JOIN vw_ps_act_firm pospsn2_
            ON tbucke0_.ps_act_key = pspsn2_.ps_act_key

CROSS JOIN snp_prd posprd3_
CROSS JOIN vw_ps_act_firm pospsn5_
where tbucke0_.prod_cycle_key=posprd3_.cycle_key and tbucke0_.prod_key=posprd3_.prod_key and tbucke0_.posn_acct_key=pospsn5_.posn_act_key

从上面可以看出,hibernate最后生成了交叉连接。这是为什么?如何阻止hibernate生成这些交叉连接?

如果我只使用像

这样的hql
"FROM TBucket t1  ";

正在生成

from vw_f_t_bucket tbucke0_ 
cross join snp_prd psprd1_ cross join vw_psn_act_firm pspsn3_ 

如何让它进行常规连接而不是交叉连接?

1 个答案:

答案 0 :(得分:0)

您已经指定了预先获取并在查询中指示了JOIN FETCH。 JOIN FETCH是不必要的,因为该属性已经为eager fetch注释(因此FROM中的内连接。)