HHH-8305 - Fix ToOne join tables with optional=true and OneToOne inverse mappings.

(cherry picked from commit 86fad51)
This commit is contained in:
Chris Cranford 2016-06-15 11:12:55 -05:00
parent ec7e2d88ef
commit 1e860c4195
7 changed files with 437 additions and 0 deletions

View File

@ -456,6 +456,16 @@ public final class AuditMetadataGenerator {
final Element joinElement = MetadataTools.createJoin( parent, auditTableName, schema, catalog );
joinElements.put( join, joinElement );
// HHH-8305 - Fix case when join is considered optional.
if ( join.isOptional() ) {
joinElement.addAttribute( "optional", "true" );
}
// HHH-8305 - Fix case when join is the inverse side of a mapping.
if ( join.isInverse() ) {
joinElement.addAttribute( "inverse", "true" );
}
final Element joinKey = joinElement.addElement( "key" );
MetadataTools.addColumns( joinKey, join.getKey().getColumnIterator() );
MetadataTools.addColumn( joinKey, verEntCfg.getRevisionFieldName(), null, null, null, null, null, null );

View File

@ -0,0 +1,64 @@
/*
* 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.manytoone.bidirectional;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import org.hibernate.envers.Audited;
/**
* @author Chris Cranford
*/
@Entity
@Audited
public class BiRefedOptionalEntity {
@Id
@GeneratedValue
private Integer id;
@OneToMany(mappedBy = "reference")
private List<BiRefingOptionalEntity> references = new ArrayList<>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<BiRefingOptionalEntity> getReferences() {
return references;
}
public void setReferences(List<BiRefingOptionalEntity> references) {
this.references = references;
}
@Override
public int hashCode() {
return ( id != null ? id.hashCode() : 0 );
}
@Override
public boolean equals(Object object) {
if ( object == this ) {
return true;
}
if ( !( object instanceof BiRefedOptionalEntity ) ) {
return false;
}
BiRefedOptionalEntity that = (BiRefedOptionalEntity) object;
return !( id != null ? !id.equals( that.id ) : that.id != null );
}
}

View File

@ -0,0 +1,64 @@
/*
* 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.manytoone.bidirectional;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import org.hibernate.envers.Audited;
/**
* @author Chris Cranford
*/
@Entity
@Audited
public class BiRefingOptionalEntity {
@Id
@GeneratedValue
private Integer id;
@ManyToOne(optional = true)
@JoinTable(name = "A_B", joinColumns = @JoinColumn(name = "a_id", unique = true), inverseJoinColumns = @JoinColumn(name = "b_id") )
private BiRefedOptionalEntity reference;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public BiRefedOptionalEntity getReference() {
return reference;
}
public void setReference(BiRefedOptionalEntity reference) {
this.reference = reference;
}
@Override
public int hashCode() {
return ( id != null ? id.hashCode() : 0 );
}
@Override
public boolean equals(Object object) {
if ( object == this ) {
return true;
}
if ( !( object instanceof BiRefingOptionalEntity ) ) {
return false;
}
BiRefingOptionalEntity that = (BiRefingOptionalEntity) object;
return !( id != null ? !id.equals( that.id ) : that.id != null );
}
}

View File

@ -0,0 +1,87 @@
/*
* 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.manytoone.bidirectional;
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;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Chris Cranford
*/
@TestForIssue(jiraKey = "HHH-8305")
public class BidirectionalManyToOneOptionalTest extends BaseEnversJPAFunctionalTestCase {
private Integer refingWithNoRefedId;
private Integer refingId;
private Integer refedId;
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
BiRefingOptionalEntity.class,
BiRefedOptionalEntity.class
};
}
@Test
@Priority(10)
public void initData() {
EntityManager entityManager = getEntityManager();
try {
// Revision 1
entityManager.getTransaction().begin();
// store refing with null refed entity
BiRefingOptionalEntity refingWithNoRefed = new BiRefingOptionalEntity();
refingWithNoRefed.setReference( null );
entityManager.persist( refingWithNoRefed );
// store refing with non-null refed entity
BiRefingOptionalEntity refing = new BiRefingOptionalEntity();
BiRefedOptionalEntity refed = new BiRefedOptionalEntity();
refed.getReferences().add( refing );
refing.setReference( refed );
entityManager.persist( refing );
entityManager.persist( refed );
entityManager.getTransaction().commit();
this.refingId = refing.getId();
this.refedId = refed.getId();
this.refingWithNoRefedId = refingWithNoRefed.getId();
}
finally {
entityManager.close();
}
}
@Test
public void testRevisionCounts() {
assertEquals( 1, getAuditReader().getRevisions( BiRefingOptionalEntity.class, refingId ).size() );
assertEquals( 1, getAuditReader().getRevisions( BiRefingOptionalEntity.class, refingWithNoRefedId ).size() );
assertEquals( 1, getAuditReader().getRevisions( BiRefedOptionalEntity.class, refedId ).size() );
}
@Test
public void testRevisionHistoryNullReference() {
BiRefingOptionalEntity rev1 = getAuditReader().find( BiRefingOptionalEntity.class, refingWithNoRefedId, 1 );
assertNull( rev1.getReference() );
}
@Test
public void testRevisionHistoryWithNonNullReference() {
assertNotNull( getAuditReader().find( BiRefingOptionalEntity.class, refingId, 1).getReference() );
assertEquals( 1, getAuditReader().find( BiRefedOptionalEntity.class, refedId, 1 ).getReferences().size() );
}
}

View File

@ -0,0 +1,61 @@
/*
* 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.onetoone.bidirectional;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import org.hibernate.envers.Audited;
/**
* @author Chris Cranford
*/
@Entity
@Audited
public class BiRefedOptionalEntity {
@Id
@GeneratedValue
private Integer id;
@OneToOne(mappedBy = "reference", optional = true)
private BiRefingOptionalEntity referencing;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public BiRefingOptionalEntity getReferencing() {
return referencing;
}
public void setReferencing(BiRefingOptionalEntity referencing) {
this.referencing = referencing;
}
@Override
public int hashCode() {
return ( id != null ? id.hashCode() : 0 );
}
@Override
public boolean equals(Object object) {
if ( object == this ) {
return true;
}
if ( !( object instanceof BiRefedOptionalEntity ) ) {
return false;
}
BiRefedOptionalEntity that = (BiRefedOptionalEntity) object;
return !( id != null ? !id.equals( that.id ) : that.id != null );
}
}

View File

@ -0,0 +1,64 @@
/*
* 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.onetoone.bidirectional;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToOne;
import org.hibernate.envers.Audited;
/**
* @author Chris Cranford
*/
@Entity
@Audited
public class BiRefingOptionalEntity {
@Id
@GeneratedValue
private Integer id;
@OneToOne(optional = true)
@JoinTable(name = "A_B", joinColumns = @JoinColumn(name = "a_id", unique = true), inverseJoinColumns = @JoinColumn(name = "b_id") )
private BiRefedOptionalEntity reference;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public BiRefedOptionalEntity getReference() {
return reference;
}
public void setReference(BiRefedOptionalEntity reference) {
this.reference = reference;
}
@Override
public int hashCode() {
return ( id != null ? id.hashCode() : 0 );
}
@Override
public boolean equals(Object object) {
if ( object == this ) {
return true;
}
if ( !( object instanceof BiRefingOptionalEntity ) ) {
return false;
}
BiRefingOptionalEntity that = (BiRefingOptionalEntity) object;
return !( id != null ? !id.equals( that.id ) : that.id != null );
}
}

View File

@ -0,0 +1,87 @@
/*
* 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.onetoone.bidirectional;
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;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Chris Cranford
*/
@TestForIssue(jiraKey = "HHH-8305")
public class BidirectionalOneToOneOptionalTest extends BaseEnversJPAFunctionalTestCase {
private Integer refingWithNoRefedId;
private Integer refingId;
private Integer refedId;
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
BiRefingOptionalEntity.class,
BiRefedOptionalEntity.class
};
}
@Test
@Priority(10)
public void initData() {
EntityManager entityManager = getEntityManager();
try {
// Revision 1
entityManager.getTransaction().begin();
// store refing with null refed entity
BiRefingOptionalEntity refingWithNoRefed = new BiRefingOptionalEntity();
refingWithNoRefed.setReference( null );
entityManager.persist( refingWithNoRefed );
// store refing with non-null refed entity
BiRefingOptionalEntity refing = new BiRefingOptionalEntity();
BiRefedOptionalEntity refed = new BiRefedOptionalEntity();
refed.setReferencing( refing );
refing.setReference( refed );
entityManager.persist( refing );
entityManager.persist( refed );
entityManager.getTransaction().commit();
this.refingId = refing.getId();
this.refedId = refed.getId();
this.refingWithNoRefedId = refingWithNoRefed.getId();
}
finally {
entityManager.close();
}
}
@Test
public void testRevisionCounts() {
assertEquals( 1, getAuditReader().getRevisions( BiRefingOptionalEntity.class, refingId ).size() );
assertEquals( 1, getAuditReader().getRevisions( BiRefingOptionalEntity.class, refingWithNoRefedId ).size() );
assertEquals( 1, getAuditReader().getRevisions( BiRefedOptionalEntity.class, refedId ).size() );
}
@Test
public void testRevisionHistoryNullReference() {
BiRefingOptionalEntity rev1 = getAuditReader().find( BiRefingOptionalEntity.class, refingWithNoRefedId, 1 );
assertNull( rev1.getReference() );
}
@Test
public void testRevisionHistoryWithNonNullReference() {
assertNotNull( getAuditReader().find( BiRefingOptionalEntity.class, refingId, 1).getReference() );
assertNotNull( getAuditReader().find( BiRefedOptionalEntity.class, refedId, 1 ).getReferencing() );
}
}