diff --git a/core/src/main/java/org/hibernate/cfg/Ejb3Column.java b/core/src/main/java/org/hibernate/cfg/Ejb3Column.java index ce98343cc1..d68859c4ad 100644 --- a/core/src/main/java/org/hibernate/cfg/Ejb3Column.java +++ b/core/src/main/java/org/hibernate/cfg/Ejb3Column.java @@ -496,7 +496,9 @@ public class Ejb3Column { private void processExpression(ReadWriteExpression annotation) { String nonNullLogicalColumnName = logicalColumnName != null ? logicalColumnName : ""; //use the default for annotations - if ( annotation != null && annotation.forColumn().equals( nonNullLogicalColumnName ) ) { + if ( annotation != null && + ( StringHelper.isEmpty( annotation.forColumn() ) + || annotation.forColumn().equals( nonNullLogicalColumnName ) ) ) { readExpression = annotation.read(); if ( StringHelper.isEmpty( readExpression ) ) { readExpression = null; diff --git a/testsuite/src/test/java/org/hibernate/test/annotations/various/readwriteexpression/ReadWriteExpressionTest.java b/testsuite/src/test/java/org/hibernate/test/annotations/various/readwriteexpression/ReadWriteExpressionTest.java index 5ca95b9379..415f67c1c4 100644 --- a/testsuite/src/test/java/org/hibernate/test/annotations/various/readwriteexpression/ReadWriteExpressionTest.java +++ b/testsuite/src/test/java/org/hibernate/test/annotations/various/readwriteexpression/ReadWriteExpressionTest.java @@ -23,8 +23,6 @@ */ package org.hibernate.test.annotations.various.readwriteexpression; -import java.util.Date; - import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.criterion.Restrictions; @@ -41,7 +39,7 @@ public class ReadWriteExpressionTest extends TestCase { final double HEIGHT_INCHES = 73; final double HEIGHT_CENTIMETERS = HEIGHT_INCHES * 2.54d; - Staff staff = new Staff(HEIGHT_INCHES, HEIGHT_INCHES, 1); + Staff staff = new Staff(HEIGHT_INCHES, HEIGHT_INCHES, HEIGHT_INCHES*2, 1); s.persist( staff ); s.flush(); @@ -52,6 +50,9 @@ public class ReadWriteExpressionTest extends TestCase { heightViaSql = (Double)s.createSQLQuery("select radiusS from t_staff where t_staff.id=1").uniqueResult(); assertEquals(HEIGHT_CENTIMETERS, heightViaSql, 0.01d); + heightViaSql = (Double)s.createSQLQuery("select diamet from t_staff where t_staff.id=1").uniqueResult(); + assertEquals(HEIGHT_CENTIMETERS*2, heightViaSql, 0.01d); + // Test projection Double heightViaHql = (Double)s.createQuery("select s.sizeInInches from Staff s where s.id = 1").uniqueResult(); assertEquals(HEIGHT_INCHES, heightViaHql, 0.01d); diff --git a/testsuite/src/test/java/org/hibernate/test/annotations/various/readwriteexpression/Staff.java b/testsuite/src/test/java/org/hibernate/test/annotations/various/readwriteexpression/Staff.java index b591af2006..b26000ce68 100644 --- a/testsuite/src/test/java/org/hibernate/test/annotations/various/readwriteexpression/Staff.java +++ b/testsuite/src/test/java/org/hibernate/test/annotations/various/readwriteexpression/Staff.java @@ -37,9 +37,10 @@ import org.hibernate.annotations.ReadWriteExpression; @Table(name="t_staff") public class Staff { - public Staff(double sizeInInches, double radius, Integer id) { + public Staff(double sizeInInches, double radius, double diameter, Integer id) { this.sizeInInches = sizeInInches; this.radiusS = radius; + this.diameter = diameter; this.id = id; } @@ -57,10 +58,19 @@ public class Staff { public void setSizeInInches(double sizeInInches) { this.sizeInInches = sizeInInches; } private double sizeInInches; + //Weird extra S to avoid potential SQL keywords @ReadWriteExpression( read = "radiusS / 2.54", write = "? * 2.54" ) public double getRadiusS() { return radiusS; } public void setRadiusS(double radiusS) { this.radiusS = radiusS; } private double radiusS; + + @Column(name="diamet") + @ReadWriteExpression( + read = "diamet / 2.54", + write = "? * 2.54" ) + public double getDiameter() { return diameter; } + public void setDiameter(double diameter) { this.diameter = diameter; } + private double diameter; }