Apply read and write expressions to the revision entity.

This commit is contained in:
Brian Parker 2011-02-12 15:10:41 -06:00 committed by Brian Parker
parent f6da72130b
commit 73729c97e3
6 changed files with 108 additions and 9 deletions

View File

@ -81,11 +81,11 @@ public class RevisionInfoConfiguration {
Element idProperty = MetadataTools.addNativelyGeneratedId(class_mapping, revisionInfoIdData.getName(), Element idProperty = MetadataTools.addNativelyGeneratedId(class_mapping, revisionInfoIdData.getName(),
revisionPropType); revisionPropType);
MetadataTools.addColumn(idProperty, "REV", null, 0, 0, null); MetadataTools.addColumn(idProperty, "REV", null, 0, 0, null, null, null);
Element timestampProperty = MetadataTools.addProperty(class_mapping, revisionInfoTimestampData.getName(), Element timestampProperty = MetadataTools.addProperty(class_mapping, revisionInfoTimestampData.getName(),
revisionInfoTimestampType.getName(), true, false); revisionInfoTimestampType.getName(), true, false);
MetadataTools.addColumn(timestampProperty, "REVTSTMP", null, 0, 0, null); MetadataTools.addColumn(timestampProperty, "REVTSTMP", null, 0, 0, null, null, null);
return document; return document;
} }
@ -98,7 +98,7 @@ public class RevisionInfoConfiguration {
if (revisionPropSqlType != null) { if (revisionPropSqlType != null) {
// Putting a fake name to make Hibernate happy. It will be replaced later anyway. // Putting a fake name to make Hibernate happy. It will be replaced later anyway.
MetadataTools.addColumn(rev_rel_mapping, "*" , null, 0, 0, revisionPropSqlType); MetadataTools.addColumn(rev_rel_mapping, "*" , null, 0, 0, revisionPropSqlType, null, null);
} }
return rev_rel_mapping; return rev_rel_mapping;
@ -310,4 +310,4 @@ class RevisionInfoConfigurationResult {
return revisionInfoTimestampData; return revisionInfoTimestampData;
} }
} }

View File

@ -159,7 +159,7 @@ public final class AuditMetadataGenerator {
// add a column for the timestamp of the end revision // add a column for the timestamp of the end revision
String revisionInfoTimestampSqlType = TimestampType.INSTANCE.getName(); String revisionInfoTimestampSqlType = TimestampType.INSTANCE.getName();
Element timestampProperty = MetadataTools.addProperty(any_mapping, verEntCfg.getRevisionEndTimestampFieldName(), revisionInfoTimestampSqlType, true, true, false); Element timestampProperty = MetadataTools.addProperty(any_mapping, verEntCfg.getRevisionEndTimestampFieldName(), revisionInfoTimestampSqlType, true, true, false);
MetadataTools.addColumn(timestampProperty, verEntCfg.getRevisionEndTimestampFieldName(), 0, 0, 0, null); MetadataTools.addColumn(timestampProperty, verEntCfg.getRevisionEndTimestampFieldName(), 0, 0, 0, null, null, null);
} }
} }
} }
@ -301,7 +301,7 @@ public final class AuditMetadataGenerator {
Element joinKey = joinElement.addElement("key"); Element joinKey = joinElement.addElement("key");
MetadataTools.addColumns(joinKey, join.getKey().getColumnIterator()); MetadataTools.addColumns(joinKey, join.getKey().getColumnIterator());
MetadataTools.addColumn(joinKey, verEntCfg.getRevisionFieldName(), null, 0, 0, null); MetadataTools.addColumn(joinKey, verEntCfg.getRevisionFieldName(), null, 0, 0, null, null, null);
} }
} }

View File

@ -85,7 +85,7 @@ public class MetadataTools {
Element column_mapping = parent.element("column"); Element column_mapping = parent.element("column");
if (column_mapping == null) { if (column_mapping == null) {
return addColumn(parent, name, null, 0, 0, null); return addColumn(parent, name, null, 0, 0, null, null, null);
} }
if (!StringTools.isEmpty(name)) { if (!StringTools.isEmpty(name)) {
@ -96,7 +96,7 @@ public class MetadataTools {
} }
public static Element addColumn(Element parent, String name, Integer length, Integer scale, Integer precision, public static Element addColumn(Element parent, String name, Integer length, Integer scale, Integer precision,
String sqlType) { String sqlType, String customRead, String customWrite) {
Element column_mapping = parent.addElement("column"); Element column_mapping = parent.addElement("column");
column_mapping.addAttribute("name", name); column_mapping.addAttribute("name", name);
@ -113,6 +113,13 @@ public class MetadataTools {
column_mapping.addAttribute("sql-type", sqlType); column_mapping.addAttribute("sql-type", sqlType);
} }
if (!StringTools.isEmpty(customRead)) {
column_mapping.addAttribute("read", customRead);
}
if (!StringTools.isEmpty(customWrite)) {
column_mapping.addAttribute("write", customWrite);
}
return column_mapping; return column_mapping;
} }
@ -180,7 +187,7 @@ public class MetadataTools {
while (columns.hasNext()) { while (columns.hasNext()) {
Column column = columns.next(); Column column = columns.next();
addColumn(any_mapping, column.getName(), column.getLength(), column.getScale(), column.getPrecision(), addColumn(any_mapping, column.getName(), column.getLength(), column.getScale(), column.getPrecision(),
column.getSqlType()); column.getSqlType(), column.getCustomRead(), column.getCustomWrite());
} }
} }

View File

@ -0,0 +1,52 @@
package org.hibernate.envers.test.integration.readwriteexpression;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.integration.basic.BasicTestEntity2;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import javax.persistence.EntityManager;
import java.util.List;
public class ReadWriteExpressionChange extends AbstractEntityTest {
private static final double HEIGHT_INCHES = 73;
private static final double HEIGHT_CENTIMETERS = HEIGHT_INCHES * 2.54d;
private Integer id;
@Override
public void configure(Ejb3Configuration cfg) {
cfg.addAnnotatedClass(Staff.class);
}
@BeforeClass(dependsOnMethods = "init")
public void initData() {
EntityManager em = getEntityManager();
em.getTransaction().begin();
Staff staff = new Staff(HEIGHT_INCHES, 1);
em.persist(staff);
em.getTransaction().commit();
id = staff.getId();
}
@Test
public void shouldRespectWriteExpression() {
EntityManager em = getEntityManager();
List resultList = em.createNativeQuery("select size_in_cm from t_staff_AUD where id ="+id).getResultList();
assert 1 == resultList.size();
Double sizeInCm = (Double) resultList.get(0);
assert sizeInCm.equals(HEIGHT_CENTIMETERS);
}
@Test
public void shouldRespectReadExpression() {
List<Number> revisions = getAuditReader().getRevisions(Staff.class, id);
assert 1 == revisions.size();
Number number = revisions.get(0);
Staff staffRev = getAuditReader().find(Staff.class, id, number);
assert HEIGHT_INCHES == staffRev.getSizeInInches();
}
}

View File

@ -0,0 +1,39 @@
package org.hibernate.envers.test.integration.readwriteexpression;
import org.hibernate.annotations.ColumnTransformer;
import org.hibernate.envers.Audited;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="t_staff")
public class Staff {
public Staff() {
}
public Staff(double sizeInInches, Integer id) {
this.sizeInInches = sizeInInches;
this.id = id;
}
@Id
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
private Integer id;
@Audited
@Column(name="size_in_cm")
@ColumnTransformer(
forColumn = "size_in_cm",
read = "size_in_cm / 2.54E0",
write = "? * 2.54E0" )
public double getSizeInInches() { return sizeInInches; }
public void setSizeInInches(double sizeInInches) { this.sizeInInches = sizeInInches; }
private double sizeInInches;
}

View File

@ -62,6 +62,7 @@
<package name="org.hibernate.envers.test.integration.proxy" /> <package name="org.hibernate.envers.test.integration.proxy" />
<package name="org.hibernate.envers.test.integration.query" /> <package name="org.hibernate.envers.test.integration.query" />
<package name="org.hibernate.envers.test.integration.query.ids" /> <package name="org.hibernate.envers.test.integration.query.ids" />
<package name="org.hibernate.envers.test.integration.readwriteexpression" />
<package name="org.hibernate.envers.test.integration.reference" /> <package name="org.hibernate.envers.test.integration.reference" />
<package name="org.hibernate.envers.test.integration.reventity" /> <package name="org.hibernate.envers.test.integration.reventity" />
<package name="org.hibernate.envers.test.integration.revfordate" /> <package name="org.hibernate.envers.test.integration.revfordate" />