我在Criteria中有以下查询,需要2分钟才能执行(从方法返回)
public final Data getData(final Long dataid) {
Criteria dataCriteria = session.createCriteria(Data.class)
.setFetchMode("mgr")
.setFetchMode("tmgr")
.setFetchMode("comp")
.createAlias("myent","myent", Criteria.LEFT_JOIN)
.createAlias("myent.party", "pardata", Criteria.LEFT_JOIN)
.createAlias("slabs","slabs", Criteria.LEFT_JOIN)
.createAlias("tpr", "tpr", Criteria.LEFT_JOIN)
.createAlias("tpr.par", "par",Criteria.LEFT_JOIN)
.createAlias("par.LE_EN", "le",Criteria.LEFT_JOIN)
.createAlias("le.RAT", "ler",Criteria.LEFT_JOIN)
.createAlias("c","c",Criteria.LEFT_JOIN)
.createAlias("c.crd","crd",Criteria.LEFT_JOIN)
.createAlias("crd.coc", "coc",Criteria.LEFT_JOIN)
.createAlias("coc.thpr","cocthpr",Criteria.LEFT_JOIN)
.createAlias("cocthpr.par", "cocpar",Criteria.LEFT_JOIN)
.add(Restrictions.eq("dataid", dataid));
return (Data) dataCriteria.uniqueResult();
}
我将它重构为HQL,现在从方法/执行查询返回需要10秒。以下是HQL版本。
public final Data getData(final Long dataid) {
StringBuffer qbuffer = new StringBuffer();
qbuffer.append("select data from com.a.b.c.Data as data");
qbuffer.append(" left join fetch data.mgr mgr");
qbuffer.append(" left join fetch data.tmgr tmgr");
qbuffer.append(" left join fetch data.comp comp");
qbuffer.append(" left join fetch data.myent myent");
qbuffer.append(" left join fetch myent.par pardata");
qbuffer.append(" left join fetch data.slabs slabs");
qbuffer.append(" left join fetch data.tpr tpr");
qbuffer.append(" left join fetch tpr.par par");
qbuffer.append(" left join fetch par.LE_EN le");
qbuffer.append(" left join fetch le.RAT ler");
qbuffer.append(" left join fetch data.cd c");
qbuffer.append(" left join fetch data.crd crd");
qbuffer.append(" left join fetch crd.coc coc");
qbuffer.append(" left join fetch coc.thpr cocthpr");
qbuffer.append(" left join fetch cocthpr.par cocpar");
qbuffer.append(" where data.dataid = :dataid ");
Query query = getSession().createQuery(qbuffer.toString()).setLong("dataid", dataid);
return (Data)query.uniqueResult();
}
有人可以解释一下吗?如果需要,我将提供HBM映射(懒惰/渴望等)。注意 - >除了从Criteria到HQL的转换之外,没有其他任何修改。