From 768088881b1713b6624fe01ada92dfa0816be34c Mon Sep 17 00:00:00 2001 From: Emmanuel Bernard Date: Wed, 29 Sep 2010 09:12:51 +0000 Subject: [PATCH] HHH-4510 Allow forColumn to be left if a property has a single column git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@20746 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../annotations/ReadWriteExpression.java | 2 +- .../java/org/hibernate/cfg/Ejb3Column.java | 3 ++- .../docbook/en-US/content/basic_mapping.xml | 27 +++++++++++++++---- .../various/readwriteexpression/Staff.java | 2 -- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/org/hibernate/annotations/ReadWriteExpression.java b/core/src/main/java/org/hibernate/annotations/ReadWriteExpression.java index fe52e87daf..5ebb85f204 100644 --- a/core/src/main/java/org/hibernate/annotations/ReadWriteExpression.java +++ b/core/src/main/java/org/hibernate/annotations/ReadWriteExpression.java @@ -44,7 +44,7 @@ public @interface ReadWriteExpression { /** * (Logical) column name for which the expression is used */ - String forColumn(); + String forColumn() default ""; /** * Custom SQL expression used to read from the column diff --git a/core/src/main/java/org/hibernate/cfg/Ejb3Column.java b/core/src/main/java/org/hibernate/cfg/Ejb3Column.java index 7934db4535..ce98343cc1 100644 --- a/core/src/main/java/org/hibernate/cfg/Ejb3Column.java +++ b/core/src/main/java/org/hibernate/cfg/Ejb3Column.java @@ -495,7 +495,8 @@ public class Ejb3Column { } private void processExpression(ReadWriteExpression annotation) { - if ( annotation != null && annotation.forColumn().equals( logicalColumnName ) ) { + String nonNullLogicalColumnName = logicalColumnName != null ? logicalColumnName : ""; //use the default for annotations + if ( annotation != null && annotation.forColumn().equals( nonNullLogicalColumnName ) ) { readExpression = annotation.read(); if ( StringHelper.isEmpty( readExpression ) ) { readExpression = null; diff --git a/documentation/manual/src/main/docbook/en-US/content/basic_mapping.xml b/documentation/manual/src/main/docbook/en-US/content/basic_mapping.xml index 269b15757c..be94378e03 100644 --- a/documentation/manual/src/main/docbook/en-US/content/basic_mapping.xml +++ b/documentation/manual/src/main/docbook/en-US/content/basic_mapping.xml @@ -5794,7 +5794,6 @@ class LineItem { class CreditCard { @Column(name="credit_card_num") @ReadWriteExpression( - forColumn="credit_card_num", read="decrypt(credit_card_num)", write="encrypt(?)") public String getCreditCardNumber() { return creditCardNumber; } @@ -5812,12 +5811,30 @@ class CreditCard { </property> - When using @ReadWriteExpression, you must - explicitly declare the @Column.name property. You can - use the plural form @ReadWriteExpressions if more - than one columns need to define either of these rules. + You can use the plural form + @ReadWriteExpressions if more than one columns + need to define either of these rules. + If a property uses more that one column, you must use the + forColumn attribute to specify which column, the + expressions are targeting. + + @Entity +class User { + @Type(type="com.acme.type.CreditCardType") + @Columns( { + @Column(name="credit_card_num"), + @Column(name="exp_date") } ) + @ReadWriteExpression( + forColumn="credit_card_num", + read="decrypt(credit_card_num)", + write="encrypt(?)") + public CreditCard getCreditCard() { return creditCard; } + public void setCreditCard(CreditCard card) { this.creditCard = card; } + private CreditCard creditCard; +} + Hibernate applies the custom expressions automatically whenever the property is referenced in a query. This functionality is similar to a derived-property formula with two differences: 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 8241422b4e..b591af2006 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 @@ -57,9 +57,7 @@ public class Staff { public void setSizeInInches(double sizeInInches) { this.sizeInInches = sizeInInches; } private double sizeInInches; - @Column(name="radiusS") @ReadWriteExpression( - forColumn = "radiusS", read = "radiusS / 2.54", write = "? * 2.54" ) public double getRadiusS() { return radiusS; }