import
java.io.*;
import
java.sql.SQLException;
import
java.util.ArrayList;
import
java.util.List;
import
org.hibernate.Hibernate;
import
org.hibernate.HibernateException;
import
org.hibernate.Query;
import
org.hibernate.SQLQuery;
import
org.hibernate.Session;
import
org.hibernate.Transaction;
import
org.hibernate.type.Type;
import
org.springframework.orm.hibernate3.HibernateCallback;
import
org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public
class
GenericDao
extends
HibernateDaoSupport{
private
ThreadLocal<Session> sessions =
new
ThreadLocal<Session>();
private
ThreadLocal<Transaction> transactions =
new
ThreadLocal<Transaction>();
public
synchronized
Session getMySession(){
Session session = sessions.get();
if
(session==
null
){
session = getSession();
transactions.set(session.beginTransaction());
sessions.set(session);
}
else
if
(!session.isOpen()){
session = getSession();
transactions.set(session.beginTransaction());
sessions.set(session);
}
return
session;
}
public
synchronized
void
commitMySession(){
Session session = sessions.get();
if
(session!=
null
&&session.isOpen()){
transactions.get().commit();
session.close();
}
transactions.remove();
sessions.remove();
}
public
synchronized
void
rollbackMySession(){
Session session = sessions.get();
if
(session!=
null
&&session.isOpen()){
transactions.get().rollback();
session.close();
}
transactions.remove();
sessions.remove();
}
public
<T> T get(Class<T> clazz, Serializable id){
return
(T)getHibernateTemplate().get(clazz, id);
}
public
<T> T load(Class<T> clazz, Serializable id){
return
(T)getHibernateTemplate().load(clazz, id);
}
public
<T>
void
save(T entity){
// System.out.println("---->gdao.save("+entity.getClass().getName()+")----");
getHibernateTemplate().save(entity);
}
public
<T>
void
update(T entity){
getHibernateTemplate().update(entity);
}
public
<T>
void
delete(T entity){
getHibernateTemplate().delete(entity);
}
public
int
execUpdateSQL(String sql, Serializable...values){
Session sess = getMySession();
SQLQuery query = sess.createSQLQuery(sql);
if
(values!=
null
&&values.length>
0
){
for
(
int
i=
0
; i<values.length; i++){
query.setParameter(i, values[i]);
}
}
return
query.executeUpdate();
}
public
Long getFirstLong(String sql,
final
Serializable... values)
throws
Exception{
List<String> params =
new
ArrayList<String>(
1
);
List<Type> types =
new
ArrayList<Type>(
1
);
params.add(
"c"
); types.add(Hibernate.INTEGER);
Object obj = findUnique(sql, params, types, values);
if
(obj==
null
){
return
-1L;
}
if
(obj
instanceof
Object[]){
obj = ((Object[])obj)[
0
];
}
if
(obj
instanceof
Number){
return
((Number)obj).longValue();
}
return
-1L;
}
// public Long getFirstLong(String sql, List<String>params, List<Type>types) throws Exception{
// Object[] obj = findUnique(sql, params, types);
// if(obj==null||obj.length==0){
// return -1L;
// }
// Object object = obj[0];
// if(object instanceof Number){
// return ((Number)object).longValue();
// }
// return -1L;
// }
public
List<?> find(String hql){
return
getHibernateTemplate().find(hql);
}
// public List<?> find(String hql, final Object...values){
// logger.info(hql);
// return getHibernateTemplate().find(hql, values);
// }
public
<T> List<T> findByPage(Class<T> clazz,
final
int
start,
final
int
limit,
final
String where,
final
Serializable... values){
String hql =
"from "
+ clazz.getName() +
" as e"
;
if
(where!=
null
&&where.length()>
0
){
hql +=
" where "
+ where;
}
final
String fHql = hql;
return
getHibernateTemplate().executeFind(
new
HibernateCallback(){
public
Object doInHibernate(Session session)
throws
HibernateException, SQLException {
Query q = session.createQuery(fHql);
if
(values!=
null
&&values.length>
0
){
for
(
int
i=
0
; i<values.length; i++){
q.setParameter(i, values[i]);
}
}
q.setFirstResult(start).setMaxResults(limit);
return
q.list();
}
});
}
public
<T> List<T> find(Class<T> clazz,
final
String where,
final
Serializable... values){
String hql =
"from "
+ clazz.getSimpleName()+
" as e"
;
if
(where!=
null
&&where.length()>
0
){
hql +=
" where "
+ where;
}
return
getHibernateTemplate().find(hql, values);
}
public
long
getTotalCountByHql(Class<?> clazz,
final
String where,
final
Serializable... values)
throws
Exception {
String hql =
"select count(*) from "
+ clazz.getSimpleName()+
" as e"
;
if
(where!=
null
&&where.length()>
0
){
hql +=
" where "
+ where;
}
List<Number> cs = getHibernateTemplate().find(hql, values);
if
(cs!=
null
&&cs.size()>
0
){
Number n = cs.get(
0
);
return
n.longValue();
}
return
0
;
}
public
long
getTotalCount(Class<?> clazz,
final
String where,
final
Serializable... values)
throws
Exception {
String sql =
"select count(1) as c from "
+ clazz.getSimpleName() +
" e"
;
if
(where!=
null
&&where.length()>
0
){
sql +=
" where "
+ where;
}
return
getFirstLong(sql, values);
}
public
long
getTotalCount(String sql,
final
Serializable... values)
throws
Exception {
return
getFirstLong(sql, values).longValue();
}
public
Object[] findUnique(
final
String sql, List<String>params, List<Type>types,
final
Serializable...values) {
Session sess = getMySession();
logger.debug(
"------findUnique.getSession()! sess="
+sess.hashCode());
SQLQuery query = sess.createSQLQuery(sql);
for
(
int
j=
0
; j<params.size(); j++){
query.addScalar(params.get(j), types.get(j));
}
if
(values!=
null
&&values.length>
0
){
for
(
int
i=
0
; i<values.length; i++){
query.setParameter(i, values[i]);
}
}
query.setMaxResults(
1
);
List<?> list = query.list();
if
(list==
null
||list.size()==
0
){
return
null
;
}
Object obj = list.get(
0
);
if
(obj==
null
){
return
null
;
}
logger.debug(
"obj.type="
+obj.getClass().getName());
if
(!obj.getClass().isArray()){
//如果返回值不是数组,则要进行相关的处理
if
(obj
instanceof
Number){
if
(obj
instanceof
Long){
return
new
Long[]{(Long)(obj)};
}
if
(obj
instanceof
Integer){
return
new
Long[]{
new
Long((Integer)obj)};
}
return
new
Number[]{(Number)obj};
}
return
new
Object[]{obj};
}
return
(Object[])obj;
}
public
List<Object[]> find(
final
String sql, List<String>params, List<Type>types,
final
Serializable... values) {
Session sess = getMySession();
logger.debug(
"------find.getSession()! sess="
+sess.hashCode());
SQLQuery query = sess.createSQLQuery(sql);
for
(
int
j=
0
; j<params.size(); j++){
query.addScalar(params.get(j), types.get(j));
}
if
(values!=
null
&&values.length>
0
){
for
(
int
i=
0
; i<values.length; i++){
query.setParameter(i, values[i]);
}
}
return
query.list();
}
public
long
getNextId(String sequence)
throws
Exception{
String sql =
"select "
+sequence+
".nextval as nextid from dual"
;
Session sess = getMySession();
logger.debug(
"------generateId.getSession()! sess="
+sess.hashCode());
SQLQuery query = sess.createSQLQuery(sql);
query.addScalar(
"nextid"
, Hibernate.LONG);
List<?> list = query.list();
if
(list==
null
||list.size()==
0
){
return
-1L;
}
Object obj = list.get(
0
);
logger.debug(
"obj.type="
+obj.getClass().getName());
if
(obj
instanceof
Number){
return
((Number)obj).longValue();
}
return
-1L;
}
public
boolean
exists(String sql,
final
Object...values)
throws
Exception{
Session sess = getMySession();
logger.debug(
"------exists.getSession()! sess="
+sess.hashCode());
SQLQuery query = sess.createSQLQuery(sql);
for
(
int
i=
0
; i<values.length; i++){
query.setParameter(i, values[i]);
}
List<?> list = query.list();
if
(list==
null
||list.size()==
0
){
return
false
;
}
return
true
;
}
}