Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » NPE merging @ManyToOne entity
NPE merging @ManyToOne entity [message #1273148] Wed, 19 March 2014 11:53 Go to next message
Risto Oikarinen is currently offline Risto Oikarinen
Messages: 2
Registered: March 2014
Junior Member
I'm working with two linked entities using EclipseLink 2.5.1:

	
public class PermissionGroup {

   public final static String	SITES  = "sites";
   @ManyToMany(fetch = FetchType.LAZY)
   private Set<Site> sites = new LinkedHashSet<Site>();
}

public class Site {

   public final static String	PERMISSION_GROUPS = "permissiongroup";
   @ManyToMany(fetch = FetchType.LAZY, mappedBy = PermissionGroup.SITES)
   private Set<PermissionGroup> permissiongroup = new LinkedHashSet<PermissionGroup>();
}


I am trying to merge some changes to PermissionGroup.sites like this:

				EntityManager em = getEntityManager();
				T entity = em.find(getEntityClassMetadata().getMappedClass(), entityId);
				if (entity != null) {
					// make sure we are working with the latest versions
					em.refresh(entity);
					getEntityClassMetadata().setPropertyValue(entity, propertyName, propertyValue);
					// re-attach also referenced entities to the persistence
					// context
					entity = em.merge(entity);
					em.flush();
					entityA[0] = detachEntity(entity);
				}


This homever ends up in a NPE:

java.lang.NullPointerException
	at org.eclipse.persistence.internal.sessions.MergeManager.registerObjectForMergeCloneIntoWorkingCopy(MergeManager.java:1047)
	at org.eclipse.persistence.internal.sessions.MergeManager.mergeChangesOfCloneIntoWorkingCopy(MergeManager.java:557)
	at org.eclipse.persistence.internal.sessions.MergeManager.mergeChanges(MergeManager.java:313)
	at org.eclipse.persistence.mappings.CollectionMapping.mergeIntoObject(CollectionMapping.java:1599)
	at org.eclipse.persistence.internal.descriptors.ObjectBuilder.mergeIntoObject(ObjectBuilder.java:4128)
	at org.eclipse.persistence.internal.sessions.MergeManager.mergeChangesOfCloneIntoWorkingCopy(MergeManager.java:594)
	at org.eclipse.persistence.internal.sessions.MergeManager.mergeChanges(MergeManager.java:313)
	at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.mergeCloneWithReferences(UnitOfWorkImpl.java:3521)
	at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.mergeCloneWithReferences(RepeatableWriteUnitOfWork.java:384)
	at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.mergeCloneWithReferences(UnitOfWorkImpl.java:3481)
	at org.eclipse.persistence.internal.jpa.EntityManagerImpl.mergeInternal(EntityManagerImpl.java:542)
	at org.eclipse.persistence.internal.jpa.EntityManagerImpl.merge(EntityManagerImpl.java:519)
	at org.my.project.web.ui.component.jpacontainer.MutableLocalEntityProvider$1.run(MutableLocalEntityProvider.java:38)
	at com.vaadin.addon.jpacontainer.provider.MutableLocalEntityProvider.runInTransaction(MutableLocalEntityProvider.java:120)
	at org.my.project.web.ui.component.jpacontainer.MutableLocalEntityProvider.updateEntityProperty(MutableLocalEntityProvider.java:26)
	at org.my.project.web.ui.component.jpacontainer.CachingMutableLocalEntityProvider.updateEntityProperty(CachingMutableLocalEntityProvider.java:25)
	at com.vaadin.addon.jpacontainer.JPAContainer.containerItemPropertyModified(JPAContainer.java:1222)
	at com.vaadin.addon.jpacontainer.JPAContainerItem$ItemProperty.setValue(JPAContainerItem.java:289)
	at com.vaadin.data.util.TransactionalPropertyWrapper.setValue(TransactionalPropertyWrapper.java:80)
	at com.vaadin.ui.AbstractField.setValue(AbstractField.java:508)
	at com.vaadin.ui.AbstractSelect.setValue(AbstractSelect.java:698)
	at com.vaadin.ui.OptionGroup.setValue(OptionGroup.java:188)
	at org.my.project.web.ui.selector.SiteMultiSelector.setValue(SiteMultiSelector.java:59)
	at com.vaadin.ui.AbstractSelect.changeVariables(AbstractSelect.java:494)
	at com.vaadin.ui.OptionGroup.changeVariables(OptionGroup.java:81)
	at com.vaadin.server.communication.ServerRpcHandler.changeVariables(ServerRpcHandler.java:396)
	at com.vaadin.server.communication.ServerRpcHandler.handleBurst(ServerRpcHandler.java:221)
	at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:111)
	at com.vaadin.server.communication.PushHandler$2.run(PushHandler.java:160)
	at com.vaadin.server.communication.PushHandler.callWithUi(PushHandler.java:278)
	at com.vaadin.server.communication.PushHandler.onRequest(PushHandler.java:347)
	at org.atmosphere.cpr.AsynchronousProcessor.action(AsynchronousProcessor.java:260)
	at org.atmosphere.cpr.AsynchronousProcessor.suspended(AsynchronousProcessor.java:167)
	at org.atmosphere.container.TomcatWebSocketUtil.doService(TomcatWebSocketUtil.java:137)
	at org.atmosphere.container.Tomcat7AsyncSupportWithWebSocket.service(Tomcat7AsyncSupportWithWebSocket.java:59)
	at org.atmosphere.cpr.AtmosphereFramework.doCometSupport(AtmosphereFramework.java:1464)
	at org.atmosphere.websocket.DefaultWebSocketProcessor.dispatch(DefaultWebSocketProcessor.java:219)
	at org.atmosphere.websocket.DefaultWebSocketProcessor$2.run(DefaultWebSocketProcessor.java:183)
	at org.atmosphere.util.VoidExecutorService.execute(VoidExecutorService.java:101)
	at org.atmosphere.websocket.DefaultWebSocketProcessor.dispatch(DefaultWebSocketProcessor.java:178)
	at org.atmosphere.websocket.DefaultWebSocketProcessor.invokeWebSocketProtocol(DefaultWebSocketProcessor.java:167)
	at org.atmosphere.container.TomcatWebSocketHandler.onTextMessage(TomcatWebSocketHandler.java:92)
	at org.apache.catalina.websocket.MessageInbound.onTextData(MessageInbound.java:74)
	at org.apache.catalina.websocket.StreamInbound.doOnTextData(StreamInbound.java:187)
	at org.apache.catalina.websocket.StreamInbound.onData(StreamInbound.java:135)
	at org.apache.coyote.http11.upgrade.UpgradeProcessor.upgradeDispatch(UpgradeProcessor.java:83)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:587)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1686)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:744)


I am using EL through Vaadin framework and carefully checking for nulls before calling merge.

I browsed around, but didn't really find any useful resources... similar case has been discussed in this thread, but there was no conclusion to it.

Any points highly appreciated!

Greetings,

Risto Oikarinen

Re: NPE merging @ManyToOne entity [message #1274569 is a reply to message #1273148] Fri, 21 March 2014 12:54 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1017
Registered: July 2009
Senior Member
You should probably not be changing the collection out from the entity, and instead add or remove from the existing collection within the entity - changing the collection means change tracking cannot work. Try using the latest EclipseLink version, or try turning off change tracking bu setting persistence property "eclipselink.weaving.changetracking" to false.

I also don't think merge should be needed here, as the entity being merged is already apart of the context. The entities within the collection will not have merge cascaded to them anyway.
Re: NPE merging @ManyToOne entity [message #1276170 is a reply to message #1274569] Mon, 24 March 2014 03:22 Go to previous message
Risto Oikarinen is currently offline Risto Oikarinen
Messages: 2
Registered: March 2014
Junior Member
Hi Chris, and thanks for your reply!

I solved the problem. The Vaadin framework was incorrectly configured on this instance, trying to merge a wrong datatype (ie. not a collection, or LinkedHashSet in my case) into the @ManyToOne entity. The Vaadin-specific solution was to add a MultiSelectConverter to the Vaadin selector component, which then supplied proper Collection for merge.

I also tried "eclipselink.weaving.changetracking" as you suggested, but it didn't have effect here, since the problem was about supplied datatype and not the merge itself.
The currently working implementation is indeed replacing the whole collection in the @ManyToOne entity instead of adding/removing items. This is obviously non-optimal or might cause problems later on, but then I will have to live with it.

Greetings,

Risto Oikarinen
Previous Topic:Multi tenancy - NullPointerException
Next Topic:History for a OneToMany relationship not working
Goto Forum:
  


Current Time: Thu Aug 28 09:18:09 EDT 2014

Powered by FUDForum. Page generated in 0.01581 seconds