From: <fg...@us...> - 2008-01-26 20:22:32
|
Revision: 551 http://openutils.svn.sourceforge.net/openutils/?rev=551&view=rev Author: fgiust Date: 2008-01-26 12:22:36 -0800 (Sat, 26 Jan 2008) Log Message: ----------- removed deprecated classes, add documentation Modified Paths: -------------- trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/EnhancedExample.java trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadata.java trunk/openutils-bshd5/src/site/apt/index.apt trunk/openutils-bshd5/src/site/site.xml Added Paths: ----------- trunk/openutils-bshd5/src/site/apt/configuration.apt Removed Paths: ------------- trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/MutableDateRange.java 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 2008-01-26 19:07:44 UTC (rev 550) +++ trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java 2008-01-26 20:22:36 UTC (rev 551) @@ -43,13 +43,17 @@ private boolean aopenabled; + /** + * Instantiates a new DAO instance + */ public HibernateDAOImpl() { super(); } /** - * @param referenceClass + * Sets the class of the persistent bean managed by this DAO + * @param referenceClass the class for the bean managed by this DAO */ @SuppressWarnings({"unchecked" }) public HibernateDAOImpl(Class referenceClass) Deleted: trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/MutableDateRange.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/MutableDateRange.java 2008-01-26 19:07:44 UTC (rev 550) +++ trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/MutableDateRange.java 2008-01-26 20:22:36 UTC (rev 551) @@ -1,75 +0,0 @@ -package it.openutils.dao.hibernate; - -import java.util.Date; - - -/** - * @author Fabrizio Giustina - * @version $Id: $ - * @deprecated use FilterMetadata - */ -@Deprecated -public class MutableDateRange extends Date -{ - - /** - * - */ - private static final long serialVersionUID = 1L; - - /** - * range start. - */ - private Date from; - - /** - * range end. - */ - private Date to; - - /** - * Getter for <code>from</code>. - * @return Returns the from. - */ - public Date getFrom() - { - return this.from; - } - - /** - * Setter for <code>from</code>. - * @param from The from to set. - */ - public void setFrom(Date from) - { - this.from = from; - } - - /** - * Getter for <code>to</code>. - * @return Returns the to. - */ - public Date getTo() - { - return this.to; - } - - /** - * Setter for <code>to</code>. - * @param to The to to set. - */ - public void setTo(Date to) - { - this.to = to; - } - - /** - * Returns <code>true</code> if at least one date is set. - * @return <code>true</code> if at least one date is set - */ - public boolean isSet() - { - return this.to != null || this.from != null; - } - -} Modified: trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/EnhancedExample.java =================================================================== --- trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/EnhancedExample.java 2008-01-26 19:07:44 UTC (rev 550) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/EnhancedExample.java 2008-01-26 20:22:36 UTC (rev 551) @@ -1,7 +1,5 @@ package it.openutils.hibernate.example; -import it.openutils.dao.hibernate.MutableDateRange; - import java.lang.reflect.InvocationTargetException; import java.math.BigDecimal; import java.sql.Timestamp; @@ -19,7 +17,6 @@ import org.apache.commons.lang.StringUtils; import org.hibernate.Criteria; import org.hibernate.HibernateException; -import org.hibernate.criterion.Restrictions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.dao.DataRetrievalFailureException; @@ -29,9 +26,14 @@ * @author Fabrizio Giustina * @version $Id: $ */ -public class EnhancedExample +public final class EnhancedExample { + /** + * Logger. + */ + private static Logger log = LoggerFactory.getLogger(EnhancedExample.class); + private Map<String, FilterMetadata> metadata; private EnhancedExample(Criteria crit, Object filter, Map<String, FilterMetadata> metadata) @@ -41,14 +43,10 @@ } /** - * Logger. - */ - private static Logger log = LoggerFactory.getLogger(EnhancedExample.class); - - /** * Fills a criteria object calling addCondition() for any non-null property or for any component in collections. * @param crit Criteria * @param filter javabean which will be analyzed for non-null properties + * @param metadata Map of property names - filter metadata * @throws HibernateException exception while building the criteria */ public static void create(Criteria crit, Object filter, Map<String, FilterMetadata> metadata) @@ -91,40 +89,17 @@ fmd.createFilter(crit, simplePropertyName, value); } - else if (value instanceof MutableDateRange) // @todo MutableDateRange should be removed, use a FilterMetadata - // for this scope - { - Date from = ((MutableDateRange) value).getFrom(); - Date to = ((MutableDateRange) value).getTo(); - if (from != null && to != null) - { - log.debug("crit.add(Restrictions.between({},{}, {})", new Object[]{propertyName, from, to }); - crit.add(Restrictions.between(propertyName, from, to)); - } - else if (from != null) - { - - log.debug("crit.add(Restrictions.ge({}, {})", propertyName, to); - - crit.add(Restrictions.ge(propertyName, from)); - } - else if (to != null) - { - log.debug("crit.add(Restrictions.le({}, {})", propertyName, to); - crit.add(Restrictions.le(propertyName, to)); - } - } else { if (containsSomething(value)) { // @todo handle multiple associations in lists? // see http://opensource2.atlassian.com/projects/hibernate/browse/HHH-879 - if ((value instanceof Set || value instanceof List) && !((Collection) value).isEmpty()) + if ((value instanceof Set || value instanceof List) && !((Collection< ? >) value).isEmpty()) { // collection: the new criteria has already been created, now we only nee to analize content - for (Object element : ((Collection< ? extends Object>) value)) + for (Object element : ((Collection< ? >) value)) { log.debug("crit.createCriteria({})", simplePropertyName); @@ -132,7 +107,7 @@ fillCriteria(propertyName, childrenCriteria, element); } } - else if ((value instanceof Map) && !((Map) value).isEmpty()) + else if ((value instanceof Map) && !((Map< ? , ? >) value).isEmpty()) { FilterMetadata fmd = metadata.get(propertyName); @@ -163,6 +138,7 @@ * @param bean javabean * @return <code>true</code> if the bean contains at least a valid property */ + @SuppressWarnings("unchecked") private boolean containsSomething(Object bean) { @@ -174,15 +150,11 @@ { return true; } - else if (bean instanceof MutableDateRange) - { - return ((MutableDateRange) bean).isSet(); - } if (bean instanceof Collection) { - Collection coll = ((Collection) bean); + Collection< ? > coll = ((Collection< ? >) bean); if (coll.isEmpty()) { return false; @@ -195,7 +167,7 @@ } else if (bean instanceof Map) { - Map coll = ((Map) bean); + Map< ? , ? > coll = ((Map< ? , ? >) bean); if (coll.isEmpty()) { return false; @@ -254,12 +226,13 @@ * @param filter javabean which will be analyzed for non-null properties * @throws HibernateException exception while building the criteria */ + @SuppressWarnings("unchecked") private void fillCriteria(String parentPropertyName, Criteria crit, Object filter) throws HibernateException { - if ((filter instanceof Set || filter instanceof List) && !((Collection) filter).isEmpty()) + if ((filter instanceof Set || filter instanceof List) && !((Collection< ? >) filter).isEmpty()) { // collection: the new criteria has already been created, now we only need to analize content - for (Object element : ((Collection) filter)) + for (Object element : ((Collection< ? >) filter)) { fillCriteria(parentPropertyName, crit, element); } 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 2008-01-26 19:07:44 UTC (rev 550) +++ trunk/openutils-bshd5/src/main/java/it/openutils/hibernate/example/FilterMetadata.java 2008-01-26 20:22:36 UTC (rev 551) @@ -10,19 +10,24 @@ /** + * FilterMetadata can be used to alter how properties are handled. * @author Fabrizio Giustina - * @version $Id$ + * @version $Id: $ */ public interface FilterMetadata { - void createFilter(Criteria criteria, String propertyName, Object propertyValue); - + /** + * A filter metadata that adds a LIKE condition. + */ FilterMetadata LIKE = new FilterMetadata() { private Logger log = LoggerFactory.getLogger(FilterMetadata.class); + /** + * {@inheritDoc} + */ public void createFilter(Criteria crit, String propertyName, Object propertyValue) { crit.add(Restrictions.ilike(propertyName, (String) propertyValue, MatchMode.ANYWHERE)); @@ -34,11 +39,17 @@ } }; + /** + * A filter metadata that adds an EQUAL condition. + */ FilterMetadata EQUAL = new FilterMetadata() { private Log log = LogFactory.getLog(FilterMetadata.class); + /** + * {@inheritDoc} + */ public void createFilter(Criteria crit, String propertyName, Object propertyValue) { if (log.isDebugEnabled()) @@ -49,4 +60,12 @@ crit.add(Restrictions.eq(propertyName, propertyValue)); } }; + + /** + * The createFilter method can alter an existing criteria adding restrictions. + * @param criteria the parent criteria + * @param propertyName current property name + * @param propertyValue property value + */ + void createFilter(Criteria criteria, String propertyName, Object propertyValue); } Added: trunk/openutils-bshd5/src/site/apt/configuration.apt =================================================================== --- trunk/openutils-bshd5/src/site/apt/configuration.apt (rev 0) +++ trunk/openutils-bshd5/src/site/apt/configuration.apt 2008-01-26 20:22:36 UTC (rev 551) @@ -0,0 +1,67 @@ + -------------------------- + O p e n u t i l s + -------------------------- + Fabrizio Giustina + -------------------------- + +Using annotations + + This is the preferred way of creating and configuring a new DAO using Spring 2.5 and annotations: + ++-----------------------------------------------+ + +public interface SampleDAO extends HibernateDAO<Sample, Integer> +{ + + @Repository(value = "sampleDAO") + public static class SampleDAOImpl extends HibernateDAOImpl<Sample, Integer> implements SampleDAO + { + + @Autowired + public SampleDAOImpl(SessionFactory factory) + { + setSessionFactory(factory); + setReferenceClass(Sample.class); + } + } +} + ++-----------------------------------------------+ + + Simple, isn't it? With those few lines you are: + + * Creating youd DAO interface, which allow you to manage <<<Sample>>> objects with a key of type <<<Integer>>> + + * Implementing your interface and setup your DAO + + * Registing you DAO in the Spring context using the <<<@Repository>>> annotation + + [] + + + +Using xml + + You need to write at least the DAO interface; the DAO implementation can be a subclass of HibernateDAOImpl or can + be created by Spring you you do not need to alter or add any method. + ++-----------------------------------------------+ + +public interface SampleDAO extends HibernateDAO<Sample, Integer> +{ + +} + <bean id="sampleDAO" class="org.springframework.aop.framework.ProxyFactoryBean"> + <property name="proxyInterfaces" value="com.myapp.SampleDAO"/> + <property name="target"> + <bean + class="it.openutils.dao.hibernate.HibernateDAOImpl"> + <property name="sessionFactory" ref="sessionFactory"/> + <property name="referenceClass" value="com.myapp.Sample"/> + </bean> + </property> + </bean> + ++-----------------------------------------------+ + + Property changes on: trunk/openutils-bshd5/src/site/apt/configuration.apt ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Author Date Id Revision Name: svn:eol-style + native Modified: trunk/openutils-bshd5/src/site/apt/index.apt =================================================================== --- trunk/openutils-bshd5/src/site/apt/index.apt 2008-01-26 19:07:44 UTC (rev 550) +++ trunk/openutils-bshd5/src/site/apt/index.apt 2008-01-26 20:22:36 UTC (rev 551) @@ -1,14 +1,22 @@ -------------------------- - O p e n u t i l s + O p e n u t i l s -------------------------- Fabrizio Giustina -------------------------- openutils-bshd5 - bshd5 stands for "base spring-hibernate DAO for java 5" + bshd5 means "base spring-hibernate DAO for java 5". + It provides a base generic DAO class that can be reused and that offer most of the needed methods (load, find, save, etc) + already implemented. + The base class also provides an enhanced version of hibernate "find by example" feature allowing associations in examples. + + See {{{configuration.html}configuration}} for how to setup your DAOs and look at {{{apidocs/index.html}javadocs}} for + more details. + + Released versions Check it at {{{http://www.mvnrepository.com/artifact/net.sourceforge.openutils/openutils-bshd5}http://www.mvnrepository.com/artifact/net.sourceforge.openutils/openutils-bshd5}} Modified: trunk/openutils-bshd5/src/site/site.xml =================================================================== --- trunk/openutils-bshd5/src/site/site.xml 2008-01-26 19:07:44 UTC (rev 550) +++ trunk/openutils-bshd5/src/site/site.xml 2008-01-26 20:22:36 UTC (rev 551) @@ -21,7 +21,8 @@ <item name="openutils-bshd5" href="http://openutils.sourceforge.net/openutils-bshd5" /> </breadcrumbs> <menu name="openutils-bshd5"> - <item name="Usage" href="index.html"></item> + <item name="Usage" href="index.html"/> + <item name="Configuration" href="configuration.html"/> </menu> <menu ref="modules" inherit="bottom" /> <menu ref="reports" inherit="bottom" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fg...@us...> - 2008-04-27 13:14:18
|
Revision: 772 http://openutils.svn.sourceforge.net/openutils/?rev=772&view=rev Author: fgiust Date: 2008-04-27 06:14:25 -0700 (Sun, 27 Apr 2008) Log Message: ----------- minor changes 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/site/changes/changes.xml 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 2008-04-27 12:47:09 UTC (rev 771) +++ trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAO.java 2008-04-27 13:14:25 UTC (rev 772) @@ -84,7 +84,7 @@ * @param obj Object */ void evict(T obj); - + /** * Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent * instance currently associated with the session, it will be loaded. Return the persistent instance. If the given @@ -141,9 +141,10 @@ */ List<T> findFiltered(final T filter, final Order[] customOrder, final Map<String, FilterMetadata> metadata, final int maxResults, final int page, List<Criterion> additionalCriteria); - + /** - * Return properties from all objects related to the implementation of this DAO filtered using properties of the provided instance. + * Return properties from all objects related to the implementation of this DAO filtered using properties of the + * provided instance. * @param filter an instance of the object with the properties you whish to filter on. * @param customOrder order criterias * @param metadata filter metadata @@ -152,11 +153,11 @@ * @param additionalCriteria additional criteria * @param properties properties to be returned * @return list of properties from all objects - */ - List<?> findFilteredProperties(final T filter, final Order[] customOrder, final Map<String, FilterMetadata> metadata, - final int maxResults,final int page, List<Criterion> additionalCriteria, List<String> properties); + */ + List< ? > findFilteredProperties(final T filter, final Order[] customOrder, + final Map<String, FilterMetadata> metadata, final int maxResults, final int page, + List<Criterion> additionalCriteria, List<String> properties); - /** * Return all objects related to the implementation of this DAO filtered using properties of the provided instance. * @param filter an instance of the object with the properties you whish to filter on. @@ -207,7 +208,7 @@ boolean delete(final K key); /** - * Load object matching the given key and return it. Lazy object will be initialized. + * Load object matching the given key and return it. Throw an exception if not found. * @param key serializable key * @return Object */ @@ -221,6 +222,13 @@ T loadIfAvailable(K key); /** + * Load object matching the given key and return it. Lazy object will be initialized. + * @param key serializable key + * @return Object + */ + T get(K key); + + /** * Used by the base DAO classes but here for your modification Either save() or update() the given instance, * depending upon the value of its identifier property. * @param obj Object 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 2008-04-27 12:47:09 UTC (rev 771) +++ trunk/openutils-bshd5/src/main/java/it/openutils/dao/hibernate/HibernateDAOImpl.java 2008-04-27 13:14:25 UTC (rev 772) @@ -26,7 +26,6 @@ import org.hibernate.type.Type; import org.springframework.aop.framework.AopContext; import org.springframework.orm.hibernate3.HibernateCallback; -import org.springframework.orm.hibernate3.HibernateObjectRetrievalFailureException; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; @@ -168,29 +167,32 @@ T result; try { - result = (T) getHibernateTemplate().load(getReferenceClass(), key); - Hibernate.initialize(result); + result = (T) getHibernateTemplate().get(getReferenceClass(), key); + if (result != null) + { + Hibernate.initialize(result); + } } catch (ObjectNotFoundException e) { // during lazy initialization return null; } - catch (HibernateObjectRetrievalFailureException e) - { - // during load - if (e.getCause() instanceof ObjectNotFoundException) - { - return null; - } - throw e; - } return result; } /** * {@inheritDoc} */ + @SuppressWarnings("unchecked") + public T get(K key) + { + return (T) getHibernateTemplate().get(getReferenceClass(), key); + } + + /** + * {@inheritDoc} + */ public void saveOrUpdate(final T obj) { getHibernateTemplate().saveOrUpdate(obj); @@ -334,7 +336,7 @@ /** * {@inheritDoc} */ - @SuppressWarnings("unchecked") + @SuppressWarnings("unchecked") public List<T> findFiltered(T filter, Order[] customOrder, Map<String, FilterMetadata> metadata, int maxResults, int page, List<Criterion> additionalCriteria) { @@ -353,11 +355,10 @@ } /** - * * {@inheritDoc} */ - @SuppressWarnings("unchecked") - public List<?> findFilteredProperties(final T filter, final Order[] customOrder, + @SuppressWarnings("unchecked") + public List< ? > findFilteredProperties(final T filter, final Order[] customOrder, final Map<String, FilterMetadata> metadata, final int maxResults, final int page, List<Criterion> additionalCriteria, List<String> properties) { @@ -399,44 +400,26 @@ } /** - * Obtain an instance of Query for a named query string defined in the mapping file. - * @param name the name of a query defined externally - * @param maxResults max number of results - * @return Query - */ - protected List< ? > getNamedQuery(final String name, final int maxResults) - { - return (List< ? >) getHibernateTemplate().execute(new HibernateCallback() - { - - public Object doInHibernate(Session ses) throws HibernateException - { - Query q = ses.getNamedQuery(name); - q.setMaxResults(maxResults); - return q.list(); - } - }); - } - - /** * Obtain an instance of Query for a named query string defined in the mapping file. Use the parameters given. * @param name the name of a query defined externally * @param params the parameter array * @param maxResults max number of results * @return Query */ - protected List< ? > getNamedQuery(final String name, final Serializable[] params, final int maxResults) + protected List< ? > findByNamedQuery(final String name, final Serializable[] params, final Integer maxResults) { - return (List< ? >) getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session ses) throws HibernateException { Query q = ses.getNamedQuery(name); - q.setMaxResults(maxResults); - if (null != params) + if (maxResults != null) { + q.setMaxResults(maxResults); + } + if (params != null) + { for (int i = 0; i < params.length; i++) { q.setParameter(i, params[i]); @@ -454,7 +437,7 @@ * @param maxResults max number of results * @return Query */ - protected List< ? > getNamedQuery(final String name, final Map<String, Object> params, final int maxResults) + protected List< ? > findByNamedQuery(final String name, final Map<String, Object> params, final Integer maxResults) { return (List< ? >) getHibernateTemplate().execute(new HibernateCallback() { @@ -462,7 +445,11 @@ public Object doInHibernate(Session ses) throws HibernateException { Query q = ses.getNamedQuery(name); - q.setMaxResults(maxResults); + if (maxResults != null) + { + q.setMaxResults(maxResults); + } + if (params != null) { for (Map.Entry<String, Object> entry : params.entrySet()) @@ -476,6 +463,34 @@ } /** + * Obtain an instance of Query for a named query string defined in the mapping file. Use the parameters given. + * @param name the name of a query defined externally + * @param params the parameter array + * @param maxResults max number of results + * @return Query + * @deprecated use the better named <code>findByNamedQuery</code> method + */ + @Deprecated + protected List< ? > getNamedQuery(final String name, final Serializable[] params, int maxResults) + { + return findByNamedQuery(name, params, maxResults > 0 ? maxResults : Integer.MAX_VALUE); + } + + /** + * Obtain an instance of Query for a named query string defined in the mapping file. Use the parameters given. + * @param name the name of a query defined externally + * @param params the parameter Map + * @param maxResults max number of results + * @return Query + * @deprecated use the better named <code>findByNamedQuery</code> method + */ + @Deprecated + protected List< ? > getNamedQuery(final String name, final Map<String, Object> params, int maxResults) + { + return findByNamedQuery(name, params, maxResults > 0 ? maxResults : Integer.MAX_VALUE); + } + + /** * Convenience method to set paramers in the query given based on the actual object type in passed in as the value. * You may need to add more functionaly to this as desired (or not use this at all). * @param query the Query to set @@ -583,7 +598,7 @@ private final Map<String, FilterMetadata> metadata; /** - * + * */ private final List<String> properties; Modified: trunk/openutils-bshd5/src/site/changes/changes.xml =================================================================== --- trunk/openutils-bshd5/src/site/changes/changes.xml 2008-04-27 12:47:09 UTC (rev 771) +++ trunk/openutils-bshd5/src/site/changes/changes.xml 2008-04-27 13:14:25 UTC (rev 772) @@ -10,6 +10,10 @@ <body> <release version="2.0.3" date="in svn" description="2.0.3"> <action type="update" dev="fgiust">Updated spring and slf4j versions</action> + <action type="update" dev="fgiust">getNamedQuery() methods deprecated and replaced by better named + findByNamedQuery()</action> + <action type="add" dev="fgiust">New get(id) public method that calls session.get(id) (same as loadIfAvailable, + without initialization of lazy objects)</action> </release> </body> </document> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |