HHH-4641:
- support for @PrimaryKeyJoinColumn on inherited entities - test git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18136 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
acecf71618
commit
ecb103cf55
|
@ -381,7 +381,7 @@ public final class AuditMetadataGenerator {
|
||||||
|
|
||||||
// Adding the "key" element with all columns + the revision number column
|
// Adding the "key" element with all columns + the revision number column
|
||||||
Element keyMapping = mappingData.getFirst().addElement("key");
|
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);
|
MetadataTools.addColumn(keyMapping, verEntCfg.getRevisionFieldName(), null, 0, 0, null);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -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 + ")";
|
||||||
|
}
|
||||||
|
}
|
|
@ -79,6 +79,6 @@ public class ChildEntity extends ParentEntity {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "ChildEntity(id = " + getId() + ", data = " + getData() + ", number = " + number + ")";
|
return "ChildPrimaryKeyJoinEntity(id = " + getId() + ", data = " + getData() + ", number = " + number + ")";
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -73,6 +73,6 @@ public class ChildEntity extends ParentEntity {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "ChildEntity(id = " + getId() + ", data = " + getData() + ", number = " + number + ")";
|
return "ChildPrimaryKeyJoinEntity(id = " + getId() + ", data = " + getData() + ", number = " + number + ")";
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -18,6 +18,7 @@
|
||||||
<package name="org.hibernate.envers.test.integration.inheritance.joined.childrelation" />
|
<package name="org.hibernate.envers.test.integration.inheritance.joined.childrelation" />
|
||||||
<package name="org.hibernate.envers.test.integration.inheritance.joined.emptychild" />
|
<package name="org.hibernate.envers.test.integration.inheritance.joined.emptychild" />
|
||||||
<package name="org.hibernate.envers.test.integration.inheritance.joined.notownedrelation" />
|
<package name="org.hibernate.envers.test.integration.inheritance.joined.notownedrelation" />
|
||||||
|
<package name="org.hibernate.envers.test.integration.inheritance.joined.primarykeyjoin" />
|
||||||
<package name="org.hibernate.envers.test.integration.inheritance.joined.relation" />
|
<package name="org.hibernate.envers.test.integration.inheritance.joined.relation" />
|
||||||
<package name="org.hibernate.envers.test.integration.inheritance.joined.relation.unidirectional" />
|
<package name="org.hibernate.envers.test.integration.inheritance.joined.relation.unidirectional" />
|
||||||
<package name="org.hibernate.envers.test.integration.inheritance.single" />
|
<package name="org.hibernate.envers.test.integration.inheritance.single" />
|
||||||
|
|
Loading…
Reference in New Issue