【Java开源代码栏目提醒】:网学会员,鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“CounterDaoImpl.java”一文,供大家参考学习!
/*
*/
package com.relationinfo.finance.daoimpl;
import com.relationinfo.finance.dao.*;
import com.relationinfo.finance.factory.*;
import com.relationinfo.finance.dto.*;
import com.relationinfo.finance.exceptions.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;
import org.hibernate.Session;
import org.hibernate.Query;
public class CounterDaoImpl implements CounterDao
{
/**
* 增加新记录到 COUNTER table.
*/
public CounterPk insert(Counter dto) throws CounterDaoException
{
try {
Session session = HibernateUtil.currentSession();
// 创建和复制Hibernate对象
com.relationinfo.finance.hibernate.Counter hibernate = new com.relationinfo.finance.hibernate.Counter();
hibernate.setNextValue( dto.getNextValue() );
session.save(hibernate);
// 返回DTO主键对象
return dto.createPk();
}
catch (Exception _e) {
System.out.println( _e );
throw new CounterDaoException( _e.getMessage() );
}
}
/**
* 更新单笔记录 COUNTER table.
*/
public void update(CounterPk pk, Counter dto) throws CounterDaoException
{
try {
Session session = HibernateUtil.currentSession();
// 使用DTO主键值返回Hibernate类
List list = HibernateUtil.executeSQLQuery( "COUNTER", Counter.class,"USER_ID = ? AND TABLE_NAME = ?", new Object[] { new Integer(pk.getUserId()), pk.getTableName() } );
if (list.size() == 0) {
throw new CounterDaoException( "row not found" );
}
com.relationinfo.finance.hibernate.Counter hibernate = (com.relationinfo.finance.hibernate.Counter) list.get(0);
hibernate.setUserId( dto.getUserId() );
hibernate.setTableName( dto.getTableName() );
hibernate.setNextValue( dto.getNextValue() );
session.save(hibernate);
}
catch (Exception _e) {
System.out.println( _e );
throw new CounterDaoException( _e.getMessage() );
}
}
/**
* Deletes a single row in the COUNTER table.
*/
public void delete(CounterPk pk) throws CounterDaoException
{
try {
Session session = HibernateUtil.currentSession();
// 使用DTO主键值返回Hibernate类
List list = HibernateUtil.executeSQLQuery( "COUNTER", Counter.class,"USER_ID = ? AND TABLE_NAME = ?", new Object[] { new Integer(pk.getUserId()), pk.getTableName() } );
if (list.size() == 0) {
throw new CounterDaoException( "row not found" );
}
com.relationinfo.finance.hibernate.Counter hibernate = (com.relationinfo.finance.hibernate.Counter) list.get(0);
session.delete(hibernate);
}
catch (Exception _e) {
System.out.println( _e );
throw new CounterDaoException( _e.getMessage() );
}
}
/**
* Returns the rows from the COUNTER table that matches the specified primary-key value.
*/
public Counter findByPrimaryKey(CounterPk pk) throws CounterDaoException
{
return findByPrimaryKey( pk.getUserId(), pk.getTableName() );
}
/**
* Returns all rows from the COUNTER table that match the criteria 'USER_ID = :userId AND TABLE_NAME = :tableName'.
*/
public Counter findByPrimaryKey(int userId, String tableName) throws CounterDaoException
{
Counter ret[] = convertToDTO( HibernateUtil.executeSQLQuery( "COUNTER", Counter.class,"USER_ID = ? AND TABLE_NAME = ?", new Object[] { new Integer(userId), tableName } ));
return ret.length==0 ? null : ret[0];
}
/**
* Returns all rows from the COUNTER table that match the criteria ''.
*/
public Counter[] findAll() throws CounterDaoException
{
return convertToDTO( HibernateUtil.executeSQLQuery( "COUNTER", Counter.class,"", null ));
}
/**
* Returns all rows from the COUNTER table that match the criteria 'USER_ID = :userId'.
*/
public Counter[] findByUsr(int userId) throws CounterDaoException
{
return convertToDTO( HibernateUtil.executeSQLQuery( "COUNTER", Counter.class,"USER_ID = ?", new Object[] { new Integer(userId) } ));
}
/**
* Returns all rows from the COUNTER table that match the criteria 'USER_ID = :userId'.
*/
public Counter[] findWhereUserIdEquals(int userId) throws CounterDaoException
{
return convertToDTO( HibernateUtil.executeSQLQuery( "COUNTER", Counter.class,"USER_ID = ?", new Object[] { new Integer(userId) } ));
}
/**
* Returns all rows from the COUNTER table that match the criteria 'TAB