我有很多数据表,对于每个数据表,我们都有DAO接口和DAO实现类。
DAO接口示例:
public interface CancelPolicyDAO {
public CancelPolicy insertCancelPolicy(CancelPolicy cpdao)throws ChannelDispatcherException;
public CancelPolicy updateCancelPolicy(CancelPolicy cpdao)throws ChannelDispatcherException;
public void deleteCancelPolicy(CancelPolicy cpdao)throws ChannelDispatcherException;
public CancelPolicy findByCancelPolicyData(Integer id, Integer offSetUM, Integer nights, Float pOrAm, Byte isPercent)throws ChannelDispatcherException;
public CancelPolicy findByCancelPolicyId(Integer id)throws ChannelDispatcherException;
}
示例DAOImplementation类
public class CancelPolicyDAOImpl implements CancelPolicyDAO {
@Override
public CancelPolicy insertCancelPolicy(CancelPolicy bean) throws ChannelDispatcherException {
Session ses = null;
try {
ses = HibernateConnector.getInstance().getSession();
ses.save(bean);
ses.flush();
return bean;
} catch (Exception e) {
e.printStackTrace();
throw new ChannelDispatcherException(DbUtil.getStackTraceMessage(e));
} finally {
if (ses != null) {
try {
ses.close();
} catch (Exception er) {
er.printStackTrace();
}
}
}
}
@Override
public CancelPolicy updateCancelPolicy(CancelPolicy bean) throws ChannelDispatcherException {
Session sess = null;
try {
sess = HibernateConnector.getInstance().getSession();
sess.update(bean);
sess.flush();
return bean;
} catch (Exception e) {
e.printStackTrace();
throw new ChannelDispatcherException(DbUtil.getStackTraceMessage(e));
}
}
@Override
public void deleteCancelPolicy(CancelPolicy bean) throws ChannelDispatcherException {
Session sess = null;
try {
sess = HibernateConnector.getInstance().getSession();
sess.delete(bean);
sess.flush();
} catch (Exception e) {
e.printStackTrace();
throw new ChannelDispatcherException(DbUtil.getStackTraceMessage(e));
}
}
@Override
public CancelPolicy findByCancelPolicyData(Integer id, Integer offSetUM, Integer nights, Float pOrAm, Byte isPercent) throws ChannelDispatcherException {
Session ses = null;
try {
ses = HibernateConnector.getInstance().getSession();
Query query = ses.createQuery("from CancelPolicy a where "
+ " a.cancelPolicyTypeId =:cancelPolicyTypeId "
+ " and a.offsetUnitMultiplier =:offsetUnitMultiplier "
+ " and a.nights =:nights "
+ " and a.percentOramount =:percentOramount "
+ " and a.isPercent =:isPercent");
query.setParameter("cancelPolicyTypeId", id);
query.setParameter("offsetUnitMultiplier", (offSetUM));
query.setParameter("nights", (nights));
query.setParameter("percentOramount", pOrAm);
query.setParameter("isPercent", isPercent);
List queryList = query.list();
if (queryList != null && queryList.isEmpty()) {
return null;
} else {
return (CancelPolicy) queryList.get(0);
}
} catch (Exception e) {
e.printStackTrace();
throw new ChannelDispatcherException(DbUtil.getStackTraceMessage(e));
} finally {
if (ses != null) {
try {
ses.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public CancelPolicy findByCancelPolicyId(Integer id) throws ChannelDispatcherException {
Session ses = null;
try {
ses = HibernateConnector.getInstance().getSession();
Query query = ses.createQuery("from CancelPolicy a where "
+ " a.id =:id ");
query.setParameter("id", id);
List queryList = query.list();
if (queryList != null && queryList.isEmpty()) {
return null;
} else {
return (CancelPolicy) queryList.get(0);
}
} catch ( Exception e) {
e.printStackTrace();
throw new ChannelDispatcherException(DbUtil.getStackTraceMessage(e));
} finally {
if (ses != null) {
try {
ses.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
示例主方法
public static void main(String[] args) {
// How to handel Transaction in Hibernate ?
CancelPolicyDAO cancelPolicyDAO = HibernateDAOFactory.getInstance().getCancelPolicyDAO();
CancelPolicy insertCancelPolicy = cancelPolicyDAO.findByCancelPolicyData(2, 76, 25, 25.36f, 3);
if(insertCancelPolicy==null){
CancelPolicy cancelPolicy = new CancelPolicy();
cancelPolicy.setCancelPolicyTypeId(1);
cancelPolicy.setNights(2);
insertCancelPolicy = cancelPolicyDAO.insertCancelPolicy(cancelPolicy);
}
Integer autoIncrementId = insertCancelPolicy.getId();
AvailabilityDAO availabilityDAO = HibernateDAOFactory.getInstance().getAvailabilityDAO();
Availability availability = new Availability();
// using CancelPolicy autoIncrementId
availability.setId(autoIncrementId);
availability.setCount(2);
availability.setMaxLos(5);
availabilityDAO.insertAvailability(availability);
.
.
.
.
.
}
现在我的问题是如何在DAOImpl中处理事务?我应该为每个DAOImpl传递Session对象作为参数,还是有更好的方法?
TransactionTemplate
的逻辑 - 即一个能够在活动事务/持久化会话中执行回调(例如Callable
)的组件。这意味着在调用回调之前启动事务(创建Session
),并在调用回调后完成事务(刷新Session
,在异常情况下提交或回滚)。 - Pavel Horaltry/catch/finally
、回滚逻辑、刷新等。 - Pavel Horal