[英]How to return hibernate collection entities without returning root entity
I would like to get elements of a collection (Set<SheetConfig>) for a set of objects (WorkbookConfig) from Hibernate without getting the primary objects(WorkbookConfig). 我想从Hibernate获取一组对象(WorkbookConfig)的集合(Set <SheetConfig>)的元素,而无需获取主要对象(WorkbookConfig)。
The underlying tables look like this: 基础表如下所示:
workbook_config -> workbook_config_sheet_join <- sheet_config
workbook_config-> workbook_config_sheet_join <-sheet_config
If I just run it in my SQL utility, a successful SQL statement looks like this: 如果仅在SQL实用程序中运行它,则成功的SQL语句如下所示:
SELECT DISTINCT sheet_config_id FROM sheet_config AS sc
LEFT JOIN workbook_config_sheet_join AS wcsj
ON sc.sheet_config_id = wcsj.sheet_config_id
LEFT JOIN workbook_config AS wc
ON wc.workbook_config_id = wcsj.workbook_config_id
WHERE wc.group_id ="1"
ORDER BY sheet_name;
I would like to do this properly without using HQL. 我想在不使用HQL的情况下正确执行此操作。
My UNSUCCESSFUL attempt has resulted in this: 我的不成功尝试导致了以下结果:
@SuppressWarnings("unchecked")
public List<SheetConfig> findAllForUser() {
List<SheetConfig> sheetConfigs = null;
Session session = getSession();
Criteria crit = session.createCriteria(WorkbookConfig.class)
.add(Restrictions.in(GROUP, getGroupsForUser()))
.setFetchMode(SHEET_CONFIGS, FetchMode.JOIN);
sheetConfigs = (List<SheetConfig>) crit.list();
return sheetConfigs;
}
This is still giving me WorkbookConfigs, but what I would like to do in a single pass is get SheetConfigs. 这仍然给了我WorkbookConfigs,但是我想通过一次操作就是get SheetConfigs。 I have spent the day on the Internet trying to find a coherent explanation of the Hibernate API, and I haven't been able to find what I would think is a solution to a fairly common requirement.
我花了整整一天的时间在Internet上寻找关于Hibernate API的连贯解释,但我还找不到我认为可以解决的一个相当普遍的要求。 I can always back out and just do most of the work in Java, but it seems like I should be able to do this with the Hibernate API.
我总是可以退缩,只用Java完成大部分工作,但似乎我应该可以使用Hibernate API来完成此工作。 I appreciate any help, and also, if you can recommend a reference that explains not simply querying collections, but returning them, I would be grateful.
感谢您的帮助,此外,如果您可以推荐参考说明,不仅可以解释查询集合,还可以返回它们,我将不胜感激。
Did you look into the setProjection method on Criteria? 您是否研究了Criteria的setProjection方法? Using the
setProjection
method lets you select properties or objects from the executed query. 使用
setProjection
方法可让您从执行的查询中选择属性或对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.