Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I looked at a few different pages including the following: </p> <p>Including this one which looks like an elegant solution for folks using hibernate 3.5 or JPA 2.0 (with annotations):</p> <p><a href="https://stackoverflow.com/questions/2735071/hibernate-criteria-api-adding-a-criterion-string-should-be-in-collection">Hibernate Criteria API - adding a criterion: string should be in collection</a></p> <p>This one eventually pointed me to a solution: <a href="https://forum.hibernate.org/viewtopic.php?f=1&amp;t=942839&amp;start=0" rel="nofollow noreferrer">https://forum.hibernate.org/viewtopic.php?f=1&amp;t=942839&amp;start=0</a></p> <p>In the end I did a manual subselect, I'm not happy with it but it works. I can't believe that this is the preferred solution but I couldn't find any other. It's about as ugly as a solution can be and still warrant the name :-(</p> <pre><code>public void runFilter(AdminNoteFilter _filter) { assert _filter != null; Criteria criteria = this.getSession().createCriteria(FollowUp.class); if (_filter.hasMainCategories()) { CriteriaQueryTranslator cqt = null; Criterion mainCategoriesCriterion = HibernateFilterUtil.getEnumIdentifierCriterion(_filter.getMainCategories(), "{alias}.id in " + "(select fup.id " + "from follow_up fup, follow_up_main_categories v " + "where fup.id = v.fup_id and v.identifier in (" + HibernateFilterUtil.SUBSTITUE_QUESTION_MARKS + "))"); criteria.add(mainCategoriesCriterion); } List&lt;FollowUp&gt; adminNotes = (List&lt;FollowUp&gt;) criteria.list(); } /** * constructs a criterion for filtering on a collection of enums using a subselect. * &lt;br /&gt;https://stackoverflow.com/questions/2967199/hibernate-criteria-filtering-on-a-set-of-enum-values * &lt;br /&gt;https://forum.hibernate.org/viewtopic.php?f=1&amp;t=942839&amp;start=0 * * @param _enums non null non empty * @param _subSelect non null must contain the {@link #SUBSTITUE_QUESTION_MARKS} string to be substituted * @return the Criterion that can be added to a Criteria */ private static Criterion getEnumIdentifierCriterion(Set&lt;? extends MarshallableEnum&gt; _enums, String _subSelect) { assert _enums != null; assert _enums.size() &gt; 0; assert _subSelect != null; assert _subSelect.contains(SUBSTITUE_QUESTION_MARKS); Set&lt;String&gt; identifiersSet = MarshallableEnumUtil.getIdentifiersFromMarshallableEnums(_enums); String[] identifiers = identifiersSet.toArray(Constants.EMPTY_STRING_ARRAY); // taken from //https://forum.hibernate.org/viewtopic.php?f=1&amp;t=942839&amp;start=0 final org.hibernate.type.Type[] types = new org.hibernate.type.Type[identifiers.length]; Arrays.fill(types, org.hibernate.Hibernate.STRING); final StringBuilder questionMarks = new StringBuilder(); for (int i = 0; i &lt; identifiers.length; i++) { if (i &gt; 0) { questionMarks.append(","); } questionMarks.append("?"); } // substitute in the question marks to the sub select subselect String finalSubSelect = _subSelect.replace(SUBSTITUE_QUESTION_MARKS, questionMarks); return Restrictions.sqlRestriction(finalSubSelect, identifiers, types); } private static Set&lt;String&gt; getIdentifiersFromMarshallableEnums(Set&lt;? extends MarshallableEnum&gt; _enums) { Set&lt;String&gt; retSet = new HashSet&lt;String&gt;(); for (MarshallableEnum tmpEnum : _enums) { retSet.add(tmpEnum.getIdentifier()); } return retSet; } </code></pre> <p>Hope this helps, if you anyone finds a better solution for the version of hibernate (3.2) please post it to help the community</p> <p>Cheers Simon</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload