具有多个连接的JPA条件构建器

时间:2010-10-06 09:02:49

标签: java join jpa-2.0 criteria-api

我正在努力创建一个使用多个表的jpa查询。我似乎无法理解如何将表连接在一起。这是我想要创建的查询:

SELECT algm.m_l_i, algnsm.n_s_i
  FROM algm, alg, algnsm, mal
 WHERE algm.l_g_i = alg.l_g_i
   AND alg.l_g_t = 'xxx'
   AND algnsm.l_g_i = algm.l_g_i
   AND mal.c_l_i = algm.m_l_i
   AND mal.p_l_i = 'yyy';

我尝试了几种方法,从使用join运算符到使用键上的where运算符。我无法在网上找到足够多关于联接的信息。一旦我尝试加入超过1张桌子,我就会卡住。一些建议或提示真的很有用。 谢谢

CriteriaBuilder b = em.getCriteriaBuilder();
CriteriaQuery<Tuple> q = builder.createTupleQuery();
Root<MAL> malRoot = query.from(MAL.class);
Root<ALGM> algmRoot = query.from(ALGM.class);

// error
algmRoot.join(ML_.mLI);
// error
malRoot.join(ML_.mLI);

Predicate e1 = builder.equal(malRoot.get(MAL_.pL).get(ML_.mLI), "abc");
Predicate e2 = builder.equal(malRoot.get(MAL_.cL), algmRoot.get(ALGM_.mL));

query.where(builder.and(e1, e2));

query.select(builder.tuple(malRoot.get(MAL_.pL), malRoot.get(MAL_.cL), algmRoot.get(ALGM_.aLG).get(ALG_.lGT)));

@Entity
@Table(name = "M_A_L")
public class MAL implements Serializable {
   @EmbeddedId
   protected MALPK malPK;

   @Basic(optional = false)
   @Column(name = "ENTRY_IND")
   private String entryInd;

   @JoinColumn(name = "P_L_I", referencedColumnName = "M_L_I", insertable = false, updatable = false)
   @ManyToOne(optional = false)
   private ML pL;

   @JoinColumn(name = "C_L_I", referencedColumnName = "M_L_I", insertable = false, updatable = false)
   @ManyToOne(optional = false)
   private ML cL;
 ... 
 }

@Entity
@Table(name = "A_L_G_M")
public class ALGM implements Serializable {

    @EmbeddedId
    protected ALGMPK algmPK;

    @JoinColumn(name = "M_L_I", referencedColumnName = "M_L_I", insertable = false, updatable = false)
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    private ML mL;

    @JoinColumn(name = "L_G_I", referencedColumnName = "L_G_I", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private ALG aLG;
 ....
 }

public class ALGM_ { 
   public static volatile SingularAttribute<ALGM, ML> mL;
   public static volatile SingularAttribute<ALGM, ALGMPK> aLGMPK;
   public static volatile SingularAttribute<ALGM, ALG> aLG;
}

public class MAL_ { 
   public static volatile SingularAttribute<MAL, String> eI;
   public static volatile SingularAttribute<MAL, MALPK> mALPK;
   public static volatile SingularAttribute<MAL, ML> pL;
   public static volatile SingularAttribute<MAL, ML> cL;
}

public class ALG_ { 
   public static volatile CollectionAttribute<ALG, MW> mWC;
   public static volatile SingularAttribute<ALG, ALGCM> aLGCM;
   public static volatile SingularAttribute<ALG, ALGAVM> aLGAVM;
   public static volatile SingularAttribute<ALG, TR> tZ;
   public static volatile CollectionAttribute<ALG, ALGM> aLGMC;
   public static volatile SingularAttribute<ALG, String> d;
   public static volatile SingularAttribute<ALG, String> lGT;
   public static volatile SingularAttribute<ALG, String> lGI;
   public static volatile SingularAttribute<ALG, ALGNSM> aLGNSM;
   public static volatile SingularAttribute<ALG, ALGFVM> aLGFVM;
   public static volatile SingularAttribute<ALG, ALGNAM> aLGNAM;
}

1 个答案:

答案 0 :(得分:1)

加入JPA查询就像这样:

创建从A到B的连接:

Root<A> root .... 
CriteriaQuery<?> query .....
CriteriaBuilder builder.....

Join<A, B> BTable = root.join(SingularAttribute<A, B>);

Join<A, B> BTable = root.join(A_.B);

一旦你有了bTable的引用,就可以将它用于路径变量。