【Java开源代码栏目提醒】:网学会员鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“AccDaoImpl.java”一文,供大家参考学习
/*
*/
package com.relationinfo.finance.daoimpl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import com.relationinfo.finance.dao.AccDao;
import com.relationinfo.finance.dto.Acc;
import com.relationinfo.finance.dto.AccPk;
import com.relationinfo.finance.exceptions.AccDaoException;
public class AccDaoImpl implements AccDao
{
/**
* 增加新记录到 ACC table.
*/
public AccPk insert(Acc dto) throws AccDaoException
{
try {
Session session = HibernateUtil.currentSession();
// 创建和复制Hibernate对象
com.relationinfo.finance.hibernate.Acc hibernate = new com.relationinfo.finance.hibernate.Acc();
hibernate.setAccName( dto.getAccName() );
session.save(hibernate);
// 返回DTO主键对象
return dto.createPk();
}
catch (Exception _e) {
System.out.println( _e );
throw new AccDaoException( _e.getMessage() );
}
}
/**
* 更新单笔记录 ACC table.
*/
public void update(AccPk pk, Acc dto) throws AccDaoException
{
try {
Session session = HibernateUtil.currentSession();
// 使用DTO主键值返回Hibernate类
List list = HibernateUtil.executeSQLQuery( "ACC", Acc.class,"USER_ID = ? AND ACC_ID = ?", new Object[] { new Integer(pk.getUserId()), new Integer(pk.getAccId()) } );
if (list.size() == 0) {
throw new AccDaoException( "row not found" );
}
com.relationinfo.finance.hibernate.Acc hibernate = (com.relationinfo.finance.hibernate.Acc) list.get(0);
hibernate.setUserId( dto.getUserId() );
hibernate.setAccId( dto.getAccId() );
hibernate.setAccName( dto.getAccName() );
session.save(hibernate);
}
catch (Exception _e) {
System.out.println( _e );
throw new AccDaoException( _e.getMessage() );
}
}
/**
* Deletes a single row in the ACC table.
*/
public void delete(AccPk pk) throws AccDaoException
{
try {
Session session = HibernateUtil.currentSession();
// 使用DTO主键值返回Hibernate类
List list = HibernateUtil.executeSQLQuery( "ACC", Acc.class,"USER_ID = ? AND ACC_ID = ?", new Object[] { new Integer(pk.getUserId()), new Integer(pk.getAccId()) } );
if (list.size() == 0) {
throw new AccDaoException( "row not found" );
}
com.relationinfo.finance.hibernate.Acc hibernate = (com.relationinfo.finance.hibernate.Acc) list.get(0);
session.delete(hibernate);
}
catch (Exception _e) {
System.out.println( _e );
throw new AccDaoException( _e.getMessage() );
}
}
/**
* Returns the rows from the ACC table that matches the specified primary-key value.
*/
public Acc findByPrimaryKey(AccPk pk) throws AccDaoException
{
return findByPrimaryKey( pk.getUserId(), pk.getAccId() );
}
/**
* Returns all rows from the ACC table that match the criteria 'USER_ID = :userId AND ACC_ID = :accId'.
*/
public Acc findByPrimaryKey(int userId, int accId) throws AccDaoException
{
Acc ret[] = convertToDTO( HibernateUtil.executeSQLQuery( "ACC", Acc.class,"USER_ID = ? AND ACC_ID = ?", new Object[] { new Integer(userId), new Integer(accId) } ));
return ret.length==0 ? null : ret[0];
}
/**
* Returns all rows from the ACC table that match the criteria ''.
*/
public Acc[] findAll() throws AccDaoException
{
return convertToDTO( HibernateUtil.executeSQLQuery( "ACC", Acc.class,"", null ));
}
/**
* Returns all rows from the ACC table that match the criteria 'USER_ID = :userId'.
*/
public Acc[] findByUsr(int userId) throws AccDaoException
{
return convertToDTO( HibernateUtil.executeSQLQuery( "ACC", Acc.class,"USER_ID = ?", new Object[] { new Integer(userId) } ));
}
/**
* Returns all rows from the ACC table that match the criteria 'USER_ID = :userId'.
*/
public Acc[] findWhereUserIdEquals(int userId) throws AccDaoException
{
return convertToDTO( HibernateUtil.executeSQLQuery( "ACC", Acc.class,"USER_ID = ?", new Object[] { new Integer(userId) } ));
}
/**
* Returns all rows from the ACC table that match the criteria 'ACC_ID = :accId'.
*/
public Acc[] findWhereAccIdEquals(int accId) throws AccDaoException
{
return convertToDTO( HibernateUtil.executeSQLQuery( "ACC", Acc.class,"ACC_ID = ?", new Object[] { new Integer(accId) } ));
}
/**
* Returns all rows from the ACC table that match the criteria 'ACC_NAME = :accName'.
*/
public Acc[] findWhereAccNameEquals(String accName) throws AccDaoException
{
return convertToDTO( HibernateUtil.executeSQLQuery( "ACC", Acc.class,"ACC_NAME = ?", new Object[] { accName } ));
}
/**
* Method 'AccDaoImpl'
*
*/
public AccDaoImpl()
{
}
/**
* Method 'convertToDTO'
*
* @param hibernateList
* @throws AccDaoException
* @return Acc[]
*/
public Acc[] convertToDTO(List hibernateList) throws AccDaoException
{
Iterator iter = hibernateList.iterator();
List list = new ArrayList();
while (iter.hasNext()) {
com.relationinfo.finance.hibernate.Acc hibernate = (com.relationinfo.finance.hibernate.Acc) iter.next();
// Create DTO and populate it from hibernate class
Acc dto = new Acc();
dto.setAccName( hibernate.getAccName() );
list.add( dto );
}
Acc ret[] = new Acc[ list.size() ];
list.toArray( ret );
return ret;
}
}
上一篇:
AccDaoFactory.java
下一篇:
秋天是疼痛的