HHH-11107 - Added test case.

This commit is contained in:
Chris Cranford 2016-10-11 11:52:13 -04:00
parent 50346775b0
commit 501a1f4f9b
6 changed files with 348 additions and 0 deletions

View File

@ -0,0 +1,53 @@
/*
* 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.ids.embeddedid;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.MapsId;
import org.hibernate.envers.Audited;
/**
* @author Chris Cranford
*/
@Audited
@Entity
public class CorrectChild {
@EmbeddedId
private CorrectChildId id;
@ManyToOne
@MapsId("parent_id")
private Parent parent;
CorrectChild() {
}
public CorrectChild(Integer number, Parent parent) {
this.id = new CorrectChildId(number, parent);
this.parent = parent;
}
public CorrectChildId getId() {
return id;
}
public void setId(CorrectChildId id) {
this.id = id;
}
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
}

View File

@ -0,0 +1,48 @@
/*
* 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.ids.embeddedid;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
/**
* @author Chris Cranford
*/
@Embeddable
public class CorrectChildId implements Serializable {
@Column(name = "parent_id")
private String id;
private Integer number;
CorrectChildId() {
}
public CorrectChildId(Integer number, Parent parent) {
this.number = number;
this.id = parent.getId();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
}

View File

@ -0,0 +1,38 @@
/*
* 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.ids.embeddedid;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import org.hibernate.envers.Audited;
/**
* @author Chris Cranford
*/
@Audited
@Entity
public class IncorrectChild {
@EmbeddedId
private IncorrectChildId id;
IncorrectChild() {
}
public IncorrectChild(Integer number, Parent parent) {
this.id = new IncorrectChildId( number, parent );
}
public IncorrectChildId getId() {
return id;
}
public void setId(IncorrectChildId id) {
this.id = id;
}
}

View File

@ -0,0 +1,51 @@
/*
* 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.ids.embeddedid;
import java.io.Serializable;
import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
/**
* @author Chris Cranford
*/
@Embeddable
public class IncorrectChildId implements Serializable {
@ManyToOne
@JoinColumns({ @JoinColumn(name = "parent_id", referencedColumnName = "id") })
private Parent parent;
private Integer number;
IncorrectChildId() {
}
public IncorrectChildId(Integer number, Parent parent) {
this.number = number;
this.parent = parent;
}
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
}

View File

@ -0,0 +1,72 @@
/*
* 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.ids.embeddedid;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import org.hibernate.envers.Audited;
/**
* @author Chris Cranford
*/
@Audited
@Entity
public class Parent {
@Id
private String id;
@OneToMany(mappedBy = "parent")
private List<CorrectChild> correctChildren = new ArrayList<CorrectChild>();
@OneToMany(mappedBy = "id.parent")
private List<IncorrectChild> incorrectChildren = new ArrayList<IncorrectChild>();;
Parent() {
}
Parent(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<CorrectChild> getCorrectChildren() {
return correctChildren;
}
public void setCorrectChildren(List<CorrectChild> correctChildren) {
this.correctChildren = correctChildren;
}
public List<IncorrectChild> getIncorrectChildren() {
return incorrectChildren;
}
public void setIncorrectChildren(List<IncorrectChild> incorrectChildren) {
this.incorrectChildren = incorrectChildren;
}
public void addIncorrectChild(Integer number) {
this.incorrectChildren.add( new IncorrectChild( number, this ) );
}
public void addCorrectChild(Integer number) {
this.correctChildren.add( new CorrectChild( number, this ) );
}
}

View File

@ -0,0 +1,86 @@
/*
* 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.ids.embeddedid;
import java.util.Arrays;
import javax.persistence.EntityManager;
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase;
import org.hibernate.envers.test.Priority;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Test avoiding the creating of a foreign key constraint for an embedded identifier that
* contains a many-to-one relationship, allowing the removal of the base table entity
* without throwing a foreign-key constraint exception due to historical audit rows.
*
* @author Chris Cranford
*/
@TestForIssue(jiraKey = "HHH-11107")
public class RelationInsideEmbeddableRemoveTest extends BaseEnversJPAFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { CorrectChild.class, IncorrectChild.class, Parent.class };
}
@Test
@Priority(10)
public void initData() {
EntityManager entityManager = getOrCreateEntityManager();
try {
// Revision 1
entityManager.getTransaction().begin();
Parent parent = new Parent( "Parent" );
parent.addIncorrectChild( 1 );
parent.addCorrectChild( 1 );
entityManager.persist( parent );
for ( IncorrectChild child : parent.getIncorrectChildren() ) {
entityManager.persist( child );
}
for ( CorrectChild child : parent.getCorrectChildren() ) {
entityManager.persist( child );
}
entityManager.getTransaction().commit();
// Revision 2
entityManager.getTransaction().begin();
for ( IncorrectChild child : parent.getIncorrectChildren() ) {
entityManager.remove( child );
}
parent.getIncorrectChildren().clear();
for( CorrectChild child : parent.getCorrectChildren() ) {
entityManager.remove( child );
}
parent.getCorrectChildren().clear();
entityManager.getTransaction().commit();
// Revision 3
// This fails because of referential integrity constraints without fix.
entityManager.getTransaction().begin();
entityManager.remove( parent );
entityManager.getTransaction().commit();
}
catch ( Exception e ) {
if ( entityManager.getTransaction().isActive() ) {
entityManager.getTransaction().rollback();
}
throw e;
}
finally {
entityManager.close();
}
}
@Test
public void testRevisionCounts() {
assertEquals( Arrays.asList( 1, 3 ), getAuditReader().getRevisions( Parent.class, "Parent" ) );
}
}