001    package org.tynamo.hibernate.services;
002    
003    import org.hibernate.HibernateException;
004    import org.hibernate.Query;
005    import org.hibernate.type.Type;
006    
007    import java.util.Collection;
008    
009    public class QueryParameter
010    {
011            private String name;
012            private Object value;
013            private Type type;
014    
015            /**
016             * @param name  the name of the parameter
017             * @param value the value of the parameter
018             */
019            public QueryParameter(String name, Object value)
020            {
021                    this(name, value, null);
022            }
023    
024            /**
025             * @param name  the name of the parameter
026             * @param value the value of the parameter
027             * @param type  Hibernate type of the parameter (or <code>null</code> if
028             *              none specified)
029             */
030            public QueryParameter(String name, Object value, Type type)
031            {
032                    this.name = name;
033                    this.value = value;
034                    this.type = type;
035            }
036    
037            public String getName()
038            {
039                    return name;
040            }
041    
042            public Object getValue()
043            {
044                    return value;
045            }
046    
047            public QueryParameter(Type type)
048            {
049                    this.type = type;
050            }
051    
052            public Type getType()
053            {
054                    return type;
055            }
056    
057    
058            /**
059             * Apply the parameters to the given Query object.
060             *
061             * @param queryObject the Query object
062             * @throws org.hibernate.HibernateException
063             *          if thrown by the Query object
064             */
065            public void applyNamedParameterToQuery(Query queryObject) throws HibernateException
066            {
067                    if (value instanceof Collection)
068                    {
069                            if (type != null)
070                            {
071                                    queryObject.setParameterList(name, (Collection) value, type);
072                            } else
073                            {
074                                    queryObject.setParameterList(name, (Collection) value);
075                            }
076                    } else if (value instanceof Object[])
077                    {
078                            if (type != null)
079                            {
080                                    queryObject.setParameterList(name, (Object[]) value, type);
081                            } else
082                            {
083                                    queryObject.setParameterList(name, (Object[]) value);
084                            }
085                    } else
086                    {
087                            if (type != null)
088                            {
089                                    queryObject.setParameter(name, value, type);
090                            } else
091                            {
092                                    queryObject.setParameter(name, value);
093                            }
094                    }
095            }
096    
097    }
098