HHH-14061 - reproducing test

This commit is contained in:
VladoKuruc 2020-06-05 10:36:33 +02:00 committed by a10804
parent 5963dc7fe8
commit e45ac9f0f4
3 changed files with 137 additions and 2 deletions

View File

@ -7,11 +7,16 @@
package org.hibernate.envers.test.entities.onetomany;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import org.hibernate.envers.Audited;
import org.hibernate.envers.RelationTargetAuditMode;
/**
* ReferencEd entity
@ -27,8 +32,12 @@ public class SetRefEdEntity {
private String data;
@Audited
@OneToMany(mappedBy = "reference")
@OneToMany(fetch = FetchType.LAZY, mappedBy = "reference", cascade = CascadeType.ALL)
private Set<SetRefIngEntity> reffering;
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
@ManyToOne(fetch = FetchType.LAZY)
private SetRefIngEntity ref;
public SetRefEdEntity() {
}
@ -66,6 +75,14 @@ public class SetRefEdEntity {
this.reffering = reffering;
}
public SetRefIngEntity getRef() {
return ref;
}
public void setRef(SetRefIngEntity ref) {
this.ref = ref;
}
public boolean equals(Object o) {
if ( this == o ) {
return true;

View File

@ -7,6 +7,7 @@
package org.hibernate.envers.test.entities.onetomany;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@ -26,7 +27,7 @@ public class SetRefIngEntity {
private String data;
@Audited
@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
private SetRefEdEntity reference;
public SetRefIngEntity() {

View File

@ -0,0 +1,117 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.envers.test.integration.jta;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.persistence.EntityManager;
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase;
import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.entities.onetomany.SetRefEdEntity;
import org.hibernate.envers.test.entities.onetomany.SetRefIngEntity;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.jta.TestingJtaBootstrap;
import org.hibernate.testing.jta.TestingJtaPlatformImpl;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* @author Andrea Boriero
* @author VladoKuruc
*/
@TestForIssue( jiraKey = "HHH-14061")
public class OneToManyLazyJtaSessionClosedBeforeCommitTest extends BaseEnversJPAFunctionalTestCase {
private Integer parentId;
private Integer entityId;
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {SetRefIngEntity.class, SetRefEdEntity.class};
}
@Override
protected void addConfigOptions(Map options) {
TestingJtaBootstrap.prepare( options );
options.put( AvailableSettings.ALLOW_JTA_TRANSACTION_ACCESS, "true" );
}
@Test
@Priority(10)
public void initData() throws Exception {
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
EntityManager entityManager = getEntityManager();
try {
SetRefIngEntity refIngEntity = new SetRefIngEntity( 3, "ingEntityRef" );
entityManager.persist( refIngEntity );
SetRefEdEntity edEntity = new SetRefEdEntity( 2, "edEntity" );
edEntity.setRef(refIngEntity);
entityManager.persist( edEntity );
parentId = edEntity.getId();
SetRefIngEntity ingEntity = new SetRefIngEntity( 1, "ingEntity" );
Set<SetRefIngEntity> sries = new HashSet<>();
sries.add( ingEntity );
ingEntity.setReference( edEntity );
edEntity.setReffering( sries );
entityManager.persist( ingEntity );
entityId = ingEntity.getId();
}
finally {
entityManager.close();
TestingJtaPlatformImpl.tryCommit();
}
TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
entityManager = getEntityManager();
try {
entityManager.unwrap(Session.class).setHibernateFlushMode(FlushMode.MANUAL);
SetRefEdEntity edEntity = entityManager.find(SetRefEdEntity.class, parentId);
Set<SetRefIngEntity> reffering = edEntity.getReffering();
SetRefIngEntity ingEntity = reffering.iterator().next();
ingEntity.setReference(null);
reffering.remove(ingEntity);
entityManager.merge(ingEntity);
entityManager.flush();
//clear context in transaction
entityManager.clear();
entityManager.merge(edEntity);
entityManager.flush();
}
finally {
entityManager.close();
TestingJtaPlatformImpl.tryCommit();
}
}
@Test
public void testRevisionCounts() {
assertEquals(
Arrays.asList(1, 2),
getAuditReader().getRevisions( SetRefIngEntity.class, entityId )
);
assertEquals(
Arrays.asList(1, 2),
getAuditReader().getRevisions( SetRefEdEntity.class, parentId )
);
}
@Test
public void testRevisionHistory() {
assertEquals( Arrays.asList( 1, 2 ), getAuditReader().getRevisions( SetRefIngEntity.class, entityId ) );
assertEquals( Arrays.asList( 1, 2 ), getAuditReader().getRevisions( SetRefEdEntity.class, parentId ) );
}
}