transaction template

I wrote a test class for an entity that uses HibernateTemplate with a callback (see code below). Everything runs fine.

 

When I replace the HibernateTemplate by a TransactionTemplate I get this exception:

org.hibernate.InstantiationException: Cannot instantiate abstract class or interface: org.popsuite.hr.model.Contract at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:78) at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:100) at org.hibernate.tuple.entity.AbstractEntityTuplizer.instantiate(AbstractEntityTuplizer.java:351) at org.hibernate.persister.entity.AbstractEntityPersister.instantiate(AbstractEntityPersister.java:3606) at org.hibernate.impl.SessionImpl.instantiate(SessionImpl.java:1275) at org.hibernate.impl.SessionImpl.instantiate(SessionImpl.java:1264) at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1299) at org.hibernate.loader.Loader.getRow(Loader.java:1206) at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580) at org.hibernate.loader.Loader.doQuery(Loader.java:701) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236) at org.hibernate.loader.Loader.doList(Loader.java:2220) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104) at org.hibernate.loader.Loader.list(Loader.java:2099) at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378) at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338) at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121) at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79) at org.popsuite.hr.model.service.ContractServiceDelegateIntegrationTest.createPayslip(ContractServiceDelegateIntegrationTest.java:78) at org.popsuite.hr.model.service.ContractServiceDelegateIntegrationTest.access$1(ContractServiceDelegateIntegrationTest.java:69) at org.popsuite.hr.model.service.ContractServiceDelegateIntegrationTest$2.doInTransactionWithoutResult(ContractServiceDelegateIntegrationTest.java:123) at org.springframework.transaction.support.TransactionCallbackWithoutResult.doInTransaction(TransactionCallbackWithoutResult.java:33) at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:128) at org.popsuite.hr.model.service.ContractServiceDelegateIntegrationTest.testCreatePayslipWithTransactionTemplate(ContractServiceDelegateIntegrationTest.java:126) 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.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethodRunner.java:99) at org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRunner.java:81) at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) at org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.java:75) at org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45) at org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestClassMethodsRunner.java:66) at org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRunner.java:35) at org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRunner.java:42) at org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAfterRunner.java:34) at org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

 

Any idea?

 

The code: 

   private void createPayslip(Session session) {
        
        Query query = session.createQuery("select contract from Contract contract where contract.worker.branch.name = ?");
        query.setString(0, "Design2See");
        @SuppressWarnings("unchecked")
        List<Contract> contracts = query.list();
        Date refDate = new LocalDate(2007, 10, 15).toDateMidnight().toDate();
        Contract contract = contracts.get(0);

        assertEquals("Initial payslip list size should be one.", 1, contract.getPayslips().size());
        Set<Payslip> result = contract.createPayslips(refDate);
        assertEquals("Wrong number of new payslips", 1,  result.size());
        assertEquals("Wrong number of contract's payslips", 2,  contract.getPayslips().size());
    }
    
    @Test
    public void testCreatePayslipWithHibernateTemplate()
    {
        HibernateCallback callback = new HibernateCallback() {
            public Object doInHibernate(Session session) {
                createPayslip(session);
                return null;
            }
        };
        getHibernateTemplate().execute(callback);
    }
    
    @Test
    public void testCreatePayslipWithTransactionTemplate()
    {
        TransactionCallback callback = new TransactionCallbackWithoutResult() {
            public void doInTransactionWithoutResult(TransactionStatus status) {
                createPayslip();
            }
        };
        getTransactionTemplate().execute(callback);
    }

[OT] rich text editor and preview

I haven't find how to use the forum editor to insert nicely code. Either :
- the end of lines are all removed(see previous message)
- the java generic tag are removed
By the way, how to insert code tag from the rich text editor: there are no buttom to do it...
And when I do it from the simple text editor the result is a mess
 

[OT] rich text editor and preview

The solution I found for the moment (see your post reformatted) is to edit it using the HTML button and include your code into "pre" tags without anything in between. Everything seems to be preserved except indentation. I will check to see if there is any better way available. BTW, your stacktrace copy/paset is OK. So it might relate to th EOL character used in the copy source.
Regards,
Vincent

[OT] rich text editor and preview

Thanks.
In the reformatted post the java generic tags are still missing as for example in:
List<Contract> contracts = query.list();
About EOL: I tried to replace windows eol by linux eol, with the same result.
 

[OT] rich text editor and preview

Got it. It should be fine now. It was linked to HTML filtering for text input by users. Default behaviour has been changed so that copy/paste of anything will preserve formatting and content. Tell me if you still encounter any pb.

Vincent

transaction template

The error you get comes from the fact that Hibernate doesn't know how to instanciate your entity before hydrating it from the DB resultset. Jspresso entities are not POJOS but j2se proxies and cannot be instanciated with the default hibernate strategy (e.g. Class.newInstance()), but a Jspresso entity factory must be used. This means that your hibernate session must be aware of it by being registerd an interceptor that takes care of the basic entity instances lifecycle (among other things).

  • Using plain hibernate, you achieve this using org.jspresso.framework.model.persistence.hibernate.EntityProxyInterceptor (or a subclass of it) as hibernate session interceptor. This instance must be configured with the jspresso entity factory to use.
  • Using Spring, you register the interceptor : - hibernateTemplate.setEntityInterceptor(entityInterceptor) - ((HibernateTransactionManager) transactionTemplate.getTransactionManager()).setEntityInterceptor(entityInterceptor). For the latest, it assumes that the underlying transaction manager is an HibernateTransactionManager.

    In Jspresso, everything is done behing the hood when creating an HibernateBackendController (see the method linkHibernateArtifacts()). The AbstractTestDataPersister constructor also performs the hibernate template configuration step. So I think that in your test case, the hibernate template bust be correctly configured but the transaction template might not.
 
Hope this helps.
Vincent

transaction template

Thanks.
Everything works fine now.