Getting an entity with composite ID containing a detached entity fails if PersistenceContext contains a managed copy

Description

I am getting an exception: "Caused by: javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session: [com.efl.mobility.puma.dao.model.content.ProductDL#ukdipabk]" when I get (find method) an entity by its compound key. This exception only be thrown when one entity, at least, of compound key is previously loaded in session.

Simple Entity 1:

CustomerDL.java

@Entity(name="Customer") public class CustomerDL implements Serializable { private long customerId; ... }

Simple entity 2:

ProductDL.java

@Entity @Table(name="CNT_PRODUCT") public class ProductDL extends Auditory implements Serializable { private String productCod; ... }

Entity with compound key:

SubscriptionDL.java

public class SubscriptionDL implements Serializable { private SubscriptionIdDL subscriptionId; @EmbeddedId public SubscriptionIdDL getSubscriptionId() { return subscriptionId; } public void setSubscriptionId(SubscriptionIdDL subscriptionId) { this.subscriptionId = subscriptionId; } }

Embedded id:

SubscriptionIdDL.java

public class SubscriptionIdDL implements Serializable { private CustomerDL customer; private ProductDL product; @OneToOne @JoinColumn(name="CUS_ID", referencedColumnName="CUS_ID", nullable=false) public CustomerDL getCustomer() { return customer; } public void setCustomer(CustomerDL customer) { this.customer = customer; } @ManyToOne @JoinColumn(name="CNT_PRODUCT_COD", referencedColumnName="CNT_PRODUCT_COD", nullable=false) public ProductDL getProduct() { return product; } public void setProduct(ProductDL product) { this.product = product; } }

Steps to reproduce the exception:

1) Load with session find (by id) method an entity of type CustomerDL.
2) Create an entity (also fails if this entity is loaded from session) of type ProductDL and fill its id.
3) Create compound key (SubscriptionIdDL) with (1) and (2) entities.
4) Load with session find (by id) method an entity of type SubscriptionDL using (3) generated Key.

Example (using a service layer):

test.java

ProductSL productSL=productService.get("ukdipabk"); CustomerSL customerSL2=new CustomerSL(); customerSL2.setCustomerId(2L); subscriptionIdSL=new SubscriptionIdSL(); subscriptionIdSL.setProduct(productSL); subscriptionIdSL.setCustomer(customerSL2); subscriptionService.get(subscriptionIdSL); //launch "already associated exception"

emphasis NOTE: all code executed in one transaction so one hibernate session.

In Hibernate 4 version using exactly same code this work fine.

Stacktrace:

25 ene 2016 17:02:55,316 ERROR [http-nio-8080-exec-2] TransactionInterceptor.completeTransactionAfterThrowing(526) | Application exception overridden by commit exception
com.efl.mobility.puma.business.exceptions.PumaBusinessGeneralException: [com.efl.mobility.puma.business.impl.content.ContentPumaTADBusinessImpl and country: null
at com.efl.mobility.puma.business.impl.content.ContentPumaTADBusinessImpl.getAllProductsSubscribedByCustomer(ContentPumaTADBusinessImpl.java:175)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
at com.sun.proxy.$Proxy50.getAllProductsSubscribedByCustomer(Unknown Source)
at com.efl.mobility.puma.ws.impl.BasePumaContentWebService.getProducts(BasePumaContentWebService.java:315)
at com.efl.mobility.puma.ws.impl.PumaContentWebServiceImpl.ping(PumaContentWebServiceImpl.java:1367)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:181)
at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:97)
at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:200)
at org.apache.cxf.jaxrs.JAXRSInvoker.invoke(JAXRSInvoker.java:99)
at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:59)
at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:96)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:307)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:251)
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:234)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:208)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:160)
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:171)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:293)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:217)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:268)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.efl.mobility.puma.service.exception.PumaServiceException: Error getting object with id SubscriptionIdSL [customer=CustomerSL [customerId=2, email=null, customerExternalCode=null, application=null, username=null], product=ProductSL [productCod=ukdipabk, productName=The Complete Dismissal Procedures]] from class com.efl.mobility.puma.services.domain.customer.SubscriptionSL
at com.efl.mobility.puma.service.GenericPumaServiceImpl.get(GenericPumaServiceImpl.java:73)
at com.efl.mobility.puma.service.GenericPumaServiceImpl.get(GenericPumaServiceImpl.java:48)
at com.efl.mobility.puma.service.GenericPumaServiceImpl.get(GenericPumaServiceImpl.java:18)
at com.efl.mobility.puma.business.impl.content.ContentPumaTADBusinessImpl.getAllProductsSubscribedByCustomer(ContentPumaTADBusinessImpl.java:172)
... 63 more
Caused by: com.efl.mobility.puma.dao.exception.PumaDaoException: Error getting entity of class class com.efl.mobility.puma.dao.model.customer.SubscriptionDL for id SubscriptionIdDL [customer=CustomerDL [customerId=2, username=null], product=ProductDL [productCod=ukdipabk, productName=The Complete Dismissal Procedures]]
at com.efl.mobility.puma.dao.jpa.GenericPumaJpaDao.get(GenericPumaJpaDao.java:73)
at com.efl.mobility.puma.service.GenericPumaServiceImpl.get(GenericPumaServiceImpl.java:61)
... 66 more
Caused by: javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [com.efl.mobility.puma.dao.model.content.ProductDL#ukdipabk]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1664)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1619)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1106)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1033)
at sun.reflect.GeneratedMethodAccessor93.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:293)
at com.sun.proxy.$Proxy42.find(Unknown Source)
at com.efl.mobility.puma.dao.jpa.GenericPumaJpaDao.get(GenericPumaJpaDao.java:52)
... 67 more

Attachments

1
  • 25 Jan 2016, 04:47 PM

Activity

Show:

Former user July 22, 2017 at 9:04 PM

, this was fixed in 5.0.10, 5.1.1, and 5.2.0. If you are still having problems with any of those versions, then please create a new jira issue.

csplrji July 22, 2017 at 4:06 AM

This issue comes in 5.0.2 as well

Former user May 27, 2016 at 11:17 PM

Fixed in master, 5.1, and 5.0 branches.

Former user May 3, 2016 at 9:35 PM

I've incorporated the tests from the pull request, added more tests and a fix into a new commit pushed to my fork: https://github.com/gbadner/hibernate-core/commits/HHH-10476-master

Former user February 25, 2016 at 6:53 AM

I've create a pull request with a FailureExpected unit test that reproduces this issue: https://github.com/hibernate/hibernate-orm/pull/1275

Fixed

Details

Assignee

Reporter

Labels

Fix versions

Affects versions

Priority

Created January 25, 2016 at 4:14 PM
Updated July 22, 2017 at 9:04 PM
Resolved May 27, 2016 at 11:17 PM