有人可以帮我构建我需要的hibernate查询吗?
我有两个表:区域和 AreasLocal 。第一个结构为树表,包含应用程序的菜单,第二个作为区域名称的i18n存储库。
我希望根据所选语言(idLocal属性)尽可能优化整个区域菜单树(急切地,尽可能少地使用查询)。
在下一个查询中,我正确地绘制了区域树菜单,除了本地化名称,这些名称显示为混合(某些区域名称为西班牙语,其他区域为英语等):
Query query = this.sessionFactory.getCurrentSession().createQuery(
"select distinct areas" +
" from Areas areas" +
" left join fetch areas.children childs " +
" left join fetch areas.locales locales" +
" where areas.visible = 1 and areas.parent.idArea = :idArea" +
" and areasLocal.id.idLocal = :idLocal " +
" order by areas.orden ");
query.setParameter("idArea", idArea);
query.setParameter("idLocal", idLocal);
public class Areas implements Serializable {
private Integer idArea;
private Areas parent;
private Integer idTipoAreas;
private Boolean visible;
private Integer orden;
private Set<Areas> children = new HashSet<Areas>(0);
private Set<AreasLocal> locales = new HashSet<AreasLocal>(0);
//setters getters ommited
}
public class AreasLocal implements Serializable {
private AreasLocalId id;
private String name;
//setters getters ommited
}
public class AreasLocalId implements Serializable {
private int idArea;
private int idLocal;
//setters getters ommited
}
<hibernate-mapping>
<class name="Areas" table="areas">
<id name="idArea" type="java.lang.Integer">
<column name="ID_AREA" />
</id>
<many-to-one name="parent" class="Areas" fetch="select">
<column name="ID_PARENT"/>
</many-to-one>
<property name="idTipoAreas" type="java.lang.Integer">
<column name="ID_TIPO_AREAS" not-null="true" />
</property>
<property name="visible" type="java.lang.Boolean">
<column name="ES_VISIBLE" not-null="true" sql-type="bit" />
</property>
<property name="orden" type="java.lang.Integer">
<column name="ORDEN" />
</property>
<set name="children" table="areas" inverse="true" lazy="false" fetch="select" order-by="ID_AREA">
<key>
<column name="ID_PARENT"/>
</key>
<one-to-many class="Areas" />
</set>
<set name="locales" table="areas_local" inverse="true" lazy="false" fetch="select">
<key>
<column name="ID_AREA"/>
</key>
<one-to-many class="AreasLocal" />
</set>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="AreasLocal" table="areas_local">
<composite-id name="id" class="AreasLocalId">
<key-property name="idArea" type="java.lang.Integer">
<column name="ID_AREA" />
</key-property>
<key-property name="idLocal" type="java.lang.Integer">
<column name="ID_LOCAL" />
</key-property>
</composite-id>
<property name="name" type="string">
<column name="NAME" not-null="true" />
</property>
</class>
</hibernate-mapping>
我已经意识到HQL 不承认递归连接。
所以,你必须做这样的事情:但是也没有按预期工作,只检索到有孩子的深层菜单。
Query query = this.sessionFactory.getCurrentSession().createQuery(
"select distinct areas0" +
" from Areas areas0" +
" left join fetch areas0.children areas1" +
" left join fetch areas0.locales areas0Locales" +
" left join fetch areas1.children areas2" +
" left join fetch areas1.locales areas1Locales" +
" left join fetch areas2.children areas3" +
" left join fetch areas2.locales areas2Locales" +
" left join fetch areas3.children areas4" +
" left join fetch areas3.locales areas3Locales" +
//"..." +
" where areas0.esVisible = 1" +
" and areas0.parent.idArea = :idArea" +
" and areas0Locales.id.idLocal = :idLocal " +
" and areas1Locales.id.idLocal = :idLocal " +
" and areas2Locales.id.idLocal = :idLocal " +
" and areas3Locales.id.idLocal = :idLocal " +
//"..." +
" order by areas0.orden ");
任何帮助都会得到赞赏