cancel
Showing results for 
Search instead for 
Did you mean: 

Datahub: LazyInitializationException when obtaining target items

ncmg
Explorer
0 Kudos

Problem

I'm getting the following Exception:

 org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.hybris.datahub.domain.jpa.entities.TargetSystemEntity.targetItemMetadataEntities, could not initialize proxy - no Session

The Exception occurs when trying to obtain all target items in an AdapterService:

 publicationActionService.findTargetItemsByPublication(publicationId, targetItemType, pageable);

I get the same Exception with the following deprecated code as well:

 targetSystemPublication.getTargetItems();

Any ideas where to start hunting this down?

Stack Trace

 org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.hybris.datahub.domain.jpa.entities.TargetSystemEntity.targetItemMetadataEntities, could not initialize proxy - no Session
     at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:575) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
     at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:214) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
     at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:155) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
     at org.hibernate.collection.internal.PersistentSet.size(PersistentSet.java:160) ~[hibernate-core-4.3.6.Final.jar:4.3.6.Final]
     at com.hybris.datahub.service.impl.DefaultPublicationActionService.getSubTargetSystemPublication(DefaultPublicationActionService.java:287) ~[datahub-service-5.7.0.2-RC1.jar:5.7.0.2-RC1]
     at com.hybris.datahub.service.impl.DefaultPublicationActionService.getTargetSystemPublication(DefaultPublicationActionService.java:308) ~[datahub-service-5.7.0.2-RC1.jar:5.7.0.2-RC1]
     at com.hybris.datahub.service.impl.DefaultPublicationActionService.findTargetItemsByPublication(DefaultPublicationActionService.java:336) ~[datahub-service-5.7.0.2-RC1.jar:5.7.0.2-RC1]
     at sun.reflect.GeneratedMethodAccessor2445.invoke(Unknown Source) ~[na:na]
     at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_66]
     at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) ~[spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]
     at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:201) ~[spring-aop-4.1.5.RELEASE.jar:4.1.5.RELEASE]
     at com.sun.proxy.$Proxy134.findTargetItemsByPublication(Unknown Source) ~[na:na]

Environment

  • Datahub 5.7.0.2

  • affects MySQL and HSQLDB

    all tables dropped, fresh install

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Did you obtain the target items inside a transaction?

rafal_gdowski
Active Contributor
0 Kudos

Data Hub 5.7 is the first minor version using JPA, hence the transactional scope changes.

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi, I am facing the same issue with Hybris 5.7.0.3 Is there any update or workaround on this issue, on how to manage the JPA transactions in AdapterService?

ncmg
Explorer
0 Kudos

Just execute your call to the service inside a Spring TX transaction, something like this:

 TransactionTemplate template = new TransactionTemplate(transactionManager);
 template.execute((status) -> {
     ... query all target items here ...
 });
Former Member
0 Kudos

Is this required for OOTB implementation? I am getting same error with no customization and trying to replicate B2B customers via Idocs and REST services. Which file needs to be changed to get this working?

0 Kudos

Or instead of using TransactionTemplate, you could just define a separate bean that will fetch your TargetItems through the Publication Service and annotate that bean with @Transaction(propagation = Propagation.REQUIRED).

This way, you could just inject this bean into your adapterService class and correctly handle the publish method.

Former Member
0 Kudos

All documentation has been updated to include cross references to other documents and notations where appropriate.

ncmg
Explorer
0 Kudos

One needs to be aware of transactions:

This page should be linked in the Implementation guide as well! There is a document on how to implement an AdapterService, but no hint on transactions: