📄 hibernateservice.java
字号:
package com.tcg.core;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Created by IntelliJ IDEA.
* UserSession: Administrator
* Date: 2007-9-11
* Time: 14:27:33
* To change this template use File | Settings | File Templates.
*/
public class HibernateService extends HibernateDaoSupport {
public void initialize(Object object, String... methodName){
if(methodName != null){
for (int i = 0; i < methodName.length; i++) {
String[] methodArray = methodName[i].split("\\.");
Method method = null;
Object initializeObject = object;
try {
if(methodArray.length == 1){
this.getHibernateTemplate().lock(initializeObject, org.hibernate.LockMode.NONE);
method = object.getClass().getMethod(methodArray[0], new Class[] {});
initializeObject = method.invoke(initializeObject, new Object[] {});
this.getHibernateTemplate().initialize(initializeObject);
}else{
for(int j=0;j<methodArray.length;j++){
method = initializeObject.getClass().getMethod(methodArray[j], new Class[] {});
initializeObject = method.invoke(initializeObject, new Object[] {});
}
this.getHibernateTemplate().lock(initializeObject, org.hibernate.LockMode.NONE);
this.getHibernateTemplate().initialize(initializeObject);
}
} catch (NoSuchMethodException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IllegalAccessException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (InvocationTargetException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}else{
getHibernateTemplate().lock(object, org.hibernate.LockMode.NONE);
getHibernateTemplate().initialize(object);
}
}
public void initialize(List list, String... methodName){
for (Object o : list) {
initialize(o, methodName);
}
}
public void initialize(Set set, String... methodName){
for (Object o : set) {
initialize(o, methodName);
}
}
public void initialize(Object[] objects, String... methodName){
for (Object o : objects) {
initialize(o, methodName);
}
}
public <T extends Object> T get(Class<T> aClass, Serializable id) {
if(id == null) return null;
return (T)getHibernateTemplate().get(aClass, id);
}
public void save(Object o) {
getHibernateTemplate().saveOrUpdate(o);
}
public void delete(Object o) {
getHibernateTemplate().delete(o);
}
public void delete(Class aClass, Serializable id) {
getHibernateTemplate().delete(getHibernateTemplate().get(aClass, id));
}
public Object getByQuery(String hql) {
List list = findByQuery(hql);
Object o = null;
if(list != null && list.size() > 0)
o = list.iterator().next();
return o;
}
public Map getBySQL(final String sql) {
return (Map) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createSQLQuery(sql);
List list = query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP).list();
if(list != null && list.size() > 0){
return list.iterator().next();
}else{
return new HashMap();
}
}
}, true);
}
public List findByQuery(String hql) {
return getHibernateTemplate().find(hql);
}
public List findByQuery(String hql, Object o) {
return getHibernateTemplate().find(hql, o);
}
public List findByQuery(String hql, Object[] o) {
return getHibernateTemplate().find(hql, o);
}
public List findByQuery(String hql, String[] strings, Object[] objects) {
return getHibernateTemplate().findByNamedParam(hql, strings, objects);
}
public List findBySQL(final String sql) {
return (List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createSQLQuery(sql);
return query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP).list();
}
}, true);
}
public void executeBySql(final String sql){
getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createSQLQuery(sql);
return query.executeUpdate();
}
}, true);
}
public List findByCriteria(DetachedCriteria criteria) {
return getHibernateTemplate().findByCriteria(criteria);
}
public PaginationSupport findPage(final String hql, final int currentPage, final int pageSize) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
String lowerHql = hql.trim().toLowerCase();
int fromPos = 0;
if(!lowerHql.startsWith("from ")){
fromPos = lowerHql.indexOf(" from ")+1;
}
String countHql;
if(fromPos == 0)
countHql = "select count(*) "+hql;
else
countHql = "select count(*) "+hql.substring(fromPos);
if(countHql.indexOf("fetch")>0) countHql = countHql.replaceAll(" fetch ", " ");
int distinctPos = lowerHql.indexOf(" distinct ");
int totalCount;
if(distinctPos>=0 && distinctPos<fromPos){
totalCount = session.createQuery(hql).list().size();
}else{
totalCount = ((Long) session.createQuery(countHql).uniqueResult()).intValue();
}
int startRow = (currentPage - 1) * pageSize;
if(startRow<0)startRow = 0;
Query query = session.createQuery(hql);
query.setFirstResult(startRow);
query.setMaxResults(pageSize);
List list = query.list();
return new PaginationSupport(list, totalCount, startRow, pageSize);
}
}, true);
}
public PaginationSupport findPage(final String hql, final String countHql,
final int currentPage, final int pageSize) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Query query = session.createQuery(hql);
int totalCount = ((Long) session.createQuery(countHql).uniqueResult()).intValue();
int startRow = (currentPage - 1) * pageSize;
if(startRow<0)startRow = 0;
query.setFirstResult(startRow);
query.setMaxResults(pageSize);
List list = query.list();
return new PaginationSupport(list, totalCount, startRow, pageSize);
}
}, true);
}
public PaginationSupport findPageBySQL(final String sql, final int currentPage, final int pageSize) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
String countSql = "select count(*) from ("+sql+")";
int totalCount = ((BigDecimal) session.createSQLQuery(countSql).uniqueResult()).intValue();
int startRow = (currentPage - 1) * pageSize;
if(startRow<0)startRow = 0;
Query query = session.createSQLQuery(sql);
query.setFirstResult(startRow);
query.setMaxResults(pageSize);
List list = query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP).list();
return new PaginationSupport(list, totalCount, startRow, pageSize);
}
}, true);
}
public PaginationSupport findPageByCriteria(final DetachedCriteria detachedCriteria,
final int currentPage, final int pageSize) {
return (PaginationSupport) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
Integer totalCount = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
if(totalCount == null)
totalCount = 0;
criteria.setProjection(null);
int startRow = (currentPage - 1) * pageSize;
while(startRow >= totalCount){
startRow = startRow - pageSize;
}
if(startRow<0)startRow = 0;
List items = criteria.setFirstResult(startRow).setMaxResults(pageSize).
setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY).list();
return new PaginationSupport(items, totalCount, startRow, pageSize);
}
}, true);
}
void executeUpdate(final String sql){
getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
return session.connection ().createStatement().executeUpdate(sql);
}
}, true);
}
public int getCountByQuery(final String hql)
{
return (Integer)getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException
{
String lowerHql = hql.trim().toLowerCase();
int fromPos = 0;
if(!lowerHql.startsWith("from ")){
fromPos = lowerHql.indexOf(" from ")+1;
}
String countHql;
if(fromPos == 0)
countHql = "select count(*) "+hql;
else
countHql = "select count(*) "+hql.substring(fromPos);
if(countHql.indexOf("fetch")>0) countHql = countHql.replaceAll(" fetch ", " ");
int distinctPos = lowerHql.indexOf(" distinct ");
int totalCount;
if(distinctPos>=0 && distinctPos<fromPos){
totalCount = session.createQuery(hql).list().size();
}else{
totalCount = ((Long) session.createQuery(countHql).uniqueResult()).intValue();
}
return totalCount; //To change body of implemented methods use File | Settings | File Templates.
}
}, true);
}
public static void main(String[] args) {
String hql = "select ffrom from22 dfdfrom * from sys where id in(select adf from fd sdfs group by dfsdf)";
String lowerHql = hql.trim().toLowerCase();
int fromPos = 0;
if(!lowerHql.startsWith("from ")){
fromPos = lowerHql.indexOf(" from ")+1;
}
String countHql = "";
if(fromPos == 0)
countHql = "select count(*) "+hql;
else
countHql = "select count(*) "+hql.substring(fromPos);
System.out.println("countHql = " + countHql);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -