/**
* 刪除記錄
* @param entity BO的對象
* <pre>
* this.getHibernateTemplate().delete(entity);
* 按照規範,調用ECLRecordDelete記錄刪除的BO信息;
* </pre>
*/
public void delete(Object entity){
this.getHibernateTemplate().delete(entity);
}
/**
* 經過id得到記錄
* @param Class entityClass,對象類型
* @param Serializable id,BO的id值
* <pre>
* return hibernateTemplate.get(entityClass,id);
* </pre>
*/
public Object findById(Class entityClass, Serializable id) {
return this.getHibernateTemplate().get(entityClass,id);
}
/**
* 得到延遲加載數據
* @param Object object,須要加載的對象
* <pre>
* Hibernate.initialize(object);
* </pre>
*/
public void getLazyData(Object object) {
this.getHibernateTemplate().initialize(object);
}
/**
* 增長記錄
* @param Object entity,須要保存的BO
* <pre>
* hibernateTemplate.save(entity);
* return entity;
* </pre>
*/
public Object save(Object entity) {
Object id = (Object)this.getHibernateTemplate().save(entity);
return id;
}
/**
* 修改記錄
* @param Object,要修改的BO
* <pre>
* hibernateTemplate.update(entity);
* </pre>
*/
public void update(Object entity) {
this.getHibernateTemplate().update(entity);
}sql
//經過傳遞多個條件查詢表記錄數據庫
public List findByHQLCondition(Class entityClass,List propertyNameList,List propertyValueList){
StringBuffer sqlStr = new StringBuffer();
sqlStr.append("from ").append(entityClass.getName()).append(" as bo");
sqlStr.append(" where 1=1 ");
if(propertyNameList !=null && propertyValueList!=null && propertyNameList.size() == propertyValueList.size()){
for(int i=0;i<propertyNameList.size();i++){
sqlStr.append(" and bo.").append(propertyNameList.get(i)).append(" = :").append(propertyNameList.get(i));
}
}else{
return null;
}
final String sql = sqlStr.toString();
final List propertyNList = propertyNameList;
final List propertyVList = propertyValueList;
return(List) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
List li = new ArrayList();
Query query = session.createQuery(sql);
for(int i=0;i<propertyNList.size();i++){
query.setParameter(propertyNList.get(i).toString(),propertyVList.get(i));
}
Iterator it = query.iterate();
while(it.hasNext()){
li.add(it.next());
}
return li;
}
});
}session
//根據傳遞的HQL語句,來查詢數據庫app
public List findByHQL(String hql){
return this.getHibernateTemplate().find(hql);
}this
//只根據一個條件來查詢數據庫字段hibernate
public List findByOneHQLCondition(Class entityClass,String propertyName,String propertyValue) {
StringBuffer sqlStr = new StringBuffer();
sqlStr.append("from ").append(entityClass.getName()).append(" as bo where bo.");
sqlStr.append(propertyName);
sqlStr.append(" = :");
sqlStr.append(propertyName);
final String sql = sqlStr.toString();
return getHibernateTemplate().findByNamedParam(sql, propertyName,propertyValue);
}對象