diff --git a/envers/src/main/java/org/hibernate/envers/configuration/RevisionInfoConfiguration.java b/envers/src/main/java/org/hibernate/envers/configuration/RevisionInfoConfiguration.java index 6ad52516ee..9163b8c02e 100644 --- a/envers/src/main/java/org/hibernate/envers/configuration/RevisionInfoConfiguration.java +++ b/envers/src/main/java/org/hibernate/envers/configuration/RevisionInfoConfiguration.java @@ -81,11 +81,11 @@ public class RevisionInfoConfiguration { Element idProperty = MetadataTools.addNativelyGeneratedId(class_mapping, revisionInfoIdData.getName(), 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(), revisionInfoTimestampType.getName(), true, false); - MetadataTools.addColumn(timestampProperty, "REVTSTMP", null, 0, 0, null); + MetadataTools.addColumn(timestampProperty, "REVTSTMP", null, 0, 0, null, null, null); return document; } @@ -98,7 +98,7 @@ public class RevisionInfoConfiguration { 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, revisionPropSqlType); + MetadataTools.addColumn(rev_rel_mapping, "*" , null, 0, 0, revisionPropSqlType, null, null); } return rev_rel_mapping; @@ -310,4 +310,4 @@ class RevisionInfoConfigurationResult { return revisionInfoTimestampData; } -} \ No newline at end of file +} 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 38d7c79dbf..bc2d42913b 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 @@ -159,7 +159,7 @@ public final class AuditMetadataGenerator { // add a column for the timestamp of the end revision String revisionInfoTimestampSqlType = TimestampType.INSTANCE.getName(); 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"); 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); } } diff --git a/envers/src/main/java/org/hibernate/envers/configuration/metadata/MetadataTools.java b/envers/src/main/java/org/hibernate/envers/configuration/metadata/MetadataTools.java index 60992022f8..8275755063 100644 --- a/envers/src/main/java/org/hibernate/envers/configuration/metadata/MetadataTools.java +++ b/envers/src/main/java/org/hibernate/envers/configuration/metadata/MetadataTools.java @@ -85,7 +85,7 @@ public class MetadataTools { Element column_mapping = parent.element("column"); 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)) { @@ -96,7 +96,7 @@ public class MetadataTools { } 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"); column_mapping.addAttribute("name", name); @@ -113,6 +113,13 @@ public class MetadataTools { 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; } @@ -180,7 +187,7 @@ public class MetadataTools { while (columns.hasNext()) { Column column = columns.next(); addColumn(any_mapping, column.getName(), column.getLength(), column.getScale(), column.getPrecision(), - column.getSqlType()); + column.getSqlType(), column.getCustomRead(), column.getCustomWrite()); } } diff --git a/envers/src/test/java/org/hibernate/envers/test/integration/readwriteexpression/ReadWriteExpressionChange.java b/envers/src/test/java/org/hibernate/envers/test/integration/readwriteexpression/ReadWriteExpressionChange.java new file mode 100644 index 0000000000..73e3a05d87 --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/integration/readwriteexpression/ReadWriteExpressionChange.java @@ -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 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(); + } + +} diff --git a/envers/src/test/java/org/hibernate/envers/test/integration/readwriteexpression/Staff.java b/envers/src/test/java/org/hibernate/envers/test/integration/readwriteexpression/Staff.java new file mode 100644 index 0000000000..6a149b4656 --- /dev/null +++ b/envers/src/test/java/org/hibernate/envers/test/integration/readwriteexpression/Staff.java @@ -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; + + +} \ No newline at end of file diff --git a/envers/src/test/resources/testng.xml b/envers/src/test/resources/testng.xml index 905743a18a..83c7baa6c0 100644 --- a/envers/src/test/resources/testng.xml +++ b/envers/src/test/resources/testng.xml @@ -62,6 +62,7 @@ +