[hibernate-issues] [JIRA] (HHH-14078) Hibernate returns duplicates into @OneToMany collection after merge

Viktar Ramashko (JIRA) jira at hibernate.atlassian.net
Fri Jun 19 04:28:18 EDT 2020


Viktar Ramashko ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5d66387b64bfb10c11a26d27 ) *created* an issue

Hibernate ORM ( https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiM2M0ZmI2ZDNhZWJhNDE4YjgzOTNiOTk1Yjg4Yjg1MjQiLCJwIjoiaiJ9 ) / Bug ( https://hibernate.atlassian.net/browse/HHH-14078?atlOrigin=eyJpIjoiM2M0ZmI2ZDNhZWJhNDE4YjgzOTNiOTk1Yjg4Yjg1MjQiLCJwIjoiaiJ9 ) HHH-14078 ( https://hibernate.atlassian.net/browse/HHH-14078?atlOrigin=eyJpIjoiM2M0ZmI2ZDNhZWJhNDE4YjgzOTNiOTk1Yjg4Yjg1MjQiLCJwIjoiaiJ9 ) Hibernate returns duplicates into @OneToMany collection after merge ( https://hibernate.atlassian.net/browse/HHH-14078?atlOrigin=eyJpIjoiM2M0ZmI2ZDNhZWJhNDE4YjgzOTNiOTk1Yjg4Yjg1MjQiLCJwIjoiaiJ9 )

Issue Type: Bug Affects Versions: 5.4.9, 5.4.17 Assignee: Unassigned Created: 19/Jun/2020 01:28 AM Labels: bag collection duplicates one-to-many proxy Priority: Major Reporter: Viktar Ramashko ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5d66387b64bfb10c11a26d27 )

Consider following entities (getters and setters are omitted for brevity):

@Entity
public class Parent
{
	@Id
	@GeneratedValue
	private int id;

	@OneToMany(mappedBy = "parent" , orphanRemoval = true , cascade = CascadeType.ALL)
       @BatchSize(size = 20)
	private List<Child> children = new ArrayList<Child>();

      public void addChild(Child child) {
       child.setParent( this );
       this.children.add(child);
     }
}

@Entity
public class Child
{
	@Id
	@GeneratedValue
	private int id;

	@ManyToOne(optional = false , fetch = FetchType.LAZY)
       @JoinColumn(name = "parent_id" , referencedColumnName = "id" , nullable = false , updatable = false )
	private Parent parent;
}

We have already saved the parent in the database and want to add a child to it.
We use Spring Data JPA and test is written with it.

       Parent existingParent = parentRepository.findById(PARENT_ID).get();
       Child newChild= new Child ();
       existingParent .addChild(newChild);
       //when
       Parent savedParent  = parentRepository.save(existingParent);

After that hibernate perform Merge operation on Parent entity and Cascade merge on Child collection. We expect that savedParent will have one element in children collection, but it contains two the same elements. In database created only one.

This example works fine.

       Parent existingParent = parentRepository.findById(PARENT_ID).get();
       Child newChild= new Child ();
       existingParent.getChildren().size();
       existingParent .addChild(newChild);
       //when
       Parent savedParent  = parentRepository.save(existingParent);

After debugging I found out, that in Class DefaultMergeEventListener

protected void entityIsPersistent(MergeEvent event, Map copyCache) {
		LOG.trace( "Ignoring persistent instance" );

		//TODO: check that entry.getIdentifier().equals(requestedId)

		final Object entity = event.getEntity();
		final EventSource source = event.getSession();
		final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );

		( (MergeContext) copyCache ).put( entity, entity, true );  //before cascade!

		cascadeOnMerge( source, persister, entity, copyCache );
               // here we have two elements in collection, one Child that has been persisted in database, and the same Child object, before it persisted in database.
		copyValues( persister, entity, entity, source, copyCache );
               // here we have two the same Child object that have been persisted in database
		event.setResult( entity );
	}

( https://hibernate.atlassian.net/browse/HHH-14078#add-comment?atlOrigin=eyJpIjoiM2M0ZmI2ZDNhZWJhNDE4YjgzOTNiOTk1Yjg4Yjg1MjQiLCJwIjoiaiJ9 ) Add Comment ( https://hibernate.atlassian.net/browse/HHH-14078#add-comment?atlOrigin=eyJpIjoiM2M0ZmI2ZDNhZWJhNDE4YjgzOTNiOTk1Yjg4Yjg1MjQiLCJwIjoiaiJ9 )

Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.core&referrer=utm_source%3DNotificationLink%26utm_medium%3DEmail ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailNotificationLink&mt=8 ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100130- sha1:5f5b10c )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/hibernate-issues/attachments/20200619/e1cc2244/attachment.html 


More information about the hibernate-issues mailing list