⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 relationdaoimpl.java

📁 由于跟踪客户的购物意向
💻 JAVA
字号:
package com.relationinfo.customertrace.daoimpl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;

import com.relationinfo.customertrace.dao.RelationDao;
import com.relationinfo.customertrace.dto.Relation;
import com.relationinfo.customertrace.dto.RelationPk;
import com.relationinfo.customertrace.exceptions.RelationDaoException;

public class RelationDaoImpl implements RelationDao {
	/**
	 * 增加新记录到 relation table.
	 */
	public RelationPk insert(Relation dto) throws RelationDaoException {
		try {
			Session session = HibernateUtil.currentSession();

			// 创建和复制Hibernate对象
			com.relationinfo.customertrace.hibernate.Relation hibernate = new com.relationinfo.customertrace.hibernate.Relation();
			hibernate.setCustomercode(dto.getCustomercode());
			hibernate.setRelationname(dto.getRelationname());

			session.save(hibernate);

			// 返回DTO主键对象
			return dto.createPk();
		} catch (Exception _e) {
			System.out.println(_e);
			throw new RelationDaoException(_e.getMessage());
		}

	}

	/**
	 * 更新单笔记录 relation table.
	 */
	public void update(RelationPk pk, Relation dto) throws RelationDaoException {
		try {
			Session session = HibernateUtil.currentSession();

			// 使用DTO主键值返回Hibernate类
			List list = HibernateUtil.executeSQLQuery("relation",
					Relation.class, "relationcode = ?", new Object[] { pk
							.getRelationcode() });
			if (list.size() == 0) {
				throw new RelationDaoException("row not found");
			}

			com.relationinfo.customertrace.hibernate.Relation hibernate = (com.relationinfo.customertrace.hibernate.Relation) list
					.get(0);
			hibernate.setRelationcode(dto.getRelationcode());
			hibernate.setCustomercode(dto.getCustomercode());
			hibernate.setRelationname(dto.getRelationname());

			session.save(hibernate);

		} catch (Exception _e) {
			System.out.println(_e);
			throw new RelationDaoException(_e.getMessage());
		}

	}

	/**
	 * Deletes a single row in the relation table.
	 */
	public void delete(RelationPk pk) throws RelationDaoException {
		try {
			Session session = HibernateUtil.currentSession();

			// 使用DTO主键值返回Hibernate类
			List list = HibernateUtil.executeSQLQuery("relation",
					Relation.class, "relationcode = ?", new Object[] { pk
							.getRelationcode() });
			if (list.size() == 0) {
				throw new RelationDaoException("row not found");
			}

			com.relationinfo.customertrace.hibernate.Relation hibernate = (com.relationinfo.customertrace.hibernate.Relation) list
					.get(0);
			session.delete(hibernate);

		} catch (Exception _e) {
			System.out.println(_e);
			throw new RelationDaoException(_e.getMessage());
		}

	}

	/**
	 * Returns the rows from the relation table that matches the specified
	 * primary-key value.
	 */
	public Relation findByPrimaryKey(RelationPk pk) throws RelationDaoException {
		return findByPrimaryKey(pk.getRelationcode());
	}

	/**
	 * Returns all rows from the relation table that match the criteria
	 * 'relationcode = :relationcode'.
	 */
	public Relation findByPrimaryKey(String relationcode)
			throws RelationDaoException {
		Relation ret[] = convertToDTO(HibernateUtil.executeSQLQuery("relation",
				Relation.class, "relationcode = ?",
				new Object[] { relationcode }));
		return ret.length == 0 ? null : ret[0];
	}

	/**
	 * Returns all rows from the relation table that match the criteria ''.
	 */
	public Relation[] findAll() throws RelationDaoException {
		return convertToDTO(HibernateUtil.executeSQLQuery("relation",
				Relation.class, "", null));
	}

	/**
	 * Returns all rows from the relation table that match the criteria
	 * 'customercode = :customercode'.
	 */
	public Relation[] findByCustomer(String customercode)
			throws RelationDaoException {
		return convertToDTO(HibernateUtil.executeSQLQuery("relation",
				Relation.class, "customercode = ?",
				new Object[] { customercode }));
	}

	/**
	 * Returns all rows from the relation table that match the criteria
	 * 'relationcode = :relationcode'.
	 */
	public Relation[] findWhereRelationcodeEquals(String relationcode)
			throws RelationDaoException {
		return convertToDTO(HibernateUtil.executeSQLQuery("relation",
				Relation.class, "relationcode = ?",
				new Object[] { relationcode }));
	}

	/**
	 * Returns all rows from the relation table that match the criteria
	 * 'customercode = :customercode'.
	 */
	public Relation[] findWhereCustomercodeEquals(String customercode)
			throws RelationDaoException {
		return convertToDTO(HibernateUtil.executeSQLQuery("relation",
				Relation.class, "customercode = ?",
				new Object[] { customercode }));
	}

	/**
	 * Returns all rows from the relation table that match the criteria
	 * 'relationname = :relationname'.
	 */
	public Relation[] findWhereRelationnameEquals(String relationname)
			throws RelationDaoException {
		return convertToDTO(HibernateUtil.executeSQLQuery("relation",
				Relation.class, "relationname = ?",
				new Object[] { relationname }));
	}

	/**
	 * Method 'RelationDaoImpl'
	 * 
	 */
	public RelationDaoImpl() {
	}

	/**
	 * Method 'convertToDTO'
	 * 
	 * @param hibernateList
	 * @throws RelationDaoException
	 * @return Relation[]
	 */
	public Relation[] convertToDTO(List hibernateList)
			throws RelationDaoException {
		Iterator iter = hibernateList.iterator();
		List list = new ArrayList();
		while (iter.hasNext()) {
			Relation dto = (Relation) iter.next();

			list.add(dto);
		}

		Relation ret[] = new Relation[list.size()];
		list.toArray(ret);
		return ret;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -