diff --git a/envers/src/main/java/org/hibernate/envers/configuration/metadata/AuditMetadataGenerator.java b/envers/src/main/java/org/hibernate/envers/configuration/metadata/AuditMetadataGenerator.java index 5dafd953ca..a257bc5800 100644 --- a/envers/src/main/java/org/hibernate/envers/configuration/metadata/AuditMetadataGenerator.java +++ b/envers/src/main/java/org/hibernate/envers/configuration/metadata/AuditMetadataGenerator.java @@ -381,7 +381,7 @@ public final class AuditMetadataGenerator { // Adding the "key" element with all columns + the revision number column Element keyMapping = mappingData.getFirst().addElement("key"); - MetadataTools.addColumns(keyMapping, pc.getIdentifierProperty().getColumnIterator()); + MetadataTools.addColumns(keyMapping, pc.getTable().getPrimaryKey().columnIterator()); MetadataTools.addColumn(keyMapping, verEntCfg.getRevisionFieldName(), null, 0, 0, null); break; diff --git a/envers/src/test/java/org/hibernate/envers/test/integration/inheritance/joined/primarykeyjoin/ChildPrimaryKeyJoinAuditing.java b/envers/src/test/java/org/hibernate/envers/test/integration/inheritance/joined/primarykeyjoin/ChildPrimaryKeyJoinAuditing.java new file mode 100644 index 0000000000..c2b2ff43e8 --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/integration/inheritance/joined/primarykeyjoin/ChildPrimaryKeyJoinAuditing.java @@ -0,0 +1,105 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC. + * + * 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.inheritance.joined.primarykeyjoin; + +import java.util.Arrays; +import javax.persistence.EntityManager; + +import org.hibernate.envers.test.AbstractEntityTest; +import org.hibernate.envers.test.integration.inheritance.joined.ParentEntity; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.testng.Assert; + +import org.hibernate.ejb.Ejb3Configuration; +import org.hibernate.mapping.Column; + +/** + * @author Adam Warski (adam at warski dot org) + */ +public class ChildPrimaryKeyJoinAuditing extends AbstractEntityTest { + private Integer id1; + + public void configure(Ejb3Configuration cfg) { + cfg.addAnnotatedClass(ChildPrimaryKeyJoinEntity.class); + cfg.addAnnotatedClass(ParentEntity.class); + } + + @BeforeClass(dependsOnMethods = "init") + public void initData() { + EntityManager em = getEntityManager(); + + id1 = 1; + + // Rev 1 + em.getTransaction().begin(); + ChildPrimaryKeyJoinEntity ce = new ChildPrimaryKeyJoinEntity(id1, "x", 1l); + em.persist(ce); + em.getTransaction().commit(); + + // Rev 2 + em.getTransaction().begin(); + ce = em.find(ChildPrimaryKeyJoinEntity.class, id1); + ce.setData("y"); + ce.setNumber(2l); + em.getTransaction().commit(); + } + + @Test + public void testRevisionsCounts() { + assert Arrays.asList(1, 2).equals(getAuditReader().getRevisions(ChildPrimaryKeyJoinEntity.class, id1)); + } + + @Test + public void testHistoryOfChildId1() { + ChildPrimaryKeyJoinEntity ver1 = new ChildPrimaryKeyJoinEntity(id1, "x", 1l); + ChildPrimaryKeyJoinEntity ver2 = new ChildPrimaryKeyJoinEntity(id1, "y", 2l); + + assert getAuditReader().find(ChildPrimaryKeyJoinEntity.class, id1, 1).equals(ver1); + assert getAuditReader().find(ChildPrimaryKeyJoinEntity.class, id1, 2).equals(ver2); + + assert getAuditReader().find(ParentEntity.class, id1, 1).equals(ver1); + assert getAuditReader().find(ParentEntity.class, id1, 2).equals(ver2); + } + + @Test + public void testPolymorphicQuery() { + ChildPrimaryKeyJoinEntity childVer1 = new ChildPrimaryKeyJoinEntity(id1, "x", 1l); + + assert getAuditReader().createQuery().forEntitiesAtRevision(ChildPrimaryKeyJoinEntity.class, 1).getSingleResult() + .equals(childVer1); + + assert getAuditReader().createQuery().forEntitiesAtRevision(ParentEntity.class, 1).getSingleResult() + .equals(childVer1); + } + + @Test + public void testChildIdColumnName() { + Assert.assertEquals("other_id", + ((Column) getCfg() + .getClassMapping("org.hibernate.envers.test.integration.inheritance.joined.primarykeyjoin.ChildPrimaryKeyJoinEntity_AUD") + .getKey().getColumnIterator().next()).getName()); + } +} \ No newline at end of file diff --git a/envers/src/test/java/org/hibernate/envers/test/integration/inheritance/joined/primarykeyjoin/ChildPrimaryKeyJoinEntity.java b/envers/src/test/java/org/hibernate/envers/test/integration/inheritance/joined/primarykeyjoin/ChildPrimaryKeyJoinEntity.java new file mode 100644 index 0000000000..414b55a0f2 --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/integration/inheritance/joined/primarykeyjoin/ChildPrimaryKeyJoinEntity.java @@ -0,0 +1,82 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC. + * + * 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.inheritance.joined.primarykeyjoin; + +import javax.persistence.Basic; +import javax.persistence.Entity; +import javax.persistence.PrimaryKeyJoinColumn; + +import org.hibernate.envers.Audited; +import org.hibernate.envers.test.integration.inheritance.joined.ParentEntity; + +/** + * @author Adam Warski (adam at warski dot org) + */ +@Entity +@Audited +@PrimaryKeyJoinColumn(name = "other_id") +public class ChildPrimaryKeyJoinEntity extends ParentEntity { + @Basic + private Long number; + + public ChildPrimaryKeyJoinEntity() { + } + + public ChildPrimaryKeyJoinEntity(Integer id, String data, Long number) { + super(id, data); + this.number = number; + } + + public Long getNumber() { + return number; + } + + public void setNumber(Long number) { + this.number = number; + } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof ChildPrimaryKeyJoinEntity)) return false; + if (!super.equals(o)) return false; + + ChildPrimaryKeyJoinEntity childPrimaryKeyJoinEntity = (ChildPrimaryKeyJoinEntity) o; + + //noinspection RedundantIfStatement + if (number != null ? !number.equals(childPrimaryKeyJoinEntity.number) : childPrimaryKeyJoinEntity.number != null) return false; + + return true; + } + + public int hashCode() { + int result = super.hashCode(); + result = 31 * result + (number != null ? number.hashCode() : 0); + return result; + } + + public String toString() { + return "CPKJE(id = " + getId() + ", data = " + getData() + ", number = " + number + ")"; + } +} \ No newline at end of file diff --git a/envers/src/test/java/org/hibernate/envers/test/integration/inheritance/single/ChildEntity.java b/envers/src/test/java/org/hibernate/envers/test/integration/inheritance/single/ChildEntity.java index 5084cfe660..a51885bbfc 100644 --- a/envers/src/test/java/org/hibernate/envers/test/integration/inheritance/single/ChildEntity.java +++ b/envers/src/test/java/org/hibernate/envers/test/integration/inheritance/single/ChildEntity.java @@ -79,6 +79,6 @@ public class ChildEntity extends ParentEntity { } public String toString() { - return "ChildEntity(id = " + getId() + ", data = " + getData() + ", number = " + number + ")"; + return "ChildPrimaryKeyJoinEntity(id = " + getId() + ", data = " + getData() + ", number = " + number + ")"; } } \ No newline at end of file diff --git a/envers/src/test/java/org/hibernate/envers/test/integration/inheritance/tableperclass/ChildEntity.java b/envers/src/test/java/org/hibernate/envers/test/integration/inheritance/tableperclass/ChildEntity.java index a7c9d983cc..dfcdb51dff 100644 --- a/envers/src/test/java/org/hibernate/envers/test/integration/inheritance/tableperclass/ChildEntity.java +++ b/envers/src/test/java/org/hibernate/envers/test/integration/inheritance/tableperclass/ChildEntity.java @@ -73,6 +73,6 @@ public class ChildEntity extends ParentEntity { } public String toString() { - return "ChildEntity(id = " + getId() + ", data = " + getData() + ", number = " + number + ")"; + return "ChildPrimaryKeyJoinEntity(id = " + getId() + ", data = " + getData() + ", number = " + number + ")"; } } \ No newline at end of file diff --git a/envers/src/test/resources/testng.xml b/envers/src/test/resources/testng.xml index 2dd5d5db0f..5f239db5b2 100644 --- a/envers/src/test/resources/testng.xml +++ b/envers/src/test/resources/testng.xml @@ -18,6 +18,7 @@ +