I have the same problem as this person. I get that stack trace, due to the fact that I put a new record that violates bound of unique fields:
Exception in thread "main" org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.springframework.orm.hibernate5.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:241)
at org.springframework.orm.hibernate5.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:755)
at org.springframework.orm.hibernate5.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:594)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:504)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:292)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656)
at sistema.database.manager.BookManager$$EnhancerBySpringCGLIB$$d46d6805.create(<generated>)
at tests.unatantum.migration.InactiveBookMigration.migrate(InactiveBookMigration.java:53)
at tests.unatantum.migration.InactiveBookMigration.main(InactiveBookMigration.java:42)
when I call this method (in a manager), it has a generic variable T and the @Transactional
was put in the whole class, not on each single method:
public void create(T value) {
try{
System.out.println(value);
getDao().create(value);
System.out.println("good");
} catch (DataIntegrityViolationException dive){
throw new SistemaRuntimeException(dive.getMessage() + ": " + value);
}
}
The System.out.println
are just for debugging reasons.
The point is that I'd like to be able to catch that exception directly within the manager. But in the stack trace only the unknown class seems to work. How can I catch the exception within the manager (same method, if possible)?
It seems to throw the exception when it commits at the end of the method which calls the one shown (which is in the same manager). Do I really need to put the check before I call the dao, doesn't it make me waste time while I just should be able to wait for the exception?