From: <gca...@us...> - 2012-06-29 13:14:59
|
Revision: 4073 http://openutils.svn.sourceforge.net/openutils/?rev=4073&view=rev Author: gcatania Date: 2012-06-29 13:14:53 +0000 (Fri, 29 Jun 2012) Log Message: ----------- BSHD-15 fixed for FilterMetadataSupport, readability improvement Modified Paths: -------------- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2012-06-22 15:59:20 UTC (rev 4072) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2012-06-29 13:14:53 UTC (rev 4073) @@ -67,7 +67,7 @@ private Character escapeCharacter; - private PropertySelector selector; + private PropertySelector selector = new ExcludeBackrefPropertySelector(); // BSHD-15 private MatchMode matchMode; @@ -342,7 +342,7 @@ { ex.ignoreCase(); } - ex.setPropertySelector(selector != null ? selector : new ExcludeBackrefPropertySelector()); // BSHD-15 + ex.setPropertySelector(selector); Set<String> excludedPropertiesForPath = excludedProperties.get(associationPath); if (excludedPropertiesForPath != null) { Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2012-06-22 15:59:20 UTC (rev 4072) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2012-06-29 13:14:53 UTC (rev 4073) @@ -193,6 +193,7 @@ private Example example(Object entity, Set<String> propertiesToExclude) { Example ex = Example.create(entity); + ex.setPropertySelector(new ExcludeBackrefPropertySelector()); // BSHD-15 for (String propertyName : propertiesToExclude) { // skip properties handled by filterMetadata This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <gca...@us...> - 2012-07-02 12:44:10
|
Revision: 4087 http://openutils.svn.sourceforge.net/openutils/?rev=4087&view=rev Author: gcatania Date: 2012-07-02 12:44:01 +0000 (Mon, 02 Jul 2012) Log Message: ----------- BSHD-11 also fixed in FilterMetadataSupport Modified Paths: -------------- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2012-06-29 16:59:49 UTC (rev 4086) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2012-07-02 12:44:01 UTC (rev 4087) @@ -45,8 +45,6 @@ import org.hibernate.criterion.Example; import org.hibernate.criterion.Example.PropertySelector; import org.hibernate.criterion.MatchMode; -import org.hibernate.criterion.Restrictions; -import org.hibernate.engine.SessionImplementor; import org.hibernate.metadata.ClassMetadata; import org.hibernate.type.Type; @@ -265,7 +263,7 @@ } ClassMetadata classMetadata = sessionFactory.getClassMetadata(Hibernate.getClass(entity)); - addIdentifierRestriction(crit, entity, classMetadata); // BSHD-11 + ExampleTreeUtils.addIdentifierRestriction(crit, entity, classMetadata, sessionFactory.getCurrentSession()); // BSHD-11 Type[] types = classMetadata.getPropertyTypes(); String[] names = classMetadata.getPropertyNames(); @@ -300,25 +298,6 @@ } } - /* - * BSHD-11 - */ - private void addIdentifierRestriction(Criteria crit, Object entity, ClassMetadata classMetadata) - { - String identifierName = classMetadata.getIdentifierPropertyName(); - if (identifierName != null) - { - // TODO is this cast really necessary? Will it fail in future hibernate versions? - SessionImplementor si = (SessionImplementor) sessionFactory.getCurrentSession(); - - Object idValue = classMetadata.getIdentifier(entity, si); - if (idValue != null) // TODO should we use property selectors instead? - { - crit.add(Restrictions.idEq(idValue)); - } - } - } - private Example example(Object entity, String associationPath) { Example ex = Example.create(entity); Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java 2012-06-29 16:59:49 UTC (rev 4086) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java 2012-07-02 12:44:01 UTC (rev 4087) @@ -30,6 +30,11 @@ import java.util.Collection; import org.apache.commons.lang3.StringUtils; +import org.hibernate.Criteria; +import org.hibernate.Session; +import org.hibernate.criterion.Restrictions; +import org.hibernate.engine.SessionImplementor; +import org.hibernate.metadata.ClassMetadata; /** @@ -144,4 +149,28 @@ return propertyNames.length > 0 ? StringUtils.join(propertyNames, '.') : StringUtils.EMPTY; } + /** + * adds the identifier restriction to the input criteria, if required + * @param crit the criteria + * @param entity the entity to use as example + * @param classMetadata the class metadata to use + * @param ses the current session + * @see BSHD-11 + */ + public static void addIdentifierRestriction(Criteria crit, Object entity, ClassMetadata classMetadata, Session ses) + { + String identifierName = classMetadata.getIdentifierPropertyName(); + if (identifierName != null) + { + // TODO is this cast really necessary? Will it fail in future hibernate versions? + SessionImplementor si = (SessionImplementor) ses; + + Object idValue = classMetadata.getIdentifier(entity, si); + if (idValue != null) // TODO should we use property selectors instead? + { + crit.add(Restrictions.idEq(idValue)); + } + } + } + } Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2012-06-29 16:59:49 UTC (rev 4086) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2012-07-02 12:44:01 UTC (rev 4087) @@ -116,6 +116,9 @@ Map<String, FilterMetadata> currFilterMetadata = getFilterMetadata(path); crit.add(example(entity, currFilterMetadata.keySet())); ClassMetadata classMetadata = sessionFactory.getClassMetadata(Hibernate.getClass(entity)); + + ExampleTreeUtils.addIdentifierRestriction(crit, entity, classMetadata, sessionFactory.getCurrentSession()); // BSHD-11 + Type[] types = classMetadata.getPropertyTypes(); String[] names = classMetadata.getPropertyNames(); for (int i = 0; i < types.length; i++) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <gca...@us...> - 2013-02-14 15:33:01
|
Revision: 4192 http://openutils.svn.sourceforge.net/openutils/?rev=4192&view=rev Author: gcatania Date: 2013-02-14 15:32:53 +0000 (Thu, 14 Feb 2013) Log Message: ----------- BSHD-18 improve error message for missing hibernate entity mapping Modified Paths: -------------- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2013-02-04 16:09:42 UTC (rev 4191) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2013-02-14 15:32:53 UTC (rev 4192) @@ -257,10 +257,9 @@ { crit.add(c); } - ClassMetadata classMetadata = sessionFactory.getClassMetadata(Hibernate.getClass(entity)); + ClassMetadata classMetadata = ExampleTreeUtils.getClassMetadata(entity, sessionFactory); ExampleTreeUtils.addIdentifierRestriction(crit, entity, classMetadata, sessionFactory.getCurrentSession()); // BSHD-11 - Type[] types = classMetadata.getPropertyTypes(); String[] names = classMetadata.getPropertyNames(); for (int i = 0; i < types.length; i++) Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java 2013-02-04 16:09:42 UTC (rev 4191) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java 2013-02-14 15:32:53 UTC (rev 4192) @@ -31,7 +31,9 @@ import org.apache.commons.lang3.StringUtils; import org.hibernate.Criteria; +import org.hibernate.Hibernate; import org.hibernate.Session; +import org.hibernate.SessionFactory; import org.hibernate.criterion.Restrictions; import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.metadata.ClassMetadata; @@ -106,7 +108,8 @@ } if (size > 1) { - throw new IllegalArgumentException("More than one element in filter collection is unsupported."); + throw new IllegalArgumentException("More than one element in filter collection is unsupported.\n" + + coll); } } Class< ? extends Object> clazz = collectionValue.getClass(); @@ -128,6 +131,25 @@ } /** + * obtains the hibernate class metadata for the input entity + * @param entity the hibernate entity + * @param sessionFactory the session factory to retrieve metadata from + * @return the class metadata + * @throws IllegalStateException if no class metadata is configured for the input entity + */ + public static ClassMetadata getClassMetadata(Object entity, SessionFactory sessionFactory) + throws IllegalStateException + { + Class< ? > cl = Hibernate.getClass(entity); + ClassMetadata classMetadata = sessionFactory.getClassMetadata(cl); + if (classMetadata == null) + { + throw new IllegalStateException("No hibernate class metadata found for: " + cl); + } + return classMetadata; + } + + /** * @param strings an array of strings * @param s the string to append * @return a new array containing the input string array plus the input string at the end Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2013-02-04 16:09:42 UTC (rev 4191) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2013-02-14 15:32:53 UTC (rev 4192) @@ -111,10 +111,9 @@ String path = ExampleTreeUtils.getPath(walkedProperties); Map<String, FilterMetadata> currFilterMetadata = getFilterMetadata(path); crit.add(example(entity, currFilterMetadata.keySet())); - ClassMetadata classMetadata = sessionFactory.getClassMetadata(Hibernate.getClass(entity)); + ClassMetadata classMetadata = ExampleTreeUtils.getClassMetadata(entity, sessionFactory); ExampleTreeUtils.addIdentifierRestriction(crit, entity, classMetadata, sessionFactory.getCurrentSession()); // BSHD-11 - Type[] types = classMetadata.getPropertyTypes(); String[] names = classMetadata.getPropertyNames(); for (int i = 0; i < types.length; i++) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <gca...@us...> - 2013-03-12 09:07:06
|
Revision: 4201 http://openutils.svn.sourceforge.net/openutils/?rev=4201&view=rev Author: gcatania Date: 2013-03-12 09:06:55 +0000 (Tue, 12 Mar 2013) Log Message: ----------- BSHD-20 make identifier restrictions alternative to examples and filter metadata Modified Paths: -------------- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2013-03-12 08:58:39 UTC (rev 4200) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2013-03-12 09:06:55 UTC (rev 4201) @@ -251,6 +251,18 @@ private void createSubExamples(Criteria crit, Object entity, String[] walkedProperties) { + ClassMetadata classMetadata = ExampleTreeUtils.getClassMetadata(entity, sessionFactory); + boolean isIdSet = ExampleTreeUtils.addIdentifierRestriction( + crit, + entity, + classMetadata, + sessionFactory.getCurrentSession()); // BSHD-11 + if (isIdSet) + { + // BSHD-20 if the identifier is set on a property, do not impose further conditions + return; + } + String associationPath = ExampleTreeUtils.getPath(walkedProperties); crit.add(example(entity, associationPath)); for (Criterion c : getAdditionalConditions(associationPath)) @@ -258,8 +270,6 @@ crit.add(c); } - ClassMetadata classMetadata = ExampleTreeUtils.getClassMetadata(entity, sessionFactory); - ExampleTreeUtils.addIdentifierRestriction(crit, entity, classMetadata, sessionFactory.getCurrentSession()); // BSHD-11 Type[] types = classMetadata.getPropertyTypes(); String[] names = classMetadata.getPropertyNames(); for (int i = 0; i < types.length; i++) Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java 2013-03-12 08:58:39 UTC (rev 4200) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java 2013-03-12 09:06:55 UTC (rev 4201) @@ -177,9 +177,11 @@ * @param entity the entity to use as example * @param classMetadata the class metadata to use * @param ses the current session - * @see BSHD-11 + * @return true if the identifier restriction has been added, false otherwise + * @see BSHD-11, BSHD-20 */ - public static void addIdentifierRestriction(Criteria crit, Object entity, ClassMetadata classMetadata, Session ses) + public static boolean addIdentifierRestriction(Criteria crit, Object entity, ClassMetadata classMetadata, + Session ses) { String identifierName = classMetadata.getIdentifierPropertyName(); if (identifierName != null) @@ -191,8 +193,10 @@ if (idValue != null) // TODO should we use property selectors instead? { crit.add(Restrictions.idEq(idValue)); + return true; } } + return false; } } Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2013-03-12 08:58:39 UTC (rev 4200) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2013-03-12 09:06:55 UTC (rev 4201) @@ -108,12 +108,22 @@ private void createSubExamples(Criteria crit, Object entity, String[] walkedProperties) { + ClassMetadata classMetadata = ExampleTreeUtils.getClassMetadata(entity, sessionFactory); + boolean isIdSet = ExampleTreeUtils.addIdentifierRestriction( + crit, + entity, + classMetadata, + sessionFactory.getCurrentSession()); // BSHD-11 + if (isIdSet) + { + // BSHD-20 if the identifier is set on a property, do not impose further conditions + return; + } + String path = ExampleTreeUtils.getPath(walkedProperties); Map<String, FilterMetadata> currFilterMetadata = getFilterMetadata(path); crit.add(example(entity, currFilterMetadata.keySet())); - ClassMetadata classMetadata = ExampleTreeUtils.getClassMetadata(entity, sessionFactory); - ExampleTreeUtils.addIdentifierRestriction(crit, entity, classMetadata, sessionFactory.getCurrentSession()); // BSHD-11 Type[] types = classMetadata.getPropertyTypes(); String[] names = classMetadata.getPropertyNames(); for (int i = 0; i < types.length; i++) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <gca...@us...> - 2014-08-01 17:01:16
|
Revision: 4585 http://openutils.svn.sourceforge.net/openutils/?rev=4585&view=rev Author: gcatania Date: 2014-08-01 17:01:13 +0000 (Fri, 01 Aug 2014) Log Message: ----------- BSHD-25 improve error reporting in getValueFromCollection() Modified Paths: -------------- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2014-07-08 11:15:38 UTC (rev 4584) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2014-08-01 17:01:13 UTC (rev 4585) @@ -306,7 +306,7 @@ Object propertyValue = classMetadata.getPropertyValue(entity, propertyName); if (propertyType.isCollectionType()) { - propertyValue = ExampleTreeUtils.getValueFromCollection(propertyValue); + propertyValue = ExampleTreeUtils.getValueFromCollection(propertyName, propertyValue); } if (propertyValue == null) { @@ -365,7 +365,7 @@ * workaround to {@link Example} not exposing internal property selectors * @author gcatania */ -@SuppressWarnings({"serial", "static-method"}) +@SuppressWarnings({"serial", "static-method" }) class ExampleTreePropertySelectorSupport { @@ -378,6 +378,7 @@ static final class AllPropertySelector implements PropertySelector { + @Override public boolean include(Object object, String propertyName, Type type) { return true; @@ -392,6 +393,7 @@ static final class NotNullPropertySelector implements PropertySelector { + @Override public boolean include(Object object, String propertyName, Type type) { return object != null; @@ -406,6 +408,7 @@ static final class NotNullOrZeroPropertySelector implements PropertySelector { + @Override public boolean include(Object object, String propertyName, Type type) { return object != null && (!(object instanceof Number) || ((Number) object).longValue() != 0); Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java 2014-07-08 11:15:38 UTC (rev 4584) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTreeUtils.java 2014-08-01 17:01:13 UTC (rev 4585) @@ -25,6 +25,7 @@ package it.openutils.hibernate.example; import java.lang.reflect.Array; +import java.text.MessageFormat; import java.util.Arrays; import java.util.Collection; @@ -87,13 +88,15 @@ } /** - * retrieves a value from a collection + * retrieves a value from a collection property + * @param propertyName the property name (will be reported in the exception) * @param collectionValue the collection * @return a value * @see http://opensource2.atlassian.com/projects/hibernate/browse/HHH-879 * @throws IllegalArgumentException if the input collection contains more than one value */ - public static Object getValueFromCollection(Object collectionValue) throws IllegalArgumentException + public static Object getValueFromCollection(String propertyName, Object collectionValue) + throws IllegalArgumentException { if (collectionValue != null) { @@ -107,8 +110,10 @@ } if (size > 1) { - throw new IllegalArgumentException("More than one element in filter collection is unsupported.\n" - + coll); + throw new IllegalArgumentException(MessageFormat.format( + "More than one element in filter collection is unsupported.\nproperty: {0}, value: {1}", + propertyName, + coll)); } } Class< ? extends Object> clazz = collectionValue.getClass(); @@ -121,7 +126,13 @@ } if (length > 1) { - throw new IllegalArgumentException("More than one element in filter array is unsupported."); + throw new IllegalArgumentException( + MessageFormat + .format( + "More than one element in filter array is unsupported.\nproperty: {0}, value: {1} - length: {2}", + propertyName, + collectionValue, + length)); } } // TODO other cases? Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2014-07-08 11:15:38 UTC (rev 4584) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2014-08-01 17:01:13 UTC (rev 4585) @@ -141,7 +141,7 @@ if (propertyType.isCollectionType()) { - propertyValue = ExampleTreeUtils.getValueFromCollection(propertyValue); + propertyValue = ExampleTreeUtils.getValueFromCollection(propertyName, propertyValue); } if (propertyValue == null) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |