HHH-4510 Merge @ReadExpression and @WriteExpression in @ReadWriteExpression

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@20729 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2010-09-27 22:09:02 +00:00
parent 51bf38d4b0
commit 7d167768d2
7 changed files with 47 additions and 146 deletions

View File

@ -1,51 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat, Inc. and/or its affiliates 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, Inc.
*
* 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.annotations;
import java.lang.annotation.Retention;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Custom SQL expression used to read the value from a column. True for direct object loading as well as queries.
*
* For example: <code>decrypt(credit_card_num)</code>
*
* @author Emmanuel Bernard
*/
@java.lang.annotation.Target({FIELD,METHOD})
@Retention(RUNTIME)
public @interface ReadExpression {
/**
* (Logical) column name for which the expression is used
*/
String forColumn();
/**
* Custom SQL expression used to read from the column
*/
String expression();
}

View File

@ -30,26 +30,30 @@ import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Custom SQL expression used to write the value to a column. True for direct object saving as well as insert queries.
* The write expression must contain exactly one '?' placeholder for the value.
*
* For example: <code>encrypt(?)</code>
*
*
* Custom SQL expression used to read the value from and write a value to a column.
* Use for direct object loading/saving as well as queries.
* The write expression must contain exactly one '?' placeholder for the value.
*
* For example: <code>read="decrypt(credit_card_num)" write="encrypt(?)"</code>
*
* @author Emmanuel Bernard
*/
@java.lang.annotation.Target({FIELD,METHOD})
@Retention(RUNTIME)
public @interface WriteExpression {
public @interface ReadWriteExpression {
/**
* (Logical) column name for which the expression is used
*/
String forColumn();
/**
* Custom SQL expression used to read from the column
*/
String read() default "";
/**
* Custom SQL expression used to write to the column.
* The write expression must contain exactly one '?' placeholder for the value.
*/
String expression();
String write() default "";
}

View File

@ -30,13 +30,13 @@ import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Plural annotation for @ReadExpression.
* Plural annotation for @ReadWriteExpressions.
* Useful when more than one column is using this behavior.
*
* @author Emmanuel Bernard
*/
@java.lang.annotation.Target({FIELD,METHOD})
@Retention(RUNTIME)
public @interface ReadExpressions {
ReadExpression[] value();
public @interface ReadWriteExpressions {
ReadWriteExpression[] value();
}

View File

@ -1,42 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat, Inc. and/or its affiliates 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, Inc.
*
* 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.annotations;
import java.lang.annotation.Retention;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Plural annotation for @WriteExpression.
* Useful when more than one column is using this behavior.
*
* @author Emmanuel Bernard
*/
@java.lang.annotation.Target({FIELD,METHOD})
@Retention(RUNTIME)
public @interface WriteExpressions {
WriteExpression[] value();
}

View File

@ -28,10 +28,8 @@ import java.util.Map;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
import org.hibernate.annotations.Index;
import org.hibernate.annotations.ReadExpression;
import org.hibernate.annotations.ReadExpressions;
import org.hibernate.annotations.WriteExpression;
import org.hibernate.annotations.WriteExpressions;
import org.hibernate.annotations.ReadWriteExpression;
import org.hibernate.annotations.ReadWriteExpressions;
import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.cfg.annotations.Nullability;
import org.hibernate.mapping.Column;
@ -485,37 +483,27 @@ public class Ejb3Column {
if ( inferredData != null ) {
XProperty property = inferredData.getProperty();
if ( property != null ) {
{
processReadExpression( property.getAnnotation( ReadExpression.class ) );
ReadExpressions annotations = property.getAnnotation( ReadExpressions.class );
if (annotations != null) {
for ( ReadExpression annotation : annotations.value() ) {
processReadExpression( annotation );
}
}
}
{
processWriteExpression( property.getAnnotation( WriteExpression.class ) );
WriteExpressions annotations = property.getAnnotation( WriteExpressions.class );
if (annotations != null) {
for ( WriteExpression annotation : annotations.value() ) {
processWriteExpression( annotation );
}
processExpression( property.getAnnotation( ReadWriteExpression.class ) );
ReadWriteExpressions annotations = property.getAnnotation( ReadWriteExpressions.class );
if (annotations != null) {
for ( ReadWriteExpression annotation : annotations.value() ) {
processExpression( annotation );
}
}
}
}
}
private void processReadExpression(ReadExpression annotation) {
private void processExpression(ReadWriteExpression annotation) {
if ( annotation != null && annotation.forColumn().equals( logicalColumnName ) ) {
readExpression = annotation.expression();
}
}
private void processWriteExpression(WriteExpression annotation) {
if ( annotation != null && annotation.forColumn().equals( logicalColumnName ) ) {
writeExpression = annotation.expression();
readExpression = annotation.read();
if ( StringHelper.isEmpty( readExpression ) ) {
readExpression = null;
}
writeExpression = annotation.write();
if ( StringHelper.isEmpty( writeExpression ) ) {
writeExpression = null;
}
}
}

View File

@ -5782,12 +5782,10 @@ class LineItem {
<programlisting role="JAVA">@Entity
class CreditCard {
@Column(name="credit_card_num")
@ReadExpression(
@ReadWriteExpression(
forColumn="credit_card_num",
expression="decrypt(credit_card_num)")
@WriteExpression(
forColumn="credit_card_num",
expression="encrypt(?)")
read="decrypt(credit_card_num)",
write="encrypt(?)")
public String getCreditCardNumber() { return creditCardNumber; }
public void setCreditCardNumber(String number) { this.creditCardNumber = number; }
private String creditCardNumber;
@ -5803,9 +5801,10 @@ class CreditCard {
&lt;/property&gt;</programlisting>
<note>
<para>When using <classname>@ReadExpression</classname> /
<classname>@WriteExpression</classname> annotations, you must explicitly
declare the <literal>@Column.name</literal> property.</para>
<para>When using <classname>@ReadWriteExpression</classname>, you must
explicitly declare the <literal>@Column.name</literal> property. You can
use the plural form <classname>@ReadWriteExpressions</classname> if more
than one columns need to define either of these rules.</para>
</note>
<para>Hibernate applies the custom expressions automatically whenever the

View File

@ -28,8 +28,7 @@ import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.ReadExpression;
import org.hibernate.annotations.WriteExpression;
import org.hibernate.annotations.ReadWriteExpression;
/**
* @author Emmanuel Bernard
@ -50,15 +49,19 @@ public class Staff {
private Integer id;
@Column(name="size_in_cm")
@ReadExpression( forColumn = "size_in_cm", expression = "size_in_cm / 2.54" )
@WriteExpression( forColumn = "size_in_cm", expression = "? * 2.54" )
@ReadWriteExpression(
forColumn = "size_in_cm",
read = "size_in_cm / 2.54",
write = "? * 2.54" )
public double getSizeInInches() { return sizeInInches; }
public void setSizeInInches(double sizeInInches) { this.sizeInInches = sizeInInches; }
private double sizeInInches;
@Column(name="radiusS")
@ReadExpression( forColumn = "radiusS", expression = "radiusS / 2.54" )
@WriteExpression( forColumn = "radiusS", expression = "? * 2.54" )
@ReadWriteExpression(
forColumn = "radiusS",
read = "radiusS / 2.54",
write = "? * 2.54" )
public double getRadiusS() { return radiusS; }
public void setRadiusS(double radiusS) { this.radiusS = radiusS; }
private double radiusS;