I have a class having one method like:
public class AttachmentPaginationDao
extends PaginationDaoSupport
implements IAttachmentPaginationDao
{
public IPage<applicationattachment> searchApplicationAttachments(
String searchString,
PaginateRequest paginateRequest,
String applicationId)
{
paginateRequestValidator.validatePaginateRequest(
AttachmentFieldsEnum.class,
paginateRequest);
if (!StringUtils.hasText(paginateRequest.getOrderField()))
{
paginateRequest.setOrderField(
AttachmentFieldsEnum.name.name());
paginateRequest.setAscending(true);
} else if (paginateRequest.getOrderField().equals(
AttachmentFieldsEnum.userDisplayName.name()))
{
paginateRequest.setOrderField(
AttachmentFieldsEnum.userDisplayName.getField());
}
String searchClause = buildSearchClause(
new String[]
{
AttachmentFieldsEnum.name.getField()
},
searchString);
String select = "x.applicationHandle = \'" +
applicationId + "\'" +
(StringUtils.hasText(searchClause) ?
" and (" + searchClause + ")" :
"");
IPage<applicationattachment> page =
new Page<applicationattachment>();
page.setPaginateResponse(
createPaginateResponse(
ApplicationAttachment.class,
select,
paginateRequest,
null));
page.setItems(
retrievePage(
ApplicationAttachment.class,
select,
paginateRequest,
null));
return page;
}
}</applicationattachment></applicationattachment></applicationattachment>
Now, I am trying to write a test case for this, I created a base class for setup like:
public class AttachmentDAOTestBase extends AbstractCodeCenterTxnTest{
@PersistenceContext(unitName = "core")
protected EntityManager em;
protected Application application;
protected Component component;
protected ComponentUse componentUse;
protected User user;
@BeforeMethod
public void setupTestData(){
user = new User();
user.setFirstName("Test");
user.setLastName("Last");
user.setEmail("testUser@bds.com");
user.setName("testuser");
user.setActive(true);
user.setLocation("Waltham");
application = new Application();
application.setName("testapplication");
application.setOwner(user);
application.setVersion("1.0");
application.setDescription("a test application");
em.persist(user);
em.persist(application);
}
public ApplicationAttachment getApplicationAttachment(){
ApplicationAttachment attachment = new ApplicationAttachment();
attachment.setApplicationHandle(application.getHandle());
instantiateAttachment(attachment);
attachment.getUser();
return attachment;
}
}
And create class calling that method and setup like:
public class AttachmentPaginationDAOTest extends AttachmentDAOTestBase{
@Autowired
AttachmentPaginationDao attachmentPaginationDao;
@Qualifier("applicationAttachmentDao")
@Autowired
private IStringIdDao<ApplicationAttachment> applicationAttachmentDao;
@Qualifier("componentAttachmentDao")
@Autowired
private IStringIdDao<ComponentAttachment> componentAttachmentDao;
@Qualifier("componentUseAttachmentDao")
@Autowired
private IStringIdDao<ComponentUseAttachment> componentUseAttachmentDao;
private PaginateRequest getPaginateRequest(){
PaginateRequest paginateRequest = new PaginateRequest();
paginateRequest.setAscending(true);
paginateRequest.setGetTotalCount(true);
paginateRequest.setLimit(10);
return paginateRequest;
}
@Test
public void searchApplicationAttachmentsTest(){
AttachmentPaginationDao attachmentPaginationDao=new AttachmentPaginationDao();
ApplicationAttachment attachment = getApplicationAttachment();
attachment.setTimestamp(new Date());
String attachmentId = applicationAttachmentDao.persist(attachment);
IPage<ApplicationAttachment> appAttachments = attachmentPaginationDao.searchApplicationAttachments("", getPaginateRequest(), application.getHandle());
Assert.assertNotNull(appAttachments, "No Attachment pagination bean found");
}
}
While running test case, I am getting this exception:
javax.persistence.EntityExistsException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:612)
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:307)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
at $Proxy69.flush(Unknown Source)
at com.blackducksoftware.codecatalog.dao.jpa.JpaHandleIdDao.find(JpaHandleIdDao.java:33)
at com.blackducksoftware.codecatalog.dao.jpa.JpaHandleIdDao.find(JpaHandleIdDao.java:1)
at com.blackducksoftware.codecatalog.dao.jpa.ValidatingGenericDaoDecorator.find(ValidatingGenericDaoDecorator.java:24)
at com.blackducksoftware.codecatalog.service.impl.ApplicationService.getApplication(ApplicationService.java:147)
at com.blackducksoftware.codecatalog.service.impl.ApplicationService$$FastClassByCGLIB$$763f2d1c.invoke(<generated>)
at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191)
at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621)
at com.blackducksoftware.codecatalog.service.impl.ApplicationService$$EnhancerByCGLIB$$6d322916.getApplication(<generated>)
at com.blackducksoftware.codecatalog.validation.CrudValidatorSupport.validateApplicationLock(CrudValidatorSupport.java:146)
at com.blackducksoftware.codecatalog.validation.CrudValidatorSupport.access$0(CrudValidatorSupport.java:132)
at com.blackducksoftware.codecatalog.validation.CrudValidatorSupport$1.validate(CrudValidatorSupport.java:73)
at com.blackducksoftware.core.validation.impl.Validator.validate(Validator.java:78)
at com.blackducksoftware.codecatalog.validation.CrudValidatorSupport.validateCreate(CrudValidatorSupport.java:69)
at com.blackducksoftware.codecatalog.dao.jpa.ValidatingGenericDaoDecorator.persist(ValidatingGenericDaoDecorator.java:72)
at com.blackducksoftware.dao.AttachmentPaginationDAOTest.searchApplicationAttachmentsTest(AttachmentPaginationDAOTest.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:74)
at org.testng.internal.MethodInvocationHelper$1.runTestMethod(MethodInvocationHelper.java:176)
at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.run(AbstractTestNGSpringContextTests.java:158)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodInvocationHelper.invokeHookable(MethodInvocationHelper.java:189)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:666)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:846)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1170)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.runWorkers(TestRunner.java:1125)
at org.testng.TestRunner.privateRun(TestRunner.java:749)
at org.testng.TestRunner.run(TestRunner.java:600)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:317)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:312)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:274)
at org.testng.SuiteRunner.run(SuiteRunner.java:223)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:995)
at org.testng.TestNG.runSuitesLocally(TestNG.java:920)
at org.testng.TestNG.run(TestNG.java:856)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:110)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:174)
Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:92)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:222)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2229)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2665)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:60)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:304)
... 57 more
Caused by: java.sql.BatchUpdateException: Batch entry 0 insert into MULTI_AUDIT_LOG (audit_type, time_event, user_name, xml_state, ENTITY_ID, ENTITY_LABEL, ENTITY_TYPE, id) values ('create', '2011-02-11 17:30:36.640000 +05:30:00', NULL, '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><changeDetail><changes><key>description</key><post>a test application</post></changes><changes><key>name</key><post>testapplication</post></changes><changes><key>locked</key><post>false</post></changes><changes><key>useExportstatus</key><post>false</post></changes><changes><key>useProtexstatus</key><post>false</post></changes><changes><key>version</key><post>1.0</post></changes></changeDetail>', '27767', 'testapplication', 'Application', '15590') was aborted. Call getNextException to see the cause.
at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2569)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1796)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:407)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2708)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 70 more</pre>