HHH-4644:

- when using join-inheritance, the columns in child entities which map to the revision number use the correct sql-type
- test

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18150 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Adam Warski 2009-12-07 08:36:49 +00:00
parent 9dbb880df6
commit e9ff8cf3b6
4 changed files with 85 additions and 8 deletions

View File

@ -61,7 +61,7 @@ public class RevisionInfoConfiguration {
private Type revisionInfoTimestampType;
private String revisionPropType;
private Column revisionPropColumn;
private String revisionPropSqlType;
public RevisionInfoConfiguration() {
revisionInfoEntityName = "org.hibernate.envers.DefaultRevisionEntity";
@ -97,9 +97,9 @@ public class RevisionInfoConfiguration {
rev_rel_mapping.addAttribute("type", revisionPropType);
rev_rel_mapping.addAttribute("class", revisionInfoEntityName);
if (revisionPropColumn != null) {
if (revisionPropSqlType != null) {
// Putting a fake name to make Hibernate happy. It will be replaced later anyway.
MetadataTools.addColumn(rev_rel_mapping, "*" , null, 0, 0, revisionPropColumn.columnDefinition());
MetadataTools.addColumn(rev_rel_mapping, "*" , null, 0, 0, revisionPropSqlType);
}
return rev_rel_mapping;
@ -137,7 +137,10 @@ public class RevisionInfoConfiguration {
// Getting the @Column definition of the revision number property, to later use that info to
// generate the same mapping for the relation from an audit table's revision number to the
// revision entity revision number.
revisionPropColumn = property.getAnnotation(Column.class);
Column revisionPropColumn = property.getAnnotation(Column.class);
if (revisionPropColumn != null) {
revisionPropSqlType = revisionPropColumn.columnDefinition();
}
}
if (revisionTimestamp != null) {

View File

@ -95,13 +95,22 @@ public final class AuditMetadataGenerator {
entitiesJoins = new HashMap<String, Map<Join, Element>>();
}
void addRevisionInfoRelation(Element any_mapping) {
/**
* Clones the revision info relation mapping, so that it can be added to other mappings. Also, the name of
* the property and the column are set properly.
* @return A revision info mapping, which can be added to other mappings (has no parent).
*/
private Element cloneAndSetupRevisionInfoRelationMapping() {
Element rev_mapping = (Element) revisionInfoRelationMapping.clone();
rev_mapping.addAttribute("name", verEntCfg.getRevisionFieldName());
MetadataTools.addOrModifyColumn(rev_mapping, verEntCfg.getRevisionFieldName());
any_mapping.add(rev_mapping);
return rev_mapping;
}
void addRevisionInfoRelation(Element any_mapping) {
any_mapping.add(cloneAndSetupRevisionInfoRelationMapping());
}
void addRevisionType(Element any_mapping) {
@ -379,10 +388,12 @@ public final class AuditMetadataGenerator {
case JOINED:
mappingData = generateInheritanceMappingData(pc, xmlMappingData, auditTableData, "joined-subclass");
// Adding the "key" element with all columns + the revision number column
// Adding the "key" element with all id columns...
Element keyMapping = mappingData.getFirst().addElement("key");
MetadataTools.addColumns(keyMapping, pc.getTable().getPrimaryKey().columnIterator());
MetadataTools.addColumn(keyMapping, verEntCfg.getRevisionFieldName(), null, 0, 0, null);
// ... and the revision number column, read from the revision info relation mapping.
keyMapping.add((Element) cloneAndSetupRevisionInfoRelationMapping().element("column").clone());
break;
case TABLE_PER_CLASS:

View File

@ -0,0 +1,61 @@
/*
* 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.reventity;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.integration.inheritance.joined.ChildEntity;
import org.hibernate.envers.test.integration.inheritance.joined.ParentEntity;
import org.hibernate.mapping.Column;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import java.util.Iterator;
/**
* A join-inheritance test using a custom revision entity where the revision number is a long, mapped in the database
* as an int.
* @author Adam Warski (adam at warski dot org)
*/
public class LongRevEntityInheritanceChildAuditing extends AbstractEntityTest {
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(LongRevNumberRevEntity.class);
cfg.addAnnotatedClass(ChildEntity.class);
cfg.addAnnotatedClass(ParentEntity.class);
}
@Test
public void testChildRevColumnType() {
// We need the second column
Iterator childEntityKeyColumnsIterator = getCfg()
.getClassMapping("org.hibernate.envers.test.integration.inheritance.joined.ChildEntity_AUD")
.getKey()
.getColumnIterator();
childEntityKeyColumnsIterator.next();
Column second = (Column) childEntityKeyColumnsIterator.next();
assertEquals(second.getSqlType(), "int");
}
}

View File

@ -26,6 +26,7 @@ package org.hibernate.envers.test.integration.reventity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Column;
import org.hibernate.envers.RevisionEntity;
import org.hibernate.envers.RevisionNumber;
@ -40,6 +41,7 @@ public class LongRevNumberRevEntity {
@Id
@GeneratedValue
@RevisionNumber
@Column(columnDefinition = "int")
private long customId;
@RevisionTimestamp