import
java.io.*;
002 | import java.sql.SQLException; |
003 | import java.util.ArrayList; |
004 | import java.util.List; |
006 | import org.hibernate.Hibernate; |
007 | import org.hibernate.HibernateException; |
008 | import org.hibernate.Query; |
009 | import org.hibernate.SQLQuery; |
010 | import org.hibernate.Session; |
011 | import org.hibernate.Transaction; |
012 | import org.hibernate.type.Type; |
013 | import org.springframework.orm.hibernate3.HibernateCallback; |
014 | import org.springframework.orm.hibernate3.support.HibernateDaoSupport; |
016 | public class
GenericDao extends HibernateDaoSupport{ |
017 | private
ThreadLocal<Session> sessions = new
ThreadLocal<Session>(); |
018 | private
ThreadLocal<Transaction> transactions = new
ThreadLocal<Transaction>(); |
019 | public
synchronized Session getMySession(){ |
020 | Session session = sessions.get(); |
022 | session = getSession(); |
023 | transactions.set(session.beginTransaction()); |
024 | sessions.set(session); |
026 | if (!session.isOpen()){ |
027 | session = getSession(); |
028 | transactions.set(session.beginTransaction()); |
029 | sessions.set(session); |
033 | public
synchronized void commitMySession(){ |
034 | Session session = sessions.get(); |
035 | if (session!= null &&session.isOpen()){ |
036 | transactions.get().commit(); |
039 | transactions.remove(); |
042 | public
synchronized void rollbackMySession(){ |
043 | Session session = sessions.get(); |
044 | if (session!= null &&session.isOpen()){ |
045 | transactions.get().rollback(); |
048 | transactions.remove(); |
051 | public
<T>T get(Class<T>clazz, Serializable id){ |
052 | return
(T)getHibernateTemplate().get(clazz, id); |
054 | public
<T>T load(Class<T>clazz, Serializable id){ |
055 | return
(T)getHibernateTemplate().load(clazz, id); |
057 | public
<T> void save(T entity){ |
058 | //System.out.println("---->gdao.save("+entity.getClass().getName()+")----"); |
059 | getHibernateTemplate().save(entity); |
061 | public
<T> void update(T entity){ |
062 | getHibernateTemplate().update(entity); |
064 | public
<T> void delete(T entity){ |
065 | getHibernateTemplate().delete(entity); |
068 | public
int execUpdateSQL(String sql, Serializable...values){ |
069 | Session sess = getMySession(); |
070 | SQLQuery query = sess.createSQLQuery(sql); |
071 | if (values!= null &&values.length> 0 ){ |
072 | for ( int
i= 0 ; i<values.length; i++){ |
073 | query.setParameter(i, values[i]); |
076 | return
query.executeUpdate(); |
078 | public
Long getFirstLong(String sql, final
Serializable... values) throws
Exception{ |
079 | List<String> params = new ArrayList<String>( 1 ); |
080 | List<Type> types = new ArrayList<Type>( 1 ); |
081 | params.add( "c" ); types.add(Hibernate.INTEGER); |
082 | Object obj = findUnique(sql, params, types, values); |
086 | if (obj instanceof Object[]){ |
087 | obj = ((Object[])obj)[ 0 ]; |
089 | if (obj instanceof Number){ |
090 | return
((Number)obj).longValue(); |
094 | //public Long getFirstLong(String sql, List<String>params, List<Type>types) throws Exception{ |
095 | //Object[] obj = findUnique(sql, params, types); |
096 | //if(obj==null||obj.length==0){ |
099 | //Object object = obj[0]; |
100 | //if(object instanceof Number){ |
101 | //return ((Number)object).longValue(); |
105 | public
List<?> find(String hql){ |
106 | return
getHibernateTemplate().find(hql); |
108 | //public List<?> find(String hql, final Object...values){ |
110 | //return getHibernateTemplate().find(hql, values); |
113 | public
<T>List<T>findByPage(Class<T>clazz, final
int start, final
int limit, final
String where, final Serializable... values){ |
114 | String hql = "from " + clazz.getName() + " as e" ; |
115 | if (where!= null &&where.length()> 0 ){ |
116 | hql += " where " + where; |
118 | final
String fHql = hql; |
119 | return
getHibernateTemplate().executeFind( new
HibernateCallback(){ |
121 | public
Object doInHibernate(Session session) |
122 | throws
HibernateException, SQLException { |
123 | Query q = session.createQuery(fHql); |
124 | if (values!= null &&values.length> 0 ){ |
125 | for ( int
i= 0 ; i<values.length; i++){ |
126 | q.setParameter(i, values[i]); |
129 | q.setFirstResult(start).setMaxResults(limit); |
135 | public
<T>List<T>find(Class<T>clazz, final
String where, final Serializable... values){ |
136 | String hql = "from " + clazz.getSimpleName()+ " as e" ; |
137 | if (where!= null &&where.length()> 0 ){ |
138 | hql += " where " + where; |
140 | return
getHibernateTemplate().find(hql, values); |
142 | public
long getTotalCountByHql(Class<?> clazz, final String where,
final Serializable... values)
throws Exception { |
143 | String hql = "select count(*) from " + clazz.getSimpleName()+ " as e" ; |
144 | if (where!= null &&where.length()> 0 ){ |
145 | hql += " where " + where; |
147 | List<Number> cs = getHibernateTemplate().find(hql, values); |
148 | if (cs!= null &&cs.size()> 0 ){ |
154 | public
long getTotalCount(Class<?> clazz, final String where,
final Serializable... values)
throws Exception { |
155 | String sql = "select count(1) as c from " + clazz.getSimpleName() + " e" ; |
156 | if (where!= null &&where.length()> 0 ){ |
157 | sql += " where " + where; |
159 | return
getFirstLong(sql, values); |
162 | public
long getTotalCount(String sql,
final Serializable... values)
throws Exception { |
163 | return
getFirstLong(sql, values).longValue(); |
165 | public
Object[] findUnique( final
String sql, List<String>params, List<Type>types, final
Serializable...values) { |
166 | Session sess = getMySession(); |
167 | logger.debug( "------findUnique.getSession()! sess=" +sess.hashCode()); |
168 | SQLQuery query = sess.createSQLQuery(sql); |
169 | for ( int
j= 0 ; j<params.size(); j++){ |
170 | query.addScalar(params.get(j), types.get(j)); |
172 | if (values!= null &&values.length> 0 ){ |
173 | for ( int
i= 0 ; i<values.length; i++){ |
174 | query.setParameter(i, values[i]); |
177 | query.setMaxResults( 1 ); |
178 | List<?> list = query.list(); |
179 | if (list== null ||list.size()== 0 ){ |
182 | Object obj = list.get( 0 ); |
186 | logger.debug( "obj.type=" +obj.getClass().getName()); |
187 | if (!obj.getClass().isArray()){ //如果返回值不是数组,则要进行相关的处理 |
188 | if (obj instanceof Number){ |
189 | if (obj instanceof Long){ |
190 | return
new Long[]{(Long)(obj)}; |
192 | if (obj instanceof Integer){ |
193 | return
new Long[]{ new
Long((Integer)obj)}; |
195 | return
new Number[]{(Number)obj}; |
197 | return
new Object[]{obj}; |
201 | public
List<Object[]> find( final
String sql, List<String>params, List<Type>types, final
Serializable... values){ |
202 | Session sess = getMySession(); |
203 | logger.debug( "------find.getSession()! sess=" +sess.hashCode()); |
204 | SQLQuery query = sess.createSQLQuery(sql); |
206 | for ( int
j= 0 ; j<params.size(); j++){ |
207 | query.addScalar(params.get(j), types.get(j)); |
209 | if (values!= null &&values.length> 0 ){ |
210 | for ( int
i= 0 ; i<values.length; i++){ |
211 | query.setParameter(i, values[i]); |
216 | public
long getNextId(String sequence)
throws Exception{ |
217 | String sql = "select " +sequence+ ".nextval as nextid from dual" ; |
219 | Session sess = getMySession(); |
220 | logger.debug( "------generateId.getSession()! sess=" +sess.hashCode()); |
221 | SQLQuery query = sess.createSQLQuery(sql); |
222 | query.addScalar( "nextid" , Hibernate.LONG); |
223 | List<?> list = query.list(); |
224 | if (list== null ||list.size()== 0 ){ |
227 | Object obj = list.get( 0 ); |
228 | logger.debug( "obj.type=" +obj.getClass().getName()); |
229 | if (obj instanceof Number){ |
230 | return
((Number)obj).longValue(); |
234 | public
boolean exists(String sql,
final Object...values) throws
Exception{ |
235 | Session sess = getMySession(); |
236 | logger.debug( "------exists.getSession()! sess=" +sess.hashCode()); |
237 | SQLQuery query = sess.createSQLQuery(sql); |
238 | for ( int
i= 0 ; i<values.length; i++){ |
239 | query.setParameter(i, values[i]); |
241 | List<?> list = query.list(); |
242 | if (list== null ||list.size()== 0 ){ |
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理