Click here to Skip to main content
16,007,858 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a class having one method like:
Java
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()))
        {
            // default order
            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:
Java
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:
Java
public class AttachmentPaginationDAOTest extends AttachmentDAOTestBase{
    @Autowired
    AttachmentPaginationDao attachmentPaginationDao;
    @Qualifier("applicationAttachmentDao")
    @Autowired
    private IStringIdDao&lt;ApplicationAttachment&gt; applicationAttachmentDao;
    @Qualifier("componentAttachmentDao")
    @Autowired
    private IStringIdDao&lt;ComponentAttachment&gt; componentAttachmentDao;
    @Qualifier("componentUseAttachmentDao")
    @Autowired
    private IStringIdDao&lt;ComponentUseAttachment&gt; 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&lt;ApplicationAttachment&gt; 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(&lt;generated&gt;)
    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(&lt;generated&gt;)
    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 (&#39;create&#39;, &#39;2011-02-11 17:30:36.640000 +05:30:00&#39;, NULL, &#39;&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;&lt;changeDetail&gt;&lt;changes&gt;&lt;key&gt;description&lt;/key&gt;&lt;post&gt;a test application&lt;/post&gt;&lt;/changes&gt;&lt;changes&gt;&lt;key&gt;name&lt;/key&gt;&lt;post&gt;testapplication&lt;/post&gt;&lt;/changes&gt;&lt;changes&gt;&lt;key&gt;locked&lt;/key&gt;&lt;post&gt;false&lt;/post&gt;&lt;/changes&gt;&lt;changes&gt;&lt;key&gt;useExportstatus&lt;/key&gt;&lt;post&gt;false&lt;/post&gt;&lt;/changes&gt;&lt;changes&gt;&lt;key&gt;useProtexstatus&lt;/key&gt;&lt;post&gt;false&lt;/post&gt;&lt;/changes&gt;&lt;changes&gt;&lt;key&gt;version&lt;/key&gt;&lt;post&gt;1.0&lt;/post&gt;&lt;/changes&gt;&lt;/changeDetail&gt;&#39;, &#39;27767&#39;, &#39;testapplication&#39;, &#39;Application&#39;, &#39;15590&#39;) 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>
Posted
Updated 12-Feb-11 4:19am
v4
Comments
Nagy Vilmos 12-Feb-11 10:19am    
Edited to make the original class readable.

1 solution

A quick google and it looks like you are doing something that violates a constraint on the DB.
So my best advice would be to remember that debugging is your friend, step through the code and find the magic line where it goes *boom*.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900