thanks
nordborg,
But i do not want to do this,
Actually I am doing all these things through spring and hibernate, struts,
so at DaoImpl, what i am doing is just used a method like,
Code:
public class RoleDaoImpl extends GenericHibernateDAOImpl implements RoleDao {
private static Logger LOGGER = Logger.getLogger(RoleDaoImpl.class);
public void addRoleToUser(Role userRole) throws ServiceException {
LOGGER.info("Going to add Role to User.");
try {
saveOrUpdate(userRole);
LOGGER.info("added role to user.");
} catch (Exception e) {
LOGGER.error("Exception Occured in AddRoleToUser",e);
e.printStackTrace();
}
}
//... more code goes here....
}
so from the front end i just made an roleObject and call the
addRoleToUser(role) over the Dao.
This saveOrUpdate() method is defined in GenericHibernateDaoImpl which looks like
Code:
public class GenericHibernateDAOImpl<T, ID extends Serializable> implements
GenericHibernateDAO<T, ID> {
private Class<T> persistentClass;
protected HibernateTemplate hibernateTemplate;
public GenericHibernateDAOImpl() {
}
public GenericHibernateDAOImpl(Class<T> type) {
/*
* this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
* .getGenericSuperclass()).getActualTypeArguments()[0];
*/
this.persistentClass = type;
}
public Session getSession(){
return getHibernateTemplate().getSessionFactory().getCurrentSession();
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
public HibernateTemplate getHibernateTemplate() {
return this.hibernateTemplate;
}
/*
* public Session getSession() { // return HibernateUtil.getSession();
* return getHibernateTemplate().getSessionFactory().getCurrentSession(); }
*/
public Class<T> getPersistentClass() {
return persistentClass;
}
public T findById(ID id, boolean lock) {
T entity;
if (lock)
entity = (T) getHibernateTemplate().load(getPersistentClass(), id,
LockMode.UPGRADE);
else
entity = (T) getHibernateTemplate().load(getPersistentClass(), id);
return entity;
}
public List<T> findByExample(T exampleInstance, String[] excludeProperty) {
Criteria crit = getHibernateTemplate().getSessionFactory()
.getCurrentSession().createCriteria(getPersistentClass());
Example example = Example.create(exampleInstance);
for (String exclude : excludeProperty) {
example.excludeProperty(exclude);
}
crit.add(example);
return crit.list();
}
public List<T> findByCriteria(DetachedCriteria criteria) {
return getHibernateTemplate().findByCriteria(criteria);
}
public List<T> findByCriteria(DetachedCriteria criteria,int startIndex, int rowsToFetch) {
return getHibernateTemplate().findByCriteria(criteria,startIndex,rowsToFetch);
}
public T save(T entity) {
getHibernateTemplate().save(entity);
return entity;
}
public T saveOrUpdate(T entity) {
getHibernateTemplate().saveOrUpdate(entity);
return entity;
}
public void delete(T entity) {
getHibernateTemplate().delete(entity);
}
public void update(T entity) {
getHibernateTemplate().update(entity);
}
public void flush() {
getHibernateTemplate().flush();
}
public void clear() {
getHibernateTemplate().clear();
}
so i do not want to manually handle the transaction and session.
Any Idea after that?