Hello,
I have coded my Dao with a generic class, and even a DaoException class, but I'm still wondering if I'm doing the right thing, and how to handle exceptions, that is, how to react to an exception after it occurred.
I was told, in case an Hibernate Exception is fired up, to roll back the transaction, close the session, and reattach the object to a new session... Now, this is very theoritical to me.
As to rolling back the transaction, is it something I need to do myself ? I'm asking because I'm running it with Spring that takes care of the transaction.
The Spring Hibernate configuration
Code:
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
Here is chunks of my code to give an idea on what I stand:
Some of the generic Dao methods
Code:
@Override
public T makePersistent(T entity) {
try {
getSession().saveOrUpdate(entity);
} catch (HibernateException e) {
throw new DaoException("Saving the entity failed", e);
}
return entity;
}
@Override
public void makeTransient(T entity) {
try {
getSession().delete(entity);
} catch (HibernateException e) {
throw new DaoException("Deleting the entity failed", e);
}
}
The DaoException
Code:
public class DaoException extends HibernateException {
private Exception hiddenException;
public DaoException(String error, Exception exception) {
super(error);
hiddenException = exception;
}
public Exception getHiddenException() {
return (hiddenException);
}
}
A Dao method
Code:
@Override
public void delete(Contact contact) {
try {
makeTransient(contact);
} catch (ConstraintViolationException e) {
// rollback and close the current session, start a new session and
// make sure that the contact
// is attached to the new session
} finally {
close();
}
}
And in there, you can see in comment what I'm trying to do..
I need it for a test that checks the well functioning of a Hibernate constraint violation exception.
The test
Code:
@Test
public void testDelete() {
contactReferer0 = contactRefererDao.makePersistent(contactReferer0);
contactReferer1 = contactRefererDao.makePersistent(contactReferer1);
long count = contactRefererDao.countAllRows();
assertEquals(2, count);
contactRefererDao.makeTransient(contactReferer0);
count = contactRefererDao.countAllRows();
assertEquals(1, count);
Contact contact = new Contact();
contact.setEmail("email");
contact.setMessage("message");
DateTime contactDateTime = new DateTime();
contact.setContactDateTime(contactDateTime);
contact.setContactReferer(contactReferer1);
contact = contactDao.makePersistent(contact);
try {
contactDao.delete(contact);
fail();
} catch (DaoException e) {
// how to create a new session and reattach the contact ?
// so as to be able to run the next contactRefererDao.countAllRows(); statement
} finally {
}
count = contactRefererDao.countAllRows();
assertEquals(1, count);
contact.setContactReferer(null);
contactRefererDao.makeTransient(contactReferer1);
count = contactRefererDao.countAllRows();
assertEquals(0, count);
}
All my application compiles fine, all my tests pass fine too, except for the one above.
Tough this Hibernate cookie..