【Java开源代码栏目提醒】:网学会员鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“CatDaoImpl.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.CatDao;
import com.relationinfo.finance.dto.Cat;
import com.relationinfo.finance.dto.CatPk;
import com.relationinfo.finance.exceptions.CatDaoException;
public class CatDaoImpl implements CatDao
{
/**
* 增加新记录到 CAT table.
*/
public CatPk insert(Cat dto) throws CatDaoException
{
try {
Session session = HibernateUtil.currentSession();
// 创建和复制Hibernate对象
com.relationinfo.finance.hibernate.Cat hibernate = new com.relationinfo.finance.hibernate.Cat();
hibernate.setCatName( dto.getCatName() );
session.save(hibernate);
// 返回DTO主键对象
return dto.createPk();
}
catch (Exception _e) {
System.out.println( _e );
throw new CatDaoException( _e.getMessage() );
}
}
/**
* 更新单笔记录 CAT table.
*/
public void update(CatPk pk, Cat dto) throws CatDaoException
{
try {
Session session = HibernateUtil.currentSession();
// 使用DTO主键值返回Hibernate类
List list = HibernateUtil.executeSQLQuery( "CAT", Cat.class,"USER_ID = ? AND CAT_ID = ?", new Object[] { new Integer(pk.getUserId()), new Integer(pk.getCatId()) } );
if (list.size() == 0) {
throw new CatDaoException( "row not found" );
}
com.relationinfo.finance.hibernate.Cat hibernate = (com.relationinfo.finance.hibernate.Cat) list.get(0);
hibernate.setUserId( dto.getUserId() );
hibernate.setCatId( dto.getCatId() );
hibernate.setCatName( dto.getCatName() );
session.save(hibernate);
}
catch (Exception _e) {
System.out.println( _e );
throw new CatDaoException( _e.getMessage() );
}
}
/**
* Deletes a single row in the CAT table.
*/
public void delete(CatPk pk) throws CatDaoException
{
try {
Session session = HibernateUtil.currentSession();
// 使用DTO主键值返回Hibernate类
List list = HibernateUtil.executeSQLQuery( "CAT", Cat.class,"USER_ID = ? AND CAT_ID = ?", new Object[] { new Integer(pk.getUserId()), new Integer(pk.getCatId()) } );
if (list.size() == 0) {
throw new CatDaoException( "row not found" );
}
com.relationinfo.finance.hibernate.Cat hibernate = (com.relationinfo.finance.hibernate.Cat) list.get(0);
session.delete(hibernate);
}
catch (Exception _e) {
System.out.println( _e );
throw new CatDaoException( _e.getMessage() );
}
}
/**
* Returns the rows from the CAT table that matches the specified primary-key value.
*/
public Cat findByPrimaryKey(CatPk pk) throws CatDaoException
{
return findByPrimaryKey( pk.getUserId(), pk.getCatId() );
}
/**
* Returns all rows from the CAT table that match the criteria 'USER_ID = :userId AND CAT_ID = :catId'.
*/
public Cat findByPrimaryKey(int userId, int catId) throws CatDaoException
{
Cat ret[] = convertToDTO( HibernateUtil.executeSQLQuery( "CAT", Cat.class,"USER_ID = ? AND CAT_ID = ?", new Object[] { new Integer(userId), new Integer(catId) } ));
return ret.length==0 ? null : ret[0];
}
/**
* Returns all rows from the CAT table that match the criteria ''.
*/
public Cat[] findAll() throws CatDaoException
{
return convertToDTO( HibernateUtil.executeSQLQuery( "CAT", Cat.class,"", null ));
}
/**
* Returns all rows from the CAT table that match the criteria 'USER_ID = :userId'.
*/
public Cat[] findByUsr(int userId) throws CatDaoException
{
return convertToDTO( HibernateUtil.executeSQLQuery( "CAT", Cat.class,"USER_ID = ?", new Object[] { new Integer(userId) } ));
}
/**
* Returns all rows from the CAT table that match the criteria 'USER_ID = :userId'.
*/
public Cat[] findWhereUserIdEquals(int userId) throws CatDaoException
{
return convertToDTO( HibernateUtil.executeSQLQuery( "CAT", Cat.class,"USER_ID = ?", new Object[] { new Integer(userId) } ));
}
/**
* Returns all rows from the CAT table that match the criteria 'CAT_ID = :catId'.
*/
public Cat[] findWhereCatIdEquals(int catId) throws CatDaoException
{
return convertToDTO( HibernateUtil.executeSQLQuery( "CAT", Cat.class,"CAT_ID = ?", new Object[] { new Integer(catId) } ));
}
/**
* Returns all rows from the CAT table that match the criteria 'CAT_NAME = :catName'.
*/
public Cat[] findWhereCatNameEquals(String catName) throws CatDaoException
{
return convertToDTO( HibernateUtil.executeSQLQuery( "CAT", Cat.class,"CAT_NAME = ?", new Object[] { catName } ));
}
/**
* Method 'CatDaoImpl'
*
*/
public CatDaoImpl()
{
}
/**
* Method 'convertToDTO'
*
* @param hibernateList
* @throws CatDaoException
* @return Cat[]
*/
public Cat[] convertToDTO(List hibernateList) throws CatDaoException
{
Iterator iter = hibernateList.iterator();
List list = new ArrayList();
while (iter.hasNext()) {
com.relationinfo.finance.hibernate.Cat hibernate = (com.relationinfo.finance.hibernate.Cat) iter.next();
// Create DTO and populate it from hibernate class
Cat dto = new Cat();
dto.setCatName( hibernate.getCatName() );
list.add( dto );
}
Cat ret[] = new Cat[ list.size() ];
list.toArray( ret );
return ret;
}
}
上一篇:
CatDaoFactory.java
下一篇:
美国emba教育概况及其借鉴意义