0

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).

The underlying tables look like this:

workbook_config -> workbook_config_sheet_join <- sheet_config

If I just run it in my SQL utility, a successful SQL statement looks like this:

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.

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. 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. 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. 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.

Organus
  • 169
  • 2
  • 10

1 Answers1

0

Did you look into the setProjection method on Criteria? Using the setProjection method lets you select properties or objects from the executed query.

Hibernate Criteria documentation

Mathias G.
  • 4,875
  • 3
  • 39
  • 60
  • Unfortunately, this is not correct. The setProjection method only works on simple parameters, not on Sets. – Organus Sep 15 '14 at 15:04
  • Did you try something like Projections.projectionList() and add this list as your Criteria projection? – Mathias G. Sep 15 '14 at 15:29
  • Projections do not work on Collections.--See the note in this reference: http://stackoverflow.com/questions/18060880/getting-collections-using-projectionhibernate – Organus Sep 15 '14 at 19:24