redefine AttributeAccessor in terms of the new AttributeBinder API

This commit is contained in:
Gavin King 2021-09-11 17:18:06 +02:00 committed by Steve Ebersole
parent 5837a60e71
commit 2a2bf17f8f
6 changed files with 48 additions and 21 deletions

View File

@ -7,12 +7,13 @@
package org.hibernate.annotations;
import org.hibernate.property.access.spi.PropertyAccessStrategy;
import org.hibernate.tuple.AttributeAccessorBinder;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
@ -40,8 +41,9 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
* @author Steve Ebersole
* @author Emmanuel Bernard
*/
@java.lang.annotation.Target({ TYPE, METHOD, FIELD })
@Target({METHOD, FIELD})
@Retention(RUNTIME)
@AttributeBinderType(binder = AttributeAccessorBinder.class)
public @interface AttributeAccessor {
/**
* Names the {@link PropertyAccessStrategy} strategy.

View File

@ -15,7 +15,6 @@ import jakarta.persistence.Lob;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.annotations.AttributeAccessor;
import org.hibernate.annotations.AttributeBinderType;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.Immutable;
@ -314,21 +313,6 @@ public class PropertyBinder {
if ( property != null ) {
prop.setValueGenerationStrategy( determineValueGenerationStrategy( property ) );
if ( property.isAnnotationPresent( AttributeAccessor.class ) ) {
final AttributeAccessor accessor = property.getAnnotation( AttributeAccessor.class );
String value = accessor.value();
Class<?> type = accessor.strategy();
if ( !value.isEmpty() ) {
prop.setPropertyAccessorName( value );
}
else if ( !PropertyAccessStrategy.class.equals(type) ) {
prop.setPropertyAccessorName( type.getName() );
}
else {
throw new AnnotationException("@AttributeAccessor must specify a PropertyAccessStrategy type");
}
}
}
NaturalId naturalId = property != null ? property.getAnnotation( NaturalId.class ) : null;

View File

@ -0,0 +1,41 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.tuple;
import org.hibernate.AnnotationException;
import org.hibernate.annotations.AttributeAccessor;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.property.access.spi.PropertyAccessStrategy;
/**
* Configures the {@link PropertyAccessStrategy} for an attribute.
*
* @author Gavin King
*/
public class AttributeAccessorBinder implements AttributeBinder<AttributeAccessor> {
@Override
public void bind(
AttributeAccessor accessor,
MetadataBuildingContext buildingContext,
PersistentClass persistentClass,
Property property) {
String value = accessor.value();
Class<?> type = accessor.strategy();
if ( !value.isEmpty() ) {
property.setPropertyAccessorName( value );
}
else if ( !PropertyAccessStrategy.class.equals(type) ) {
property.setPropertyAccessorName( type.getName() );
}
else {
throw new AnnotationException("@AttributeAccessor must specify a PropertyAccessStrategy type");
}
}
}

View File

@ -23,7 +23,7 @@ import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
/**
* Sets up filters associated with a @TenantId field
* Sets up filters associated with a {@link TenantId} field
*
* @author Gavin King
*/

View File

@ -83,7 +83,7 @@ public class AttributeAccessorTest {
this.id = id;
}
@AttributeAccessor(strategy =BasicAttributeAccessor.class)
@AttributeAccessor(strategy = BasicAttributeAccessor.class)
public String getName() {
return name;
}

View File

@ -78,7 +78,7 @@ public class AttributeAccessorTest extends BaseEnversJPAFunctionalTestCase {
this.id = id;
}
@AttributeAccessor( strategy =BasicAttributeAccessor.class )
@AttributeAccessor( strategy = BasicAttributeAccessor.class )
public String getName() {
return name;
}