📄 hibernatedao.java
字号:
package org.javabb.dao.hibernate;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;
import java.util.List;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import org.javabb.dao.DAOConstants;
import org.javabb.infra.Parser;
import org.javabb.vo.VOObject;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate.HibernateCallback;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
/*
* Copyright 2004 JavaFree.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* $Id: HibernateDAO.java,v 1.19.6.1.2.6 2007/08/31 21:04:43 daltoncamargo Exp $
* @author Dalton Camargo - <a href="mailto:dalton@javabb.org">dalton@javabb.org </a> <br>
* @author Ronald Tetsuo Miura
*/
public class HibernateDAO extends HibernateDaoSupport implements DAOConstants {
// public Session session = HibernateTransactionInterceptor.session;
protected String pathVO = "org.javabb.vo.";
/**
* Exclus鉶 especificada por uma query
* @param query - Query a ser verificada para a exclus鉶
*/
public void deleteFrom(String query) {
getHibernateTemplate().delete(Parser.replaceHQL(query));
}
/**
* @param vo - Nome da VO
* @param index Nome da coluna index a ser pesquisada
* @return - quantidade de registros encontrados na classe
*/
protected Integer countRowsOfTable(final String vo, final String index) {
return (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
return (Integer) session.iterate( //
MessageFormat.format("select count({0}) from {1}", //
new String[] { index, pathVO + vo })).next();
}
});
}
/**
* @param nmClass Nome da classe a ser pesquisada
* @param index Nome da coluna index a ser pesquisada
* @param whereEqualField Campo a ser comparado
* @param whereEqualValue Valor do campo a ser comparado
* @return Retorna o n鷐ero de registros encontrados
*/
protected Integer countRowsByWhere(
String nmClass,
String index,
String[] whereEqualField,
String[] whereEqualValue) {
String sql = MessageFormat.format("select count(tbl.{0}) from {1} as tbl ",
new Object[] { index, pathVO + nmClass });
/**
* Monta a cl醬sula where
*/
String where = " where ";
for (int i = 0; i < whereEqualField.length; i++) {
where += (" tbl." + whereEqualField[i] + " = " + whereEqualValue[i] + " and");
}
where = where.substring(0, where.length() - 3);
sql += where;
final String finalSql = sql;
return (Integer) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
return session.iterate(finalSql).next();
}
});
}
/**
* Carrega um VO que seja filho de VOObject
* @param obj - Vo a ser carregado
* @return - VO populado
*/
protected VOObject load(VOObject obj) {
return (VOObject) getHibernateTemplate().load(obj.getClass(), obj.getId());
}
/**
* Executa uma consulta atrav閟 de uma condi玢o
* @param condicao - Query a ser pesquisada
* @return - Lista de objetos pesquisados
*/
protected List getList(String condicao) {
final String finalCondicao = Parser.replaceHQL(condicao);
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
return session.createQuery(finalCondicao).list();
}
});
}
/**
* Executa uma query com par鈓etros de pagina玢o
* @param condicao - Query a ser pesquisada
* @param firstRes - Registro inicial
* @param maxRes - M醲imo de registros
* @return - Lista contendo os objetos pesquisados
*/
protected List getList(final String condicao, final int firstRes, final int maxRes) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
String cond = Parser.replaceHQL(condicao);
Query q = session.createQuery(cond);
q.setFirstResult(firstRes);
q.setMaxResults(maxRes);
return q.list();
}
});
}
/**
* Faz uma busca por Order By usando a pagina玢o
* @param nmClass - Nome da classe a ser pesquisada
* @param orderBy - Array contendo os campos do objeto a serem ordenados
* @param ascDesc - Ordem que os arrays ser鉶 ordenados
* @param firstRes - Primeiro registro a ser selecionado
* @param maxRes - M醲imo de registros que ser
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -