Sporadic "Caused by: org.hibernate.HibernateException: Found shared references to a collection" For K6 load testing

I am getting this exception sporadically when I’m running-related API with K6 load testing with multiple users

Caused by: java.lang.reflect.InvocationTargetException
	at sun.reflect.GeneratedMethodAccessor695.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at se.test.nova.proxy.impl.TransactionInvocationHandler.invokeMethodOnWrappedObject(TransactionInvocationHandler.java:63)
	... 72 more
Caused by: org.hibernate.HibernateException: Found shared references to a collection: se.test.platform.cc.unit.impl.PatientImpl.addresses
	at org.hibernate.engine.internal.Collections.processReachableCollection(Collections.java:187)
	at org.hibernate.event.internal.FlushVisitor.processCollection(FlushVisitor.java:49)
	at org.hibernate.event.internal.AbstractVisitor.processValue(AbstractVisitor.java:104)
	at org.hibernate.event.internal.AbstractVisitor.processValue(AbstractVisitor.java:65)
	at org.hibernate.event.internal.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:59)
	at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:179)
	at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:235)
	at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:94)
	at org.hibernate.event.internal.DefaultDirtyCheckEventListener.onDirtyCheck(DefaultDirtyCheckEventListener.java:35)
	at org.hibernate.internal.SessionImpl.isDirty(SessionImpl.java:1449)
	at se.test.platform.sdk.server.entity.dto.mapper.spi.AbstractMapper.enterFlush(AbstractMapper.java:106)
	at se.test.platform.sdk.server.entity.dto.mapper.spi.AbstractDTOMapper.convert(AbstractDTOMapper.java:36)
	at se.test.fluidbalance.common.util.FluidSubstanceCollectionDTOBuilder.build(FluidSubstanceCollectionDTOBuilder.java:67)
	at se.test.fluidbalance.service.impl.FluidSubstanceServiceImpl.findRegisteredFluidSubstanceCollection(FluidSubstanceServiceImpl.java:67)

Sample ER

This is how the mapping is done,

FluidSubstance.class

  @org.hibernate.annotations.Fetch(org.hibernate.annotations.FetchMode.SELECT)
  @javax.persistence.ManyToOne(targetEntity = se.test.platform.cc.patient.impl.PatientImpl.class,
                               optional = false,
                               fetch = javax.persistence.FetchType.LAZY)

PatientImpl.class

 @NotNull
    @OneToMany(
        targetEntity = PatientAddressImpl.class,
        fetch = FetchType.LAZY,
        mappedBy = "patient",
        cascade = {CascadeType.ALL},
        orphanRemoval = true
    )
    private Set<PatientAddress> addresses = new HashSet();

Hibernate version details

org.hibernate
hibernate-entitymanager
5.3.20.Final-redhat-00001

Could anybody help me with this, please??
Thanks in advance!

Usually this means that there are multiple “managed collections” around pointing to the same collection key. IMO this can only happen if your @ManyToOne uses some kind of @JoinColumn that refers to a non-unique column of PatientImpl. Can you show the full mapping? Also, some more debugging would be needed if my assumption is wrong. Can you put a breakpoint where the exception is thrown and try to find out how the two collection instances are reachable?

Hi beikov, Thanks for the quick response. I could not find a mapping @ManyToOne that uses a @JoinColumn with a non-unique column in PatientImpl. This the full mapping of classes

@javax.persistence.MappedSuperclass
@se.test.platform.sdk.server.entity.ExternalEntity
@javax.persistence.Inheritance(strategy = javax.persistence.InheritanceType.JOINED)
@javax.persistence.PrimaryKeyJoinColumns({ @javax.persistence.PrimaryKeyJoinColumn(name = "id",
                                                                                   referencedColumnName = "id") })
@javax.persistence.Access(javax.persistence.AccessType.FIELD)
public abstract class AbstractFluidSubstance extends se.test.platform.sdk.server.entity.AbstractContainer
  implements se.test.fluidbalance.spi.FluidSubstanceSpi, se.test.platform.sdk.server.entity.Lifecycle
{
  @javax.persistence.GeneratedValue(generator = "se.test.fluidbalance.impl.FluidSubstanceImpl")
  @org.hibernate.annotations.GenericGenerator(name = "se.test.fluidbalance.impl.FluidSubstanceImpl",
                                              strategy = "se.test.platform.sdk.server.entity.persistence.hibernate.TableHiLoGenerator",
                                              parameters = {
                                                            @org.hibernate.annotations.Parameter(name = "initial_value",
                                                                                                 value = "1"),
                                                            @org.hibernate.annotations.Parameter(name = "increment_size",
                                                                                                 value = "100"),
                                                            @org.hibernate.annotations.Parameter(name = "optimizer",
                                                                                                 value = "se.test.fluidbalance.impl.hibernate.FluidSubstanceIdOptimizer"),
                                                            @org.hibernate.annotations.Parameter(name = "segment_value",
                                                                                                 value = "se.test.fluidbalance.FluidSubstance") })
  @org.hibernate.annotations.Type(type = "se.test.fluidbalance.impl.hibernate.FluidSubstanceIdType")
  @javax.persistence.Id
  private se.test.fluidbalance.FluidSubstance.Id id;
  @javax.persistence.Version
  private long version;

  protected se.test.fluidbalance.impl.FluidSubstanceBeans getBeans()
  {
    return (se.test.fluidbalance.impl.FluidSubstanceBeans) se.test.platform.sdk.server.entity.beans.EntityBeansMapper
      .getEntityBeansStatically(se.test.fluidbalance.impl.FluidSubstanceImpl.class);
  }

  @org.hibernate.annotations.Fetch(org.hibernate.annotations.FetchMode.SELECT)
  @javax.persistence.ManyToOne(targetEntity = se.test.platform.cc.patient.impl.PatientImpl.class,
                               optional = false,
                               fetch = javax.persistence.FetchType.LAZY)
  @javax.validation.constraints.NotNull
  private se.test.platform.cc.patient.Patient patient;
  @org.hibernate.annotations.Columns(columns = { @javax.persistence.Column(name = "mesureDateTime", nullable = false) })
  @javax.validation.constraints.NotNull
  @org.hibernate.annotations.Type(type = "se.test.platform.sdk.server.entity.persistence.hibernate.PersistentDateTime")
  private org.joda.time.DateTime mesureDateTime;
  @javax.persistence.Basic
  @javax.persistence.Column(nullable = true, length = 1000)
  @javax.validation.constraints.Size(max = 1000)
  private String comment;
  @javax.persistence.Embedded
  @javax.validation.Valid
  @org.hibernate.annotations.Target(se.test.platform.cc.stereotype.signable.impl.SigningComponentImpl.class)
  @javax.persistence.Column(nullable = true, updatable = false)
  @javax.persistence.AttributeOverrides({
                                         @javax.persistence.AttributeOverride(name = "signingStatus",
                                                                              column = @javax.persistence.Column(name = "statusComponent_signingStatus",
                                                                                                                 nullable = true)),
                                         @javax.persistence.AttributeOverride(name = "invalidation",
                                                                              column = @javax.persistence.Column(name = "statusComponent_invalidation",
                                                                                                                 nullable = true)),
                                         @javax.persistence.AttributeOverride(name = "signable",
                                                                              column = @javax.persistence.Column(name = "statusComponent_signable",
                                                                                                                 nullable = true)),
                                         @javax.persistence.AttributeOverride(name = "signings",
                                                                              column = @javax.persistence.Column(name = "statusComponent_signings",
                                                                                                                 nullable = true)) })
  @javax.persistence.AssociationOverrides({ @javax.persistence.AssociationOverride(name = "signings",
                                                                                   joinTable = @javax.persistence.JoinTable(name = "FluidSubstance_statusComponent_signings",
        

Hard to say what is going on the. We would need a reproducer test case(hibernate-test-case-templates/JPAUnitTestCase.java at main · hibernate/hibernate-test-case-templates · GitHub) to look into the issue more deeply.