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

📄 toplinkdaotemplate.java

📁 本套系统采用了业界当前最为流行的beanAction组件
💻 JAVA
字号:
/*
 *  Copyright 2004 Clinton Begin
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.ibatis.dao.client.template;

import java.util.List;

import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import oracle.toplink.queryframework.ReportQuery;
import oracle.toplink.sessions.Session;
import oracle.toplink.sessions.UnitOfWork;

import com.ibatis.dao.client.DaoException;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.engine.transaction.toplink.ToplinkDaoTransaction;

/**
 * A DaoTemplate for Toplink implementations that provides a convenient method
 * to access the Toplink Session.
 * 
 * @author Wayne Gentile
 * @version $Revision: 1.1 $ $Date: 2008/07/24 06:53:14 $
 */
public abstract class ToplinkDaoTemplate extends DaoTemplate {

	//~ Constructors
	// -----------------------------------------------------------

	/**
	 * The DaoManager that manages this Dao instance will be passed in as the
	 * parameter to this constructor automatically upon instantiation.
	 * 
	 * @param daoManager
	 *            The Dao manager instance for this template
	 */
	public ToplinkDaoTemplate(DaoManager daoManager) {
		super(daoManager);

	}

	//~ Methods
	// ----------------------------------------------------------------

	/**
	 * Returns a count of the number of objects in a result set given the
	 * specified expression.
	 * 
	 * @param referenceClass
	 *            The reference class to use as instances in the result set
	 * @param expression
	 *            The expression to use as a where clause
	 * 
	 * @return The number of rows returned in the result set after executing the
	 *         query
	 */
	protected int getCount(Class referenceClass, Expression expression) {

		int count = 0;

		// Build the query to retrieve the object
		ExpressionBuilder builder = new ExpressionBuilder();
		ReportQuery query = new ReportQuery(builder);
		query.setReferenceClass(referenceClass);
		query.addCount();
		query.setSelectionCriteria(expression);

		// Execute the query
		List results = (List) getSession().executeQuery(query);
		if ((results != null) && (results.size() > 0)) {

			count = ((Integer) results.get(0)).intValue();

		}

		return count;

	}

	/**
	 * Gets the Toplink session associated with the current DaoTransaction that
	 * this Dao is working under.
	 * 
	 * @return A Toplink Session instance.
	 * 
	 * @throws DaoException
	 *             If a DaoException is thrown
	 */
	protected Session getSession() throws DaoException {

		ToplinkDaoTransaction trans = (ToplinkDaoTransaction) daoManager
				.getTransaction(this);

		return trans.getSession();

	}

	/**
	 * Gets the Toplink UnitOfWork associated with the current DaoTransaction
	 * that this Dao is working under.
	 * 
	 * @return A Toplink UnitOfWork instance.
	 * 
	 * @throws DaoException
	 *             If a DaoException is thrown
	 */
	protected UnitOfWork getUnitOfWork() throws DaoException {

		// Get a UnitOfWork using the TransactionManager
		ToplinkDaoTransaction trans = (ToplinkDaoTransaction) daoManager
				.getTransaction(this);
		UnitOfWork uow = trans.getUnitOfWork();

		if ((uow == null) || !uow.isActive()) {

			throw new DaoException("No active unit of work.");

		}

		return uow;

	}

}

⌨️ 快捷键说明

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