📄 unitofworkhibernateimpl.java
字号:
/* @LICENSE_COPYRIGHT@ */
package net.sf.irunninglog.hibernate;
import java.io.Serializable;
import java.util.Collection;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import net.sf.irunninglog.businessobject.IBusinessObject;
import net.sf.irunninglog.transaction.IUnitOfWork;
import net.sf.irunninglog.transaction.TransactionException;
/**
* Hibernate implementation of the <code>IUnitOfWork</code> interface. This
* class uses the Hibernate API to provide a unit of work implementation.
*
* @author <a href="mailto:allan_e_lewis@yahoo.com">Allan Lewis</a>
* @version $Revision: 1.4 $ $Date: 2005/06/29 03:50:40 $
* @since iRunningLog 1.0
* @see IUnitOfWork
*/
public final class UnitOfWorkHibernateImpl implements IUnitOfWork {
/** Log instance for this class. */
private static final Log LOG =
LogFactory.getLog(UnitOfWorkHibernateImpl.class);
/** Hibernate session used to update the persistent store. */
private Session mSession;
/**
* Create a new unit of work. This has protected access, as only
* <code>TransactionHibernateImpl</code> instance should need to create
* unit of work instances.
*
* @param session The Hibernate session that this unit of work will use to
* interact with the persistent store.
*/
protected UnitOfWorkHibernateImpl(Session session) {
super();
if (LOG.isDebugEnabled()) {
LOG.debug("Creating a new unit of work using this session "
+ session);
}
if (session == null) {
LOG.error("Cannot create a unit of work with a null session");
throw new NullPointerException("Cannot create a unit of work with a"
+ " null Session");
}
mSession = session;
}
/**
* Create a new record in the applicaiton's persistent store using a
* business object.
*
* @param obj The business object to be used in creating the record
* @throws TransactionException If the record cannot be created
*/
public void create(Serializable obj) throws TransactionException {
try {
if (LOG.isDebugEnabled()) {
LOG.debug("create: Using Hibernate to save this BO " + obj);
}
mSession.save(obj);
} catch (HibernateException ex) {
LOG.error("Error saving a BO", ex);
throw new TransactionException("Error saving a BO", ex);
}
}
/**
* Update a record in the application's persistent store based on a business
* object's values.
*
* @param obj The business object to be used in locating and updating the
* record
* @throws TransactionException If the record cannot be updated
*/
public void update(Serializable obj) throws TransactionException {
try {
if (LOG.isDebugEnabled()) {
LOG.debug("update: Using Hibernate to update this BO " + obj);
}
mSession.update(obj);
} catch (HibernateException ex) {
LOG.error("Error updating a BO", ex);
throw new TransactionException("Error updating a BO", ex);
}
}
/**
* Either create or update a record in the application's persistent store
* based on the values of a business object.
*
* @param obj The business object to be used in either creating or updating
* the record
* @throws TransactionException If the record cannot be created/updated
*/
public void createOrUpdate(Serializable obj) throws TransactionException {
try {
if (LOG.isDebugEnabled()) {
LOG.debug("createOrUpdate: Using Hibernate to save/update this"
+ " BO " + obj);
}
mSession.saveOrUpdate(obj);
} catch (HibernateException ex) {
LOG.error("Error saving/updating a BO", ex);
throw new TransactionException("Error saving/updating a BO", ex);
}
}
/**
* Delete a record from the application's persistent store based on the
* values of a business object.
*
* @param obj The business object to be used in locating the object to
* delete
* @throws TransactionException If the record cannot be located/deleted
*/
public void delete(Serializable obj) throws TransactionException {
try {
if (LOG.isDebugEnabled()) {
LOG.debug("delete: Using Hibernate to delete this BO " + obj);
}
mSession.delete(obj);
} catch (HibernateException ex) {
LOG.error("Error deleting a BO", ex);
throw new TransactionException("Error deleting a BO", ex);
}
}
/**
* Load a record from the application's persistent store into a business
* object.
*
* @param clazz The class of the business object to be returned
* @param id The persistent id of the record to be loaded
* @return The business object containing values loaded from the persistent
* store
* @throws TransactionException If the record cannot be loaded
*/
public Serializable load(Class clazz, String id)
throws TransactionException {
try {
if (LOG.isDebugEnabled()) {
LOG.debug("load: Using Hibernate to load using the followinig"
+ " class and id: '" + clazz + "', '" + id + "'");
}
return (IBusinessObject) mSession.load(clazz, id);
} catch (HibernateException ex) {
LOG.error("Error loading a BO", ex);
throw new TransactionException("Error loading a BO", ex);
}
}
/**
* Locate records in the application's persistent store and return them
* as business objects.
*
* @param queryName String representing the query to be executed. Should
* correspond to a named query specified in the Hibernate
* mappings file
* @param parameters Values to be substituted into the query string to
* customize the query
* @return The collection of business object(s) located by the query
* @throws TransactionException If there is an error querying the persistent
* store
*/
public Collection find(String queryName, Object[] parameters)
throws TransactionException {
try {
Query query = mSession.getNamedQuery(queryName);
if (LOG.isDebugEnabled()) {
LOG.debug("find: Query before setting parameters "
+ query.getQueryString());
}
for (int i = 0; i < parameters.length; i++) {
if (LOG.isDebugEnabled()) {
LOG.debug("find: Replacing parameter with value "
+ parameters[i]);
}
query.setParameter(i, parameters[i]);
}
if (LOG.isDebugEnabled()) {
LOG.debug("find: Query after setting parameters "
+ query.getQueryString());
}
return query.list();
} catch (HibernateException ex) {
LOG.error("Error querying for BOs", ex);
throw new TransactionException("Error querying for BOs", ex);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -