I need to get the index of an entity within a persistent collection.
For example, the mapping contains the following set:
Code:
<set name="photos" table="photo_category_map_view" order-by="date_taken, photo">
<key column="category"/>
<many-to-many column="photo" class="eg.Photo"/>
</set>
I naively tried using
Code:
session.createFilter(category.getPhotos(), "select index(this) where this=:photo").setEntity("photo", photo).uniqueResult();
but it doesn't work, resulting in the exception
Code:
org.hibernate.QueryException: could not resolve property: index of: eg.Photo [select index(this) where this=?]
at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1310)
at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(FromElementType.java:280)
at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:373)
at org.hibernate.hql.ast.tree.MethodNode.resolveCollectionProperty(MethodNode.java:114)
at org.hibernate.hql.ast.tree.MethodNode.collectionProperty(MethodNode.java:95)
at org.hibernate.hql.ast.tree.MethodNode.resolve(MethodNode.java:44)
at org.hibernate.hql.ast.HqlSqlWalker.processFunction(HqlSqlWalker.java:842)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.functionCall(HqlSqlBaseWalker.java:2328)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:1963)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:1825)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1394)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:553)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:128)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:83)
at org.hibernate.engine.query.FilterQueryPlan.<init>(FilterQueryPlan.java:24)
at org.hibernate.engine.query.QueryPlanCache.getFilterQueryPlan(QueryPlanCache.java:94)
at org.hibernate.impl.SessionImpl.getFilterQueryPlan(SessionImpl.java:1444)
at org.hibernate.impl.SessionImpl.createFilter(SessionImpl.java:1247)
(snip)
The Hibernate reference says that the index() function works for "for elements of a one-to-many association or collection of values."
Can it be used for many-to-many persistent collections, or how else would I determine the index without iterating over the entire set?
Hibernate version: 3.2.0cr4
Name and version of the database you are using: Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production