简体   繁体   English

如何在不返回根实体的情况下返回休眠集合实体

[英]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方法可让您从执行的查询中选择属性或对象。

Hibernate Criteria documentation 休眠标准文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 map 在实体内部使用 Hibernate 实体集合? - How to map a collection of entities, inside an entity, using Hibernate? 删除实体,它是集合的元素,并且在Hibernate中具有另一个实体的集合 - Deleting entity, which is element of collection and has collection of another entities in Hibernate 休眠实体:没有实体属性的列 - Hibernate entities: columns without entity properties 如何限制Hibernate仅从Criteria API中涉及多个实体的根实体生成带有列的SQL查询 - How to restrict Hibernate to generate SQL query with columns only from root entity in Criteria API involving multiple entities 如何在Hibernate中访问实体集合? - How to Access Entity collection in Hibernate? 休眠如何将一个实体扩展到所有实体 - hibernate how to extend one entity to all the entities 如何在不返回集合本身的情况下返回集合的数据? - How to return collections' data without returning a collection itself? Hibernate-仅通过类知道ID返回的实体集合 - Hibernate - Return collection of entities by ID knowing only the class 从Hibernate中的集合中删除项目而不加载实体 - Delete item from collection in Hibernate without loading entities 休眠,保存新实体而不获取相关实体 - Hibernate, Saving new entity without fetching associated entities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM