|
From: <gca...@us...> - 2012-05-20 13:12:33
|
Revision: 4037
http://openutils.svn.sourceforge.net/openutils/?rev=4037&view=rev
Author: gcatania
Date: 2012-05-20 13:12:26 +0000 (Sun, 20 May 2012)
Log Message:
-----------
BSHD-2 code cleanup, added find properties support
Modified Paths:
--------------
trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAO.java
trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java
trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java
trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadata.java
trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java
trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/test/HibernateDAOPersistenceTest.java
Modified: trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAO.java
===================================================================
--- trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAO.java 2012-05-15 19:48:16 UTC (rev 4036)
+++ trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAO.java 2012-05-20 13:12:26 UTC (rev 4037)
@@ -164,13 +164,43 @@
List<T> findFiltered(ExampleTree exampleTree, int maxResults, int page, Order... orders);
/**
+ * Retrieve a set of properties from the entities returned by
+ * {@link #findFiltered(Object, Order[], Map, int, int, List)}
+ * @param filter an instance of this DAO's entity class to be used as filter
+ * @param maxResults the maximum number of results to be fetched
+ * @param page the zero-based page number to use when displaying paginated results (the first entity returned is the
+ * one at position <code>maxResults * page</code> in the complete list of results), or <code>0</code> for no
+ * pagination
+ * @param properties the names of the properties to return
+ * @param orders the orders to apply with respect to entity class properties
+ * @return a list of distinct entity instances (never null)
+ */
+ List<Object[]> findFilteredProperties(T filter, int maxResults, int page, List<String> properties, Order... orders);
+
+ /**
+ * Retrieve a set of properties from the entities returned by
+ * {@link #findFiltered(Object, Order[], Map, int, int, List)}
+ * @param exampleTree the example tree criterion to match
+ * @param maxResults the maximum number of results to be fetched
+ * @param page the zero-based page number to use when displaying paginated results (the first entity returned is the
+ * one at position <code>maxResults * page</code> in the complete list of results), or <code>0</code> for no
+ * pagination
+ * @param properties the names of the properties to return
+ * @param orders the orders to apply with respect to entity class properties
+ * @return a list of distinct entity instances (never null)
+ */
+ List<Object[]> findFilteredProperties(ExampleTree exampleTree, int maxResults, int page, List<String> properties,
+ Order... orders);
+
+ /**
* Retrieve the entities handled by this DAO whose property values match, via <code>equals()</code> or via a
* specified <code>FilterMetadata</code> object, <code>filter</code>'s non-null property values.
* @param filter an instance of this DAO's entity class to be used as filter
* @param metadata a map that matches names of entity class properties to <code>FilterMetadata</code> modifiers,
* that will be used for comparing values of the corresponding property
* @return a list of distinct entity instances (never null)
- * @deprecated use of {@link FilterMetadata} has been deprecated
+ * @deprecated {@link FilterMetadata} has been deprecated in favor of {@link ExampleTree#add(String, Criterion)} and
+ * {@link ExampleTree#overridePropertyFilter(String, String, Criterion)}
*/
@Deprecated
List<T> findFiltered(T filter, Map<String, ? extends FilterMetadata> metadata);
@@ -186,7 +216,8 @@
* one at position <code>maxResults * page</code> in the complete list of results), or <code>0</code> for no
* pagination
* @return a list of distinct entity instances (never null)
- * @deprecated use of {@link FilterMetadata} has been deprecated
+ * @deprecated {@link FilterMetadata} has been deprecated in favor of {@link ExampleTree#add(String, Criterion)} and
+ * {@link ExampleTree#overridePropertyFilter(String, String, Criterion)}
*/
@Deprecated
List<T> findFiltered(T filter, Map<String, ? extends FilterMetadata> metadata, int maxResults, int page);
@@ -204,7 +235,8 @@
* pagination
* @param orders the orders to apply with respect to entity class properties
* @return a list of distinct entity instances (never null)
- * @deprecated use of {@link FilterMetadata} has been deprecated
+ * @deprecated {@link FilterMetadata} has been deprecated in favor of {@link ExampleTree#add(String, Criterion)} and
+ * {@link ExampleTree#overridePropertyFilter(String, String, Criterion)}
*/
@Deprecated
List<T> findFiltered(T filter, Map<String, ? extends FilterMetadata> metadata, int maxResults, int page,
@@ -224,33 +256,14 @@
* @param criteria a list of additional Hibernate criteria
* @param orders the orders to apply with respect to entity class properties
* @return a list of distinct entity instances (never null)
- * @deprecated use of {@link FilterMetadata} has been deprecated
+ * @deprecated {@link FilterMetadata} has been deprecated in favor of {@link ExampleTree#add(String, Criterion)} and
+ * {@link ExampleTree#overridePropertyFilter(String, String, Criterion)}
*/
@Deprecated
List<T> findFiltered(T filter, Map<String, ? extends FilterMetadata> metadata, int maxResults, int page,
List< ? extends Criterion> criteria, Order... orders);
/**
- * Retrieve a set of properties from the entities returned by
- * {@link #findFiltered(Object, Order[], Map, int, int, List)}
- * @param filter an instance of this DAO's entity class to be used as filter
- * @param metadata a map that matches names of entity class properties to <code>FilterMetadata</code> modifiers,
- * that will be used for comparing values of the corresponding property
- * @param maxResults the maximum number of results to be fetched
- * @param page the zero-based page number to use when displaying paginated results (the first entity returned is the
- * one at position <code>maxResults * page</code> in the complete list of results), or <code>0</code> for no
- * pagination
- * @param criteria a list of additional Hibernate criteria
- * @param properties the names of the properties to return
- * @param orders the orders to apply with respect to entity class properties
- * @return a list of distinct entity instances (never null)
- * @deprecated use of {@link FilterMetadata} has been deprecated
- */
- @Deprecated
- List< ? > findFilteredProperties(T filter, Map<String, ? extends FilterMetadata> metadata, int maxResults,
- int page, List< ? extends Criterion> criteria, List<String> properties, Order... orders);
-
- /**
* Retrieve the first entity instance that matches the input <code>filter</code>, if existing.
* @param filter an instance of this DAO's entity class to be used as filter
* @return the first matching instance of the entity class managed by this DAO, or <code>null</code> if none found
@@ -403,7 +416,8 @@
* one at position <code>maxResults * page</code> in the complete list of results), or <code>0</code> for no
* pagination
* @return a list of distinct entity instances (never null)
- * @deprecated use of {@link FilterMetadata} has been deprecated
+ * @deprecated {@link FilterMetadata} has been deprecated in favor of {@link ExampleTree#add(String, Criterion)} and
+ * {@link ExampleTree#overridePropertyFilter(String, String, Criterion)}
*/
@Deprecated
List<T> findFiltered(T filter, Order[] orders, Map<String, ? extends FilterMetadata> metadata, int maxResults,
@@ -423,7 +437,8 @@
* pagination
* @param criteria a list of additional Hibernate criteria
* @return a list of distinct entity instances (never null)
- * @deprecated use of {@link FilterMetadata} has been deprecated
+ * @deprecated {@link FilterMetadata} has been deprecated in favor of {@link ExampleTree#add(String, Criterion)} and
+ * {@link ExampleTree#overridePropertyFilter(String, String, Criterion)}
*/
@Deprecated
List<T> findFiltered(T filter, Order[] orders, Map<String, ? extends FilterMetadata> metadata, int maxResults,
@@ -443,10 +458,11 @@
* @param criteria a list of additional Hibernate criteria
* @param properties the names of the properties to return
* @return a list of distinct entity instances (never null)
- * @deprecated use of {@link FilterMetadata} has been deprecated
+ * @deprecated {@link FilterMetadata} has been deprecated in favor of {@link ExampleTree#add(String, Criterion)} and
+ * {@link ExampleTree#overridePropertyFilter(String, String, Criterion)}
*/
@Deprecated
- List< ? > findFilteredProperties(T filter, Order[] orders, Map<String, ? extends FilterMetadata> metadata,
+ List<Object[]> findFilteredProperties(T filter, Order[] orders, Map<String, ? extends FilterMetadata> metadata,
int maxResults, int page, List< ? extends Criterion> criteria, List<String> properties);
}
Modified: trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java
===================================================================
--- trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java 2012-05-15 19:48:16 UTC (rev 4036)
+++ trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java 2012-05-20 13:12:26 UTC (rev 4037)
@@ -31,7 +31,6 @@
import java.io.Serializable;
import java.sql.SQLException;
-import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@@ -43,7 +42,6 @@
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
-import org.hibernate.ObjectNotFoundException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.CriteriaSpecification;
@@ -128,26 +126,8 @@
/**
* {@inheritDoc}
*/
- public List<T> find(List< ? extends Criterion> criteria, Order... orders)
+ public List<T> find(final List< ? extends Criterion> criteria, final Order... orders)
{
- final List< ? extends Criterion> actualCriteria;
- if (criteria != null)
- {
- actualCriteria = criteria;
- }
- else
- {
- actualCriteria = Collections.<Criterion> emptyList();
- }
- final List<Order> actualOrders;
- if (orders != null)
- {
- actualOrders = Arrays.asList(orders);
- }
- else
- {
- actualOrders = Collections.<Order> emptyList();
- }
return getHibernateTemplate().execute(new HibernateCallback<List<T>>()
{
@@ -155,13 +135,19 @@
public List<T> doInHibernate(final Session ses) throws HibernateException
{
Criteria crit = ses.createCriteria(getReferenceClass());
- for (Criterion c : actualCriteria)
+ if (criteria != null)
{
- crit.add(c);
+ for (Criterion c : criteria)
+ {
+ crit.add(c);
+ }
}
- for (Order o : actualOrders)
+ if (orders != null)
{
- crit.addOrder(o);
+ for (Order o : orders)
+ {
+ crit.addOrder(o);
+ }
}
return crit.list();
}
@@ -251,7 +237,8 @@
}
/**
- * @deprecated use of {@link FilterMetadata} has been deprecated {@inheritDoc}
+ * @deprecated {@link FilterMetadata} has been deprecated in favor of {@link ExampleTree#add(String, Criterion)} and
+ * {@link ExampleTree#overridePropertyFilter(String, String, Criterion)} {@inheritDoc}
*/
@Deprecated
public List<T> findFiltered(T filter, Map<String, ? extends FilterMetadata> metadata)
@@ -266,7 +253,8 @@
}
/**
- * @deprecated use of {@link FilterMetadata} has been deprecated {@inheritDoc}
+ * @deprecated {@link FilterMetadata} has been deprecated in favor of {@link ExampleTree#add(String, Criterion)} and
+ * {@link ExampleTree#overridePropertyFilter(String, String, Criterion)} {@inheritDoc}
*/
@Deprecated
public List<T> findFiltered(T filter, Map<String, ? extends FilterMetadata> metadata, int maxResults, int page)
@@ -337,19 +325,12 @@
*/
public T loadIfAvailable(K key)
{
- try
+ T result = getHibernateTemplate().get(getReferenceClass(), key);
+ if (result != null)
{
- T result = getHibernateTemplate().get(getReferenceClass(), key);
- if (result != null)
- {
- Hibernate.initialize(result);
- return result;
- }
+ Hibernate.initialize(result);
+ return result;
}
- catch (ObjectNotFoundException e)
- {
- // thrown by HibernateTemplate#get() if the object does not exist
- }
return null;
}
@@ -449,12 +430,32 @@
*/
public List<T> findFiltered(ExampleTree exampleTree, int maxResults, int page, Order... orders)
{
- return getHibernateTemplate().execute(new ExampleTreeCallback(exampleTree, orders, maxResults, page));
+ return getHibernateTemplate().execute(new ExampleTreeCallback<T>(exampleTree, maxResults, page, orders));
}
/**
- * @deprecated use of {@link FilterMetadata} has been deprecated {@inheritDoc}
+ * {@inheritDoc}
*/
+ public List<Object[]> findFilteredProperties(T filter, int maxResults, int page, List<String> properties,
+ Order... orders)
+ {
+ return getThis().findFilteredProperties(new ExampleTree(filter), maxResults, page, properties, orders);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public List<Object[]> findFilteredProperties(ExampleTree exampleTree, int maxResults, int page,
+ List<String> properties, Order... orders)
+ {
+ return getHibernateTemplate().execute(
+ new ExampleTreePropertiesCallback(exampleTree, maxResults, page, properties, orders));
+ }
+
+ /**
+ * @deprecated {@link FilterMetadata} has been deprecated in favor of {@link ExampleTree#add(String, Criterion)} and
+ * {@link ExampleTree#overridePropertyFilter(String, String, Criterion)} {@inheritDoc}
+ */
@Deprecated
public List<T> findFiltered(T filter, Order[] customOrder, Map<String, ? extends FilterMetadata> metadata,
int maxResults, int page)
@@ -469,7 +470,8 @@
}
/**
- * @deprecated use of {@link FilterMetadata} has been deprecated {@inheritDoc}
+ * @deprecated {@link FilterMetadata} has been deprecated in favor of {@link ExampleTree#add(String, Criterion)} and
+ * {@link ExampleTree#overridePropertyFilter(String, String, Criterion)} {@inheritDoc}
*/
@Deprecated
public List<T> findFiltered(T filter, Map<String, ? extends FilterMetadata> metadata, int maxResults, int page,
@@ -479,47 +481,20 @@
}
/**
- * @deprecated use of {@link FilterMetadata} has been deprecated {@inheritDoc}
+ * @deprecated {@link FilterMetadata} has been deprecated in favor of {@link ExampleTree#add(String, Criterion)} and
+ * {@link ExampleTree#overridePropertyFilter(String, String, Criterion)} {@inheritDoc}
*/
@Deprecated
public List<T> findFiltered(T filter, Map<String, ? extends FilterMetadata> metadata, int maxResults, int page,
List< ? extends Criterion> criteria, Order... orders)
{
- HibernateCallback<List<T>> callback;
- if (MapUtils.isNotEmpty(metadata))
- {
- // backwards compatibility
- callback = new HibernateCallbackForExecution(
- filter,
- page,
- maxResults,
- metadata,
- orders,
- criteria,
- Collections.<String> emptyList());
- }
- else
- {
- ExampleTree exampleTree = new ExampleTree(filter);
- appendToRoot(exampleTree, criteria);
- callback = new ExampleTreeCallback(exampleTree, orders, maxResults, page);
- }
- return getHibernateTemplate().execute(callback);
- }
-
- /**
- * @deprecated use of {@link FilterMetadata} has been deprecated {@inheritDoc}
- */
- @Deprecated
- public List< ? > findFilteredProperties(T filter, Map<String, ? extends FilterMetadata> metadata, int maxResults,
- int page, List< ? extends Criterion> criteria, List<String> properties, Order... orders)
- {
return getHibernateTemplate().execute(
- new HibernateCallbackForExecution(filter, page, maxResults, metadata, orders, criteria, properties));
+ new LegacySupportCallback<T>(filter, maxResults, page, metadata, criteria, orders));
}
/**
- * @deprecated use of {@link FilterMetadata} has been deprecated {@inheritDoc}
+ * @deprecated {@link FilterMetadata} has been deprecated in favor of {@link ExampleTree#add(String, Criterion)} and
+ * {@link ExampleTree#overridePropertyFilter(String, String, Criterion)} {@inheritDoc}
*/
@Deprecated
public List<T> findFiltered(T filter, Order[] orders, Map<String, ? extends FilterMetadata> metadata,
@@ -529,14 +504,16 @@
}
/**
- * @deprecated use of {@link FilterMetadata} has been deprecated {@inheritDoc}
+ * @deprecated {@link FilterMetadata} has been deprecated in favor of {@link ExampleTree#add(String, Criterion)} and
+ * {@link ExampleTree#overridePropertyFilter(String, String, Criterion)} {@inheritDoc}
*/
@Deprecated
- public List< ? > findFilteredProperties(T filter, Order[] orders, Map<String, ? extends FilterMetadata> metadata,
- int maxResults, int page, List< ? extends Criterion> criteria, List<String> properties)
+ public List<Object[]> findFilteredProperties(T filter, Order[] orders,
+ Map<String, ? extends FilterMetadata> metadata, int maxResults, int page, List< ? extends Criterion> criteria,
+ List<String> properties)
{
return getHibernateTemplate().execute(
- new HibernateCallbackForExecution(filter, page, maxResults, metadata, orders, criteria, properties));
+ new LegacySupportPropertiesCallback(filter, maxResults, page, metadata, criteria, properties, orders));
}
/**
@@ -578,12 +555,13 @@
* @param maxResults max number of results
* @return Query
*/
- protected List< ? > findByNamedQuery(final String name, final Serializable[] params, final Integer maxResults)
+ protected List<Object> findByNamedQuery(final String name, final Serializable[] params, final Integer maxResults)
{
- return getHibernateTemplate().execute(new HibernateCallback<List< ? >>()
+ return getHibernateTemplate().execute(new HibernateCallback<List<Object>>()
{
- public List< ? > doInHibernate(final Session ses) throws HibernateException
+ @SuppressWarnings("unchecked")
+ public List<Object> doInHibernate(final Session ses) throws HibernateException
{
Query q = ses.getNamedQuery(name);
if (maxResults != null)
@@ -609,12 +587,13 @@
* @param maxResults max number of results
* @return Query
*/
- protected List< ? > findByNamedQuery(final String name, final Map<String, ? > params, final Integer maxResults)
+ protected List<Object> findByNamedQuery(final String name, final Map<String, ? > params, final Integer maxResults)
{
- return getHibernateTemplate().execute(new HibernateCallback<List< ? >>()
+ return getHibernateTemplate().execute(new HibernateCallback<List<Object>>()
{
- public List< ? > doInHibernate(final Session ses) throws HibernateException
+ @SuppressWarnings("unchecked")
+ public List<Object> doInHibernate(final Session ses) throws HibernateException
{
Query q = ses.getNamedQuery(name);
if (maxResults != null)
@@ -643,7 +622,7 @@
* @deprecated use the better named <code>findByNamedQuery</code> method
*/
@Deprecated
- protected List< ? > getNamedQuery(String name, Serializable[] params, int maxResults)
+ protected List<Object> getNamedQuery(String name, Serializable[] params, int maxResults)
{
return findByNamedQuery(name, params, maxResults > 0 ? maxResults : Integer.MAX_VALUE);
}
@@ -657,7 +636,7 @@
* @deprecated use the better named <code>findByNamedQuery</code> method
*/
@Deprecated
- protected List< ? > getNamedQuery(String name, Map<String, ? > params, int maxResults)
+ protected List<Object> getNamedQuery(String name, Map<String, ? > params, int maxResults)
{
return findByNamedQuery(name, params, maxResults > 0 ? maxResults : Integer.MAX_VALUE);
}
@@ -682,7 +661,10 @@
* Returns the default set of FilterMetadata that will be applied to any query. The default implementation doesn't
* set any default filter, subclasses may override this.
* @return map of property name - filter metadata
+ * @deprecated {@link FilterMetadata} has been deprecated in favor of {@link ExampleTree#add(String, Criterion)} and
+ * {@link ExampleTree#overridePropertyFilter(String, String, Criterion)}
*/
+ @Deprecated
protected Map<String, ? extends FilterMetadata> getDefaultFilterMetadata()
{
return Collections.emptyMap();
@@ -748,121 +730,80 @@
/**
* @author gcatania
*/
- private final class ExampleTreeCallback implements HibernateCallback<List<T>>
+ @SuppressWarnings("deprecation")
+ private class LegacySupportCallback<R> extends ExampleTreeCallback<R>
{
- private final Order[] orders;
+ private final T rootEntity;
- private final int page;
+ private final Map<String, ? extends FilterMetadata> filterMetadata;
- private final int maxResults;
+ private final List< ? extends Criterion> additionalCriteria;
- private final ExampleTree exampleTree;
-
- private ExampleTreeCallback(ExampleTree exampleTree, Order[] orders, int maxResults, int page)
+ private LegacySupportCallback(
+ T rootEntity,
+ int maxResults,
+ int page,
+ Map<String, ? extends FilterMetadata> metadata,
+ List< ? extends Criterion> additionalCriteria,
+ Order... orders)
{
- this.exampleTree = exampleTree;
- this.orders = orders;
- this.page = page;
- this.maxResults = maxResults;
+ super(new ExampleTree(rootEntity), maxResults, page, orders);
+ this.rootEntity = rootEntity;
+ this.filterMetadata = metadata;
+ this.additionalCriteria = additionalCriteria;
}
- @SuppressWarnings("unchecked")
- public List<T> doInHibernate(Session session) throws HibernateException, SQLException
+ @Override
+ protected Criteria createCriteria(Session session)
{
- Criteria crit = exampleTree.create(session);
-
- // backwards compatibility
- Map<String, ? extends FilterMetadata> filterMetadata = getDefaultFilterMetadata();
+ Criteria crit = super.createCriteria(session);
if (MapUtils.isNotEmpty(filterMetadata))
{
- new FilterMetadataSupport(exampleTree.getRootEntity(), filterMetadata).appendTo(crit, session);
+ new FilterMetadataSupport(rootEntity, filterMetadata).appendTo(crit, session);
}
-
- crit.setMaxResults(maxResults);
- crit.setFirstResult(maxResults * page);
- if (orders != null)
+ if (additionalCriteria != null)
{
- for (Order o : orders)
+ for (Criterion c : additionalCriteria)
{
- crit.addOrder(o);
+ crit.add(c);
}
}
- return crit.list();
+ return crit;
}
+
}
/**
- * @deprecated callback implementation that uses EnhancedExample, deprecated in favor of ExampleTreeCallback
- * @author carone
- * @version $Id$
+ * @author gcatania
*/
- @Deprecated
- private final class HibernateCallbackForExecution implements HibernateCallback<List<T>>
+ @SuppressWarnings("deprecation")
+ private final class LegacySupportPropertiesCallback extends LegacySupportCallback<Object[]>
{
- private T filter;
+ private final List<String> properties;
- private int page;
-
- private int maxResults;
-
- private Map<String, ? extends FilterMetadata> metadata;
-
- private List<String> properties;
-
- private Order[] orderProperties;
-
- private List< ? extends Criterion> additionalCriteria;
-
- private HibernateCallbackForExecution(
- T filter,
+ private LegacySupportPropertiesCallback(
+ T rootEntity,
+ int maxResults,
int page,
- int maxResults,
Map<String, ? extends FilterMetadata> metadata,
- Order[] orderProperties,
List< ? extends Criterion> additionalCriteria,
- List<String> properties)
+ List<String> properties,
+ Order... orders)
{
- this.filter = filter;
- this.page = page;
- this.maxResults = maxResults;
- this.metadata = metadata;
- this.orderProperties = orderProperties;
- this.additionalCriteria = additionalCriteria;
+ super(rootEntity, maxResults, page, metadata, additionalCriteria, orders);
this.properties = properties;
+
}
- @SuppressWarnings("unchecked")
- public List<T> doInHibernate(Session ses) throws HibernateException
+ @Override
+ protected Criteria createCriteria(Session session)
{
- // Criteria crit = ses.createCriteria(filter.getClass());
- Criteria crit = new FilterMetadataSupport(filter, metadata).create(ses);
- crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
- crit.setMaxResults(maxResults);
- crit.setFirstResult(maxResults * page);
-
- if (orderProperties != null)
- {
- for (Order order : orderProperties)
- {
- if (order != null)
- {
- crit.addOrder(order);
- }
- }
- }
- if (!CollectionUtils.isEmpty(additionalCriteria))
- {
- for (Criterion criterion : additionalCriteria)
- {
- crit.add(criterion);
- }
- }
+ Criteria crit = super.createCriteria(session);
if (!CollectionUtils.isEmpty(properties))
{
ProjectionList projectionList = Projections.projectionList();
-
for (String property : properties)
{
projectionList.add(Property.forName(property));
@@ -870,8 +811,93 @@
crit.setProjection(projectionList);
}
- return crit.list();
+ return crit;
}
}
+}
-}
\ No newline at end of file
+
+/**
+ * @author gcatania
+ * @param R the result class
+ */
+class ExampleTreeCallback<R> implements HibernateCallback<List<R>>
+{
+
+ private final Order[] orders;
+
+ private final int maxResults;
+
+ private final int page;
+
+ private final ExampleTree exampleTree;
+
+ protected ExampleTreeCallback(ExampleTree exampleTree, int maxResults, int page, Order... orders)
+ {
+ this.exampleTree = exampleTree;
+ this.maxResults = maxResults;
+ this.page = page;
+ this.orders = orders;
+ }
+
+ /**
+ * internal method that creates the query criteria. Subclasses may override but should call {@code super()}
+ * @param session the hibernate session
+ * @return the hibernate criteria
+ */
+ protected Criteria createCriteria(Session session)
+ {
+ Criteria crit = exampleTree.create(session);
+
+ crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
+ crit.setMaxResults(maxResults);
+ crit.setFirstResult(maxResults * page);
+ if (orders != null)
+ {
+ for (Order o : orders)
+ {
+ crit.addOrder(o);
+ }
+ }
+
+ return crit;
+ }
+
+ @SuppressWarnings("unchecked")
+ public List<R> doInHibernate(Session session) throws HibernateException, SQLException
+ {
+ return createCriteria(session).list();
+ }
+}
+
+
+class ExampleTreePropertiesCallback extends ExampleTreeCallback<Object[]>
+{
+
+ private final List<String> properties;
+
+ protected ExampleTreePropertiesCallback(ExampleTree exampleTree, int maxResults, int page,
+
+ List<String> properties, Order... orders)
+ {
+ super(exampleTree, maxResults, page, orders);
+ this.properties = properties;
+ }
+
+ @Override
+ protected Criteria createCriteria(Session session)
+ {
+ Criteria crit = super.createCriteria(session);
+ if (!CollectionUtils.isEmpty(properties))
+ {
+ ProjectionList projectionList = Projections.projectionList();
+ for (String property : properties)
+ {
+ projectionList.add(Property.forName(property));
+ }
+
+ crit.setProjection(projectionList);
+ }
+ return crit;
+ }
+}
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-05-15 19:48:16 UTC (rev 4036)
+++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/ExampleTree.java 2012-05-20 13:12:26 UTC (rev 4037)
@@ -74,9 +74,9 @@
private boolean isIgnoreCaseEnabled;
- private Map<String, Set<String>> excludedProperties = new HashMap<String, Set<String>>();
+ private final Map<String, Set<String>> excludedProperties = new HashMap<String, Set<String>>();
- private Map<String, List<Criterion>> additionalConditions = new HashMap<String, List<Criterion>>();
+ private final Map<String, List<Criterion>> additionalConditions = new HashMap<String, List<Criterion>>();
/**
* builds an instance of {@code ExampleTree} associated with the input entity
@@ -246,14 +246,6 @@
return this;
}
- /**
- * @return the rootEntity
- */
- public Object getRootEntity()
- {
- return rootEntity;
- }
-
private class ExampleTreeWalker implements Serializable
{
@@ -261,7 +253,7 @@
private final SessionFactory sessionFactory;
- private EntityMode entityMode;
+ private final EntityMode entityMode;
public ExampleTreeWalker(Session session)
{
Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadata.java
===================================================================
--- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadata.java 2012-05-15 19:48:16 UTC (rev 4036)
+++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadata.java 2012-05-20 13:12:26 UTC (rev 4037)
@@ -25,9 +25,8 @@
package it.openutils.hibernate.example;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
+import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
@@ -36,9 +35,12 @@
/**
* FilterMetadata can be used to alter how properties are handled.
+ * @deprecated in favor of {@link ExampleTree#add(String, Criterion)} and
+ * {@link ExampleTree#overridePropertyFilter(String, String, Criterion)}
* @author Fabrizio Giustina
* @version $Id: $
*/
+@Deprecated
public interface FilterMetadata
{
@@ -48,7 +50,7 @@
FilterMetadata LIKE = new FilterMetadata()
{
- private Logger log = LoggerFactory.getLogger(FilterMetadata.class);
+ private final Logger log = LoggerFactory.getLogger(FilterMetadata.class);
/**
* {@inheritDoc}
@@ -70,7 +72,7 @@
FilterMetadata EQUAL = new FilterMetadata()
{
- private Log log = LogFactory.getLog(FilterMetadata.class);
+ private final Logger log = LoggerFactory.getLogger(FilterMetadata.class);
/**
* {@inheritDoc}
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-05-15 19:48:16 UTC (rev 4036)
+++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadataSupport.java 2012-05-20 13:12:26 UTC (rev 4037)
@@ -45,15 +45,16 @@
/**
- * legacy support for filter metadata.
+ * legacy support for {@link FilterMetadata}.
* @author gcatania
*/
+@SuppressWarnings("deprecation")
public class FilterMetadataSupport
{
private final Object entity;
- private Map<String, ? extends FilterMetadata> filterMetadata;
+ private final Map<String, ? extends FilterMetadata> filterMetadata;
/**
* builds an instance of {@code ExampleTree} associated with the input entity
@@ -97,7 +98,7 @@
private final SessionFactory sessionFactory;
- private EntityMode entityMode;
+ private final EntityMode entityMode;
public ExampleTreeWalker(Session session)
{
Modified: trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/test/HibernateDAOPersistenceTest.java
===================================================================
--- trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/test/HibernateDAOPersistenceTest.java 2012-05-15 19:48:16 UTC (rev 4036)
+++ trunk/openutils-bshd5/src/test/java/it/openutils/hibernate/test/HibernateDAOPersistenceTest.java 2012-05-20 13:12:26 UTC (rev 4037)
@@ -48,6 +48,7 @@
import java.util.List;
import org.hibernate.criterion.Example;
+import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
@@ -419,6 +420,35 @@
Assert.assertEquals(found.size(), 2);
}
+ @Test
+ public void testFindFilteredProperties()
+ {
+ personDAO.save(alice());
+ personDAO.save(bob());
+ personDAO.save(chuck());
+
+ Address addressFilter = new Address();
+ addressFilter.setCity("Smalltown");
+ Person filter = new Person();
+ filter.setCurrentAddress(addressFilter);
+
+ List<Object[]> foundProperties = personDAO.findFilteredProperties(
+ filter,
+ Integer.MAX_VALUE,
+ 0,
+ Arrays.asList("name", "birthDate"),
+ Order.desc("name.givenName"));
+
+ Assert.assertEquals(foundProperties.size(), 2);
+
+ Object[] bobsProperties = foundProperties.get(0);
+ Assert.assertEquals(bobsProperties[0], bob().getName());
+ Assert.assertEquals(bobsProperties[1], bob().getBirthDate());
+ Object[] alicesProperties = foundProperties.get(1);
+ Assert.assertEquals(alicesProperties[0], alice().getName());
+ Assert.assertEquals(alicesProperties[1], alice().getBirthDate());
+
+ }
// @Test
// public void testExampleAssociations()
// {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|