From 90fdca9053d1d0f7d14e2c7b20c243ca62d07305 Mon Sep 17 00:00:00 2001 From: David Mansfield Date: Thu, 10 Mar 2011 11:50:36 -0500 Subject: [PATCH] HHH-3646 - throw a better exception when criteria is placed directly on component In the CriteriaQueryTranslator, we process the path given by a SubCriteria object looking for the entity name for the property. If the SubCriteria was mistakenly created on a component type, we will exit the loop using the owning entity, and will eventually end up failing (throwing an exception) trying to lookup up the restricted property against the entity, instead of against the component. Fix this by throwing a more informative exception, and modify the documentation to be explicit about how to do this properly. --- .../manual/en-US/content/query_criteria.xml | 21 ++++++++++++++++++- .../criteria/CriteriaQueryTranslator.java | 6 +++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/documentation/src/main/docbook/manual/en-US/content/query_criteria.xml b/documentation/src/main/docbook/manual/en-US/content/query_criteria.xml index 51cdc10353..466a14da11 100644 --- a/documentation/src/main/docbook/manual/en-US/content/query_criteria.xml +++ b/documentation/src/main/docbook/manual/en-US/content/query_criteria.xml @@ -250,7 +250,26 @@ while ( iter.hasNext() ) { - + +
+ Components + + + To add a restriction against a property of an embedded component, the component property + name should be prepended to the property name when creating the Restriction. + The criteria object should be created on the owning entity, and cannot be created on the component + itself. For example, suppose the Cat has a component property fullName + with sub-properties firstName and lastName: + + + + + +
+
Example queries diff --git a/hibernate-core/src/main/java/org/hibernate/loader/criteria/CriteriaQueryTranslator.java b/hibernate-core/src/main/java/org/hibernate/loader/criteria/CriteriaQueryTranslator.java index 4e784f0196..abae77f85b 100755 --- a/hibernate-core/src/main/java/org/hibernate/loader/criteria/CriteriaQueryTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/loader/criteria/CriteriaQueryTranslator.java @@ -269,7 +269,11 @@ public class CriteriaQueryTranslator implements CriteriaQuery { componentPath = ""; } else if ( type.isComponentType() ) { - componentPath += '.'; + if (!tokens.hasMoreTokens()) { + throw new QueryException("Criteria objects cannot be created directly on components. Create a criteria on owning entity and use a dotted property to access component property: "+path); + } else { + componentPath += '.'; + } } else { throw new QueryException( "not an association: " + componentPath );