我有这个原生SQL请求:
SELECT LMC_PARAMETRE.* FROM LMC_PARAMETRE
INNER JOIN INTERVENTION ON INTERVENTION.ID_INTV = LMC_PARAMETRE.ID_INTV
INNER JOIN REF_SITE ON INTERVENTION.DID_SITE =REF_SITE.ID_SITE
WHERE INTERVENTION.DCD_STATUT_INTV = '0' AND REF_SITE.ID_CENT = '097';
我喜欢在jpql中转换它,但是“ON”关键字无法识别:
JpaQueryBuilder builder = new JpaQueryBuilder();
builder.append("SELECT lmp FROM "+LmcParametre.class.getName()+" AS lmp ");
builder.append("INNER JOIN "+Intervention.class.getName()+" AS intv ON ");
builder.append("intv.idIntv = lmp.intervention.idIntv ");
builder.append("INNER JOIN "+Site.class.getName()+" AS site ON ");
builder.append("intv.didSite = site.idSite ");
builder.append("WHERE ");
builder.append(lt("intervention.statutIntv", String.valueOf(constanteInferieurePretACharger)));
builder.append("site.centre.idCent = "+idCentre);
根据HQL文档:HQL中的联接是使用实体之间的关联完成的。 但是,我不明白这意味着什么。
由于
答案 0 :(得分:0)
假设您已使用JPA(包括它们的关系)正确建模这些实体,那么您的所有JPQL都是:“从LmcParametre lp内部连接lp.intervention中选择lp我内部连接i.site s其中s.idSite =: idSite和i.statutIntv =:statut“。
假设您已将此JPQL查询注册为LmcParametre类的命名查询(使用注释
@NamedQueries({ @NamedQuery( name="myQuery", query = "select lp from LmcParametre lp inner join lp.intervention i inner join i.site s where s.idSite = :idSite and i.statutIntv = :statut" ) })
然后你可以像这样创建一个类型化的查询:
TypedQuery<LmcParametre> query = entityManager.createNamedQuery("myQuery", LmcParametre.class);
query.setParameter("idSite", myIdSite);
query.setParameter("statut", myStatut);
List<LmcParametre> results = query.getResultList();