【Java开源代码栏目提醒】:网学会员鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“TxDaoImpl.java”一文,供大家参考学习
package com.relationinfo.finance.daoimpl;
import com.relationinfo.finance.dao.*;
import com.relationinfo.finance.factory.*;
import java.util.Date;
import java.math.BigDecimal;
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 TxDaoImpl implements TxDao {
/**
* 在TX表中增加记录
*/
public TxPk insert(Tx dto) throws TxDaoException {
try {
Session session = HibernateUtil.currentSession();
// 创建和复制Hibernate对象
com.relationinfo.finance.hibernate.Tx hibernate = new com.relationinfo.finance.hibernate.Tx();
hibernate.setCatId(dto.getCatId());
hibernate.setTxDate(dto.getTxDate());
hibernate.setPayee(dto.getPayee());
hibernate.setAmt(dto.getAmt());
hibernate.setTransferId(dto.getTransferId());
hibernate.setRef(dto.getRef());
hibernate.setDescription(dto.getDescription());
hibernate.setClr(dto.getClr());
//保存Hibernate对象
session.save(hibernate);
// 返回 DTO pk object
return dto.createPk();
} catch (Exception _e) {
System.out.println(_e);
throw new TxDaoException(_e.getMessage());
}
}
/**
* 更新Tx表中记录.
*/
public void update(TxPk pk, Tx dto) throws TxDaoException {
try {
Session session = HibernateUtil.currentSession();
// 通过主键DTO检索Hibernate类
List list = HibernateUtil.executeSQLQuery("TX", Tx.class,
"USER_ID = ? AND ACC_ID = ? AND TX_ID = ?", new Object[] {
new Integer(pk.getUserId()),
new Integer(pk.getAccId()),
new Integer(pk.getTxId()) });
if (list.size() == 0) {
throw new TxDaoException("row not found");
}
//值复制
com.relationinfo.finance.hibernate.Tx hibernate = (com.relationinfo.finance.hibernate.Tx) list
.get(0);
hibernate.setUserId(dto.getUserId());
hibernate.setAccId(dto.getAccId());
hibernate.setTxId(dto.getTxId());
hibernate.setCatId(dto.getCatId());
hibernate.setTxDate(dto.getTxDate());
hibernate.setPayee(dto.getPayee());
hibernate.setAmt(dto.getAmt());
hibernate.setTransferId(dto.getTransferId());
hibernate.setRef(dto.getRef());
hibernate.setDescription(dto.getDescription());
hibernate.setClr(dto.getClr());
//保存,更新Hibernate对象
session.save(hibernate);
} catch (Exception _e) {
System.out.println(_e);
throw new TxDaoException(_e.getMessage());
}
}
/**
* 从Tx表中删除一笔记录.
*/
public void delete(TxPk pk) throws TxDaoException {
try {
Session session = HibernateUtil.currentSession();
// 通过主键DTO检索Hibernate类
List list = HibernateUtil.executeSQLQuery("TX", Tx.class,
"USER_ID = ? AND ACC_ID = ? AND TX_ID = ?", new Object[] {
new Integer(pk.getUserId()),
new Integer(pk.getAccId()),
new Integer(pk.getTxId()) });
if (list.size() == 0) {
throw new TxDaoException("row not found");
}
com.relationinfo.finance.hibernate.Tx hibernate = (com.relationinfo.finance.hibernate.Tx) list
.get(0);
//通过Session删除Hibernate对象
session.delete(hibernate);
} catch (Exception _e) {
System.out.println(_e);
throw new TxDaoException(_e.getMessage());
}
}
/**
* Returns the rows from the TX table that matches the specified primary-key
* value.
*/
public Tx findByPrimaryKey(TxPk pk) throws TxDaoException {
return findByPrimaryKey(pk.getUserId(), pk.getAccId(), pk.getTxId());
}
/**
* Returns all rows from the TX table that match the criteria 'USER_ID =
* :userId AND ACC_ID = :accId AND TX_ID = :txId'.
*/
public Tx findByPrimaryKey(int userId, int accId, int txId)
throws TxDaoException {
Tx ret[] = convertToDTO(HibernateUtil.executeSQLQuery("TX", Tx.class,
"USER_ID = ? AND ACC_ID = ? AND TX_ID = ?", new Object[] {
new Integer(userId), new Integer(accId),
new Integer(txId) }));
return ret.length == 0 ? null : ret[0];
}
/**
* Returns all rows from the TX table that match the criteria ''.
*/
public Tx[] findAll() throws TxDaoException {
return convertToDTO(HibernateUtil.executeSQLQuery("TX", Tx.class, "",
null));
}
/**
* Returns all rows from the TX table that match the criteria 'USER