From 94e13074c19059bada41afe93a4f54c186567655 Mon Sep 17 00:00:00 2001 From: Lukasz Antoniak Date: Sun, 11 Nov 2012 18:09:22 +0100 Subject: [PATCH] HHH-3854 - Fix and test --- .../mapper/relation/ToOneEntityLoader.java | 53 +++++++ .../mapper/relation/ToOneIdMapper.java | 9 +- .../lazy/ToOneDelegateSessionImplementor.java | 13 +- ...rectionalEagerAnnotationRefEdOneToOne.java | 121 +++++++++++++++ ...ectionalEagerAnnotationRefIngOneToOne.java | 143 ++++++++++++++++++ .../BidirectionalEagerHbmRefEdPK.java | 88 +++++++++++ .../BidirectionalEagerHbmRefIngPK.java | 88 +++++++++++ .../BidirectionalEagerAnnotationTest.java | 81 ++++++++++ .../BidirectionalEagerHbmTest.java | 75 +++++++++ .../bidirectional/eagerLoading.hbm.xml | 45 ++++++ 10 files changed, 703 insertions(+), 13 deletions(-) create mode 100644 hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/ToOneEntityLoader.java create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/entities/onetoone/BidirectionalEagerAnnotationRefEdOneToOne.java create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/entities/onetoone/BidirectionalEagerAnnotationRefIngOneToOne.java create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/entities/onetoone/BidirectionalEagerHbmRefEdPK.java create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/entities/onetoone/BidirectionalEagerHbmRefIngPK.java create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/onetoone/bidirectional/BidirectionalEagerAnnotationTest.java create mode 100644 hibernate-envers/src/test/java/org/hibernate/envers/test/integration/onetoone/bidirectional/BidirectionalEagerHbmTest.java create mode 100644 hibernate-envers/src/test/resources/mappings/oneToOne/bidirectional/eagerLoading.hbm.xml diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/ToOneEntityLoader.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/ToOneEntityLoader.java new file mode 100644 index 0000000000..c59e3cc05d --- /dev/null +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/ToOneEntityLoader.java @@ -0,0 +1,53 @@ +package org.hibernate.envers.entities.mapper.relation; + +import java.io.Serializable; + +import org.hibernate.envers.configuration.AuditConfiguration; +import org.hibernate.envers.entities.mapper.relation.lazy.ToOneDelegateSessionImplementor; +import org.hibernate.envers.reader.AuditReaderImplementor; +import org.hibernate.persister.entity.EntityPersister; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +public class ToOneEntityLoader { + /** + * Immediately loads historical entity or its current state when excluded from audit process. + */ + public static Object loadImmediate(AuditReaderImplementor versionsReader, Class entityClass, String entityName, + Object entityId, Number revision, AuditConfiguration verCfg) { + if ( verCfg.getEntCfg().getNotVersionEntityConfiguration( entityName ) == null ) { + // Audited relation, look up entity with Envers. + return versionsReader.find( entityClass, entityName, entityId, revision ); + } + else { + // Not audited relation, look up entity with Hibernate. + return versionsReader.getSessionImplementor().immediateLoad( entityName, (Serializable) entityId ); + } + } + + /** + * Creates proxy of referenced *-to-one entity. + */ + public static Object createProxy(AuditReaderImplementor versionsReader, Class entityClass, String entityName, + Object entityId, Number revision, AuditConfiguration verCfg) { + EntityPersister persister = versionsReader.getSessionImplementor().getFactory().getEntityPersister( entityName ); + return persister.createProxy( + (Serializable) entityId, + new ToOneDelegateSessionImplementor( versionsReader, entityClass, entityId, revision, verCfg ) + ); + } + + /** + * Creates Hibernate proxy or retrieves the complete object of an entity if proxy is not + * allowed (e.g. @Proxy(lazy=false), final class). + */ + public static Object createProxyOrLoadImmediate(AuditReaderImplementor versionsReader, Class entityClass, String entityName, + Object entityId, Number revision, AuditConfiguration verCfg) { + EntityPersister persister = versionsReader.getSessionImplementor().getFactory().getEntityPersister( entityName ); + if ( persister.hasProxy() ) { + return createProxy( versionsReader, entityClass, entityName, entityId, revision, verCfg ); + } + return loadImmediate( versionsReader, entityClass, entityName, entityId, revision, verCfg ); + } +} diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/ToOneIdMapper.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/ToOneIdMapper.java index 5093ef70b7..308ffe42ba 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/ToOneIdMapper.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/ToOneIdMapper.java @@ -34,6 +34,7 @@ import org.hibernate.envers.entities.mapper.id.IdMapper; import org.hibernate.envers.entities.mapper.relation.lazy.ToOneDelegateSessionImplementor; import org.hibernate.envers.reader.AuditReaderImplementor; import org.hibernate.envers.tools.Tools; +import org.hibernate.persister.entity.EntityPersister; /** * @author Adam Warski (adam at warski dot org) @@ -95,10 +96,10 @@ public class ToOneIdMapper extends AbstractToOneMapper { value = versionsReader.getFirstLevelCache().get(referencedEntityName, revision, entityId); } else { EntityInfo referencedEntity = getEntityInfo(verCfg, referencedEntityName); - - value = versionsReader.getSessionImplementor().getFactory().getEntityPersister(referencedEntityName). - createProxy((Serializable)entityId, new ToOneDelegateSessionImplementor(versionsReader, referencedEntity.getEntityClass(), - entityId, revision, verCfg)); + value = ToOneEntityLoader.createProxyOrLoadImmediate( + versionsReader, referencedEntity.getEntityClass(), referencedEntityName, + entityId, revision, verCfg + ); } } diff --git a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/ToOneDelegateSessionImplementor.java b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/ToOneDelegateSessionImplementor.java index d7096b82e8..adc9f0224a 100644 --- a/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/ToOneDelegateSessionImplementor.java +++ b/hibernate-envers/src/main/java/org/hibernate/envers/entities/mapper/relation/lazy/ToOneDelegateSessionImplementor.java @@ -27,6 +27,7 @@ import java.io.Serializable; import org.hibernate.HibernateException; import org.hibernate.envers.configuration.AuditConfiguration; import org.hibernate.envers.entities.EntitiesConfigurations; +import org.hibernate.envers.entities.mapper.relation.ToOneEntityLoader; import org.hibernate.envers.reader.AuditReaderImplementor; /** @@ -41,7 +42,7 @@ public class ToOneDelegateSessionImplementor extends AbstractDelegateSessionImpl private final Class entityClass; private final Object entityId; private final Number revision; - private EntitiesConfigurations entCfg; + private final AuditConfiguration verCfg; public ToOneDelegateSessionImplementor(AuditReaderImplementor versionsReader, Class entityClass, Object entityId, Number revision, @@ -51,16 +52,10 @@ public class ToOneDelegateSessionImplementor extends AbstractDelegateSessionImpl this.entityClass = entityClass; this.entityId = entityId; this.revision = revision; - this.entCfg = verCfg.getEntCfg(); + this.verCfg = verCfg; } public Object doImmediateLoad(String entityName) throws HibernateException { - if(entCfg.getNotVersionEntityConfiguration(entityName) == null){ - // audited relation, look up entity with envers - return versionsReader.find(entityClass, entityName, entityId, revision); - } else { - // notAudited relation, look up entity with hibernate - return delegate.immediateLoad(entityName, (Serializable) entityId); - } + return ToOneEntityLoader.loadImmediate( versionsReader, entityClass, entityName, entityId, revision, verCfg ); } } diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/onetoone/BidirectionalEagerAnnotationRefEdOneToOne.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/onetoone/BidirectionalEagerAnnotationRefEdOneToOne.java new file mode 100644 index 0000000000..eb294b3424 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/onetoone/BidirectionalEagerAnnotationRefEdOneToOne.java @@ -0,0 +1,121 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.envers.test.entities.onetoone; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +import org.hibernate.annotations.Proxy; +import org.hibernate.envers.Audited; +import org.hibernate.envers.NotAudited; + +/** + * Test class for issue HHH-3854. Restricting creation of proxy objects is essential. + */ +@Entity +@Audited +@Proxy(lazy=false) +public final class BidirectionalEagerAnnotationRefEdOneToOne { + /** + * ID column. + */ + @Id + @GeneratedValue + private Integer id; + + /** + * Field containing the referring entity. + */ + @OneToOne(mappedBy = "refedOne", fetch = FetchType.EAGER) + @NotAudited + private BidirectionalEagerAnnotationRefIngOneToOne refIng; + + /** + * Field containing some data. + */ + private String data; + + @Override + public boolean equals(Object o) { + if ( this == o ) return true; + if ( ! ( o instanceof BidirectionalEagerAnnotationRefEdOneToOne ) ) return false; + + BidirectionalEagerAnnotationRefEdOneToOne that = (BidirectionalEagerAnnotationRefEdOneToOne) o; + + if ( data != null ? !data.equals( that.data ) : that.data != null ) return false; + if ( id != null ? !id.equals( that.id ) : that.id != null ) return false; + + return true; + } + + @Override + public int hashCode() { + int result = id != null ? id.hashCode() : 0; + result = 31 * result + ( data != null ? data.hashCode() : 0 ); + return result; + } + + @Override + public String toString() { + return "BidirectionalEagerAnnotationRefEdOneToOne(id = " + id + ", data = " + data + ")"; + } + + /** + * @return the id + */ + public Integer getId() { + return id; + } + + /** + * @return the refIng + */ + public BidirectionalEagerAnnotationRefIngOneToOne getRefIng() { + return refIng; + } + + /** + * @param refIng the refIng to set + */ + public void setRefIng(BidirectionalEagerAnnotationRefIngOneToOne refIng) { + this.refIng = refIng; + } + + /** + * @return the data + */ + public String getData() { + return data; + } + + /** + * @param data the data to set + */ + public void setData(String data) { + this.data = data; + } +} \ No newline at end of file diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/onetoone/BidirectionalEagerAnnotationRefIngOneToOne.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/onetoone/BidirectionalEagerAnnotationRefIngOneToOne.java new file mode 100644 index 0000000000..71e50b714a --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/onetoone/BidirectionalEagerAnnotationRefIngOneToOne.java @@ -0,0 +1,143 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.envers.test.entities.onetoone; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToOne; + +import org.hibernate.annotations.Proxy; +import org.hibernate.envers.Audited; + +/** + * Test class for issue HHH-3854. Restricting creation of proxy objects is essential. + */ +@Entity +@Audited +@Proxy(lazy=false) +public final class BidirectionalEagerAnnotationRefIngOneToOne { + /** + * ID column. + */ + @Id + @GeneratedValue + private Integer id; + + /** + * Field with an optional first referred entity. + */ + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, optional = true) + @JoinColumn(name = "REF_ID_ONE") + private BidirectionalEagerAnnotationRefEdOneToOne refedOne = null; + + /** + * Field with an optional second referred entity. + */ + @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, optional = true) + @JoinColumn(name = "REF_ID_TWO") + private BidirectionalEagerAnnotationRefEdOneToOne refedTwo = null; + + /** + * Field containing some data. + */ + private String data; + + @Override + public boolean equals(Object o) { + if ( this == o ) return true; + if ( ! ( o instanceof BidirectionalEagerAnnotationRefIngOneToOne ) ) return false; + + BidirectionalEagerAnnotationRefIngOneToOne that = (BidirectionalEagerAnnotationRefIngOneToOne) o; + + if ( data != null ? !data.equals( that.data ) : that.data != null ) return false; + if ( id != null ? !id.equals( that.id ) : that.id != null ) return false; + + return true; + } + + @Override + public int hashCode() { + int result = id != null ? id.hashCode() : 0; + result = 31 * result + ( data != null ? data.hashCode() : 0 ); + return result; + } + + @Override + public String toString() { + return "BidirectionalEagerAnnotationRefIngOneToOne(id = " + id + ", data = " + data + ")"; + } + + /** + * @return the id + */ + public Integer getId() { + return id; + } + + /** + * @return the refedOne + */ + public BidirectionalEagerAnnotationRefEdOneToOne getRefedOne() { + return refedOne; + } + + /** + * @param refedOne the refedOne to set + */ + public void setRefedOne(BidirectionalEagerAnnotationRefEdOneToOne refedOne) { + this.refedOne = refedOne; + } + + /** + * @return the refedTwo + */ + public BidirectionalEagerAnnotationRefEdOneToOne getRefedTwo() { + return refedTwo; + } + + /** + * @param refedTwo the refedTwo to set + */ + public void setRefedTwo(BidirectionalEagerAnnotationRefEdOneToOne refedTwo) { + this.refedTwo = refedTwo; + } + + /** + * @return the data + */ + public String getData() { + return data; + } + + /** + * @param data the data to set + */ + public void setData(String data) { + this.data = data; + } +} \ No newline at end of file diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/onetoone/BidirectionalEagerHbmRefEdPK.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/onetoone/BidirectionalEagerHbmRefEdPK.java new file mode 100644 index 0000000000..ee3ae95aea --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/onetoone/BidirectionalEagerHbmRefEdPK.java @@ -0,0 +1,88 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.envers.test.entities.onetoone; + +import org.hibernate.envers.Audited; + +@Audited +public class BidirectionalEagerHbmRefEdPK { + private long id; + private String data; + private BidirectionalEagerHbmRefIngPK referencing; + + public BidirectionalEagerHbmRefEdPK() {} + + public BidirectionalEagerHbmRefEdPK(String data) { + this.data = data; + } + + public long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + + public BidirectionalEagerHbmRefIngPK getReferencing() { + return referencing; + } + + public void setReferencing(BidirectionalEagerHbmRefIngPK referencing) { + this.referencing = referencing; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) return true; + if ( ! ( o instanceof BidirectionalEagerHbmRefEdPK ) ) return false; + + BidirectionalEagerHbmRefEdPK that = (BidirectionalEagerHbmRefEdPK) o; + + if ( id != that.id ) return false; + if ( data != null ? !data.equals( that.data ) : that.data != null ) return false; + + return true; + } + + @Override + public int hashCode() { + int result = (int) ( id ^ ( id >>> 32 ) ); + result = 31 * result + ( data != null ? data.hashCode() : 0 ); + return result; + } + + @Override + public String toString() { + return "BidirectionalEagerHbmRefEdPK(id = " + id + ", data = " + data + ")"; + } +} \ No newline at end of file diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/onetoone/BidirectionalEagerHbmRefIngPK.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/onetoone/BidirectionalEagerHbmRefIngPK.java new file mode 100644 index 0000000000..b7bfa6ce51 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/entities/onetoone/BidirectionalEagerHbmRefIngPK.java @@ -0,0 +1,88 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.envers.test.entities.onetoone; + +import org.hibernate.envers.Audited; + +@Audited +public class BidirectionalEagerHbmRefIngPK { + private long id; + private String data; + private BidirectionalEagerHbmRefEdPK reference; + + public BidirectionalEagerHbmRefIngPK() {} + + public BidirectionalEagerHbmRefIngPK(String data) { + this.data = data; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getData() { + return data; + } + + public void setData(String data) { + this.data = data; + } + + public BidirectionalEagerHbmRefEdPK getReference() { + return reference; + } + + public void setReference(BidirectionalEagerHbmRefEdPK reference) { + this.reference = reference; + } + + @Override + public boolean equals(Object o) { + if ( this == o ) return true; + if ( ! ( o instanceof BidirectionalEagerHbmRefIngPK ) ) return false; + + BidirectionalEagerHbmRefIngPK that = (BidirectionalEagerHbmRefIngPK) o; + + if ( id != that.id ) return false; + if ( data != null ? !data.equals( that.data ) : that.data != null ) return false; + + return true; + } + + @Override + public int hashCode() { + int result = (int) ( id ^ ( id >>> 32 ) ); + result = 31 * result + ( data != null ? data.hashCode() : 0 ); + return result; + } + + @Override + public String toString() { + return "BidirectionalEagerHbmRefIngPK(id = " + id + ", data = " + data + ")"; + } +} diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/onetoone/bidirectional/BidirectionalEagerAnnotationTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/onetoone/bidirectional/BidirectionalEagerAnnotationTest.java new file mode 100644 index 0000000000..b64b7f4e1b --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/onetoone/bidirectional/BidirectionalEagerAnnotationTest.java @@ -0,0 +1,81 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.envers.test.integration.onetoone.bidirectional; + +import javax.persistence.EntityManager; + +import org.junit.Test; + +import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase; +import org.hibernate.envers.test.Priority; +import org.hibernate.envers.test.entities.onetoone.BidirectionalEagerAnnotationRefEdOneToOne; +import org.hibernate.envers.test.entities.onetoone.BidirectionalEagerAnnotationRefIngOneToOne; +import org.hibernate.testing.TestForIssue; + +import static org.junit.Assert.assertNotNull; + +/** + * @author Erik-Berndt Scheper + */ +@TestForIssue(jiraKey = "HHH-3854") +public class BidirectionalEagerAnnotationTest extends BaseEnversJPAFunctionalTestCase { + private Integer refIngId1 = null; + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + BidirectionalEagerAnnotationRefEdOneToOne.class, + BidirectionalEagerAnnotationRefIngOneToOne.class + }; + } + + @Test + @Priority(10) + public void initData() { + EntityManager em = getEntityManager(); + + // Revision 1 + em.getTransaction().begin(); + BidirectionalEagerAnnotationRefEdOneToOne ed1 = new BidirectionalEagerAnnotationRefEdOneToOne(); + BidirectionalEagerAnnotationRefIngOneToOne ing1 = new BidirectionalEagerAnnotationRefIngOneToOne(); + ed1.setData( "referredEntity1" ); + ed1.setRefIng( ing1 ); + ing1.setData( "referringEntity" ); + ing1.setRefedOne( ed1 ); + em.persist( ed1 ); + em.persist( ing1 ); + em.getTransaction().commit(); + + refIngId1 = ing1.getId(); + + em.close(); + } + + @Test + public void testNonProxyObjectTraversing() { + BidirectionalEagerAnnotationRefIngOneToOne referencing = + getAuditReader().find( BidirectionalEagerAnnotationRefIngOneToOne.class, refIngId1, 1 ); + assertNotNull( referencing.getRefedOne().getData() ); + } +} \ No newline at end of file diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/onetoone/bidirectional/BidirectionalEagerHbmTest.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/onetoone/bidirectional/BidirectionalEagerHbmTest.java new file mode 100644 index 0000000000..75c5baf28e --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/onetoone/bidirectional/BidirectionalEagerHbmTest.java @@ -0,0 +1,75 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.envers.test.integration.onetoone.bidirectional; + +import javax.persistence.EntityManager; + +import org.junit.Test; + +import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase; +import org.hibernate.envers.test.Priority; +import org.hibernate.envers.test.entities.onetoone.BidirectionalEagerHbmRefEdPK; +import org.hibernate.envers.test.entities.onetoone.BidirectionalEagerHbmRefIngPK; +import org.hibernate.testing.TestForIssue; + +import static org.junit.Assert.assertNotNull; + +/** + * @author Erik-Berndt Scheper, Amar Singh + */ +@TestForIssue(jiraKey = "HHH-3854") +public class BidirectionalEagerHbmTest extends BaseEnversJPAFunctionalTestCase { + private Long refIngId1 = null; + + @Override + protected String[] getMappings() { + return new String[] { "mappings/oneToOne/bidirectional/eagerLoading.hbm.xml" }; + } + + @Test + @Priority(10) + public void initData() { + EntityManager em = getEntityManager(); + + // Revision 1 + em.getTransaction().begin(); + BidirectionalEagerHbmRefEdPK ed1 = new BidirectionalEagerHbmRefEdPK( "data_ed_1" ); + BidirectionalEagerHbmRefIngPK ing1 = new BidirectionalEagerHbmRefIngPK( "data_ing_1" ); + ing1.setReference( ed1 ); + em.persist( ed1 ); + em.persist( ing1 ); + em.getTransaction().commit(); + + refIngId1 = ing1.getId(); + + em.close(); + } + + @Test + public void testNonProxyObjectTraversing() { + BidirectionalEagerHbmRefIngPK referencing = + getAuditReader().find( BidirectionalEagerHbmRefIngPK.class, refIngId1, 1 ); + assertNotNull( referencing.getReference().getData() ); + } +} \ No newline at end of file diff --git a/hibernate-envers/src/test/resources/mappings/oneToOne/bidirectional/eagerLoading.hbm.xml b/hibernate-envers/src/test/resources/mappings/oneToOne/bidirectional/eagerLoading.hbm.xml new file mode 100644 index 0000000000..1a0847a95f --- /dev/null +++ b/hibernate-envers/src/test/resources/mappings/oneToOne/bidirectional/eagerLoading.hbm.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file