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. |