HHH-6520 Some formatting and method name changes. Also moving AbstractAttributeTypeResolver into the type sub-package

This commit is contained in:
Hardy Ferentschik 2011-08-01 14:35:15 +02:00
parent 3689e533fb
commit f2338af9eb
10 changed files with 371 additions and 371 deletions

View File

@ -46,9 +46,9 @@ import org.hibernate.metamodel.source.annotations.JPADotNames;
import org.hibernate.metamodel.source.annotations.JandexHelper;
import org.hibernate.metamodel.source.annotations.TypeEnumConversionHelper;
import org.hibernate.metamodel.source.annotations.attribute.type.AttributeTypeResolver;
import org.hibernate.metamodel.source.annotations.attribute.type.AttributeTypeResolverImpl;
import org.hibernate.metamodel.source.annotations.attribute.type.CompositeAttributeTypeResolver;
import org.hibernate.metamodel.source.annotations.attribute.type.EnumeratedTypeResolver;
import org.hibernate.metamodel.source.annotations.attribute.type.ExplicitAttributeTypeResolver;
import org.hibernate.metamodel.source.annotations.attribute.type.LobTypeResolver;
import org.hibernate.metamodel.source.annotations.attribute.type.TemporalTypeResolver;
@ -105,7 +105,7 @@ public class BasicAttribute extends MappedAttribute {
private final String customWriteFragment;
private final String customReadFragment;
private final String checkCondition;
private AttributeTypeResolver resolver;
private final AttributeTypeResolver resolver;
public static BasicAttribute createSimpleAttribute(String name,
Class<?> attributeType,
@ -127,7 +127,7 @@ public class BasicAttribute extends MappedAttribute {
annotations,
JPADotNames.EMBEDDED_ID
);
//if this attribute has either @Id or @EmbeddedId, then it is an id attribute
//if this attribute has either @Id or @EmbeddedId, then it is an id attribute
isId = ( idAnnotation != null || embeddedIdAnnotation != null );
AnnotationInstance versionAnnotation = JandexHelper.getSingleAnnotation( annotations, JPADotNames.VERSION );
@ -156,6 +156,7 @@ public class BasicAttribute extends MappedAttribute {
this.customReadFragment = readWrite[0];
this.customWriteFragment = readWrite[1];
this.checkCondition = parseCheckAnnotation();
this.resolver = getDefaultHibernateTypeResolver();
}
public final ColumnValues getColumnValues() {
@ -356,26 +357,22 @@ public class BasicAttribute extends MappedAttribute {
return generator;
}
@Override
public AttributeTypeResolver getHibernateTypeResolver() {
if ( resolver == null ) {
resolver = getDefaultHibernateTypeResolver();
}
return resolver;
}
@Override
public AttributeTypeResolver getHibernateTypeResolver() {
return resolver;
}
protected AttributeTypeResolver getDefaultHibernateTypeResolver() {
CompositeAttributeTypeResolver resolver = new CompositeAttributeTypeResolver(
new ExplicitAttributeTypeResolver(
this
)
);
resolver.addHibernateTypeResolver( new TemporalTypeResolver( this ) );
resolver.addHibernateTypeResolver( new LobTypeResolver( this ) );
resolver.addHibernateTypeResolver( new EnumeratedTypeResolver( this ) );
return resolver;
}
private AttributeTypeResolver getDefaultHibernateTypeResolver() {
CompositeAttributeTypeResolver resolver = new CompositeAttributeTypeResolver(
new AttributeTypeResolverImpl(
this
)
);
resolver.addHibernateTypeResolver( new TemporalTypeResolver( this ) );
resolver.addHibernateTypeResolver( new LobTypeResolver( this ) );
resolver.addHibernateTypeResolver( new EnumeratedTypeResolver( this ) );
return resolver;
}
}

View File

@ -22,7 +22,7 @@
* Boston, MA 02110-1301 USA
*/
package org.hibernate.metamodel.source.annotations.attribute;
package org.hibernate.metamodel.source.annotations.attribute.type;
import java.util.Collections;
import java.util.Map;
@ -30,39 +30,31 @@ import java.util.Map;
import org.jboss.jandex.AnnotationInstance;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.source.annotations.attribute.type.AttributeTypeResolver;
/**
* @author Strong Liu
*/
public abstract class AbstractAttributeTypeResolver implements AttributeTypeResolver {
protected abstract AnnotationInstance getAnnotationInstance();
protected abstract AnnotationInstance getTypeDeterminingAnnotationInstance();
protected abstract String resolveHibernateTypeName(AnnotationInstance annotationInstance);
protected abstract String resolveHibernateTypeName(AnnotationInstance annotationInstance);
protected Map<String, String> resolveHibernateTypeParameters(AnnotationInstance annotationInstance) {
return Collections.emptyMap();
}
// private String explicitHibernateTypeName;
// private Map<String,String> explicitHibernateTypeParameters;
/**
* An optional explicit hibernate type name specified via {@link org.hibernate.annotations.Type}.
*/
@Override
final public String getExplicitHibernateTypeName() {
return resolveHibernateTypeName( getAnnotationInstance() );
}
protected Map<String, String> resolveHibernateTypeParameters(AnnotationInstance annotationInstance) {
return Collections.emptyMap();
}
/**
* Optional type parameters. See {@link #getExplicitHibernateTypeName()}.
*/
@Override
final public Map<String, String> getExplicitHibernateTypeParameters() {
if ( StringHelper.isNotEmpty( getExplicitHibernateTypeName() ) ) {
return resolveHibernateTypeParameters( getAnnotationInstance() );
}
else {
return Collections.emptyMap();
}
}
@Override
final public String getExplicitHibernateTypeName() {
return resolveHibernateTypeName( getTypeDeterminingAnnotationInstance() );
}
@Override
final public Map<String, String> getExplicitHibernateTypeParameters() {
if ( StringHelper.isNotEmpty( getExplicitHibernateTypeName() ) ) {
return resolveHibernateTypeParameters( getTypeDeterminingAnnotationInstance() );
}
else {
return Collections.emptyMap();
}
}
}

View File

@ -27,9 +27,23 @@ package org.hibernate.metamodel.source.annotations.attribute.type;
import java.util.Map;
/**
* Determines explicit Hibernate type information for JPA mapped attributes when additional type information is
* provided via annotations like {@link javax.persistence.Lob}, {@link javax.persistence.Enumerated} and
* {@link javax.persistence.Temporal}.
*
* @author Strong Liu
*/
public interface AttributeTypeResolver {
String getExplicitHibernateTypeName();
Map<String, String> getExplicitHibernateTypeParameters();
/**
* @return returns an explicit hibernate type name in case the mapped attribute has an additional
* {@link org.hibernate.annotations.Type} annotation or an implicit type is given via the use of annotations like
* {@link javax.persistence.Lob}, {@link javax.persistence.Enumerated} and
* {@link javax.persistence.Temporal}.
*/
String getExplicitHibernateTypeName();
/**
* @return Returns a map of optional type parameters. See {@link #getExplicitHibernateTypeName()}.
*/
Map<String, String> getExplicitHibernateTypeParameters();
}

View File

@ -0,0 +1,79 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. 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.metamodel.source.annotations.attribute.type;
import java.util.HashMap;
import java.util.Map;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
import org.hibernate.metamodel.source.annotations.HibernateDotNames;
import org.hibernate.metamodel.source.annotations.JandexHelper;
import org.hibernate.metamodel.source.annotations.attribute.MappedAttribute;
/**
* @author Strong Liu
*/
public class AttributeTypeResolverImpl extends AbstractAttributeTypeResolver {
private final MappedAttribute mappedAttribute;
public AttributeTypeResolverImpl(MappedAttribute mappedAttribute) {
this.mappedAttribute = mappedAttribute;
}
@Override
protected String resolveHibernateTypeName(AnnotationInstance typeAnnotation) {
String typeName = null;
if ( typeAnnotation != null ) {
typeName = JandexHelper.getValue( typeAnnotation, "type", String.class );
}
return typeName;
}
@Override
protected Map<String, String> resolveHibernateTypeParameters(AnnotationInstance typeAnnotation) {
HashMap<String, String> typeParameters = new HashMap<String, String>();
AnnotationValue parameterAnnotationValue = typeAnnotation.value( "parameters" );
if ( parameterAnnotationValue != null ) {
AnnotationInstance[] parameterAnnotations = parameterAnnotationValue.asNestedArray();
for ( AnnotationInstance parameterAnnotationInstance : parameterAnnotations ) {
typeParameters.put(
JandexHelper.getValue( parameterAnnotationInstance, "name", String.class ),
JandexHelper.getValue( parameterAnnotationInstance, "value", String.class )
);
}
}
return typeParameters;
}
@Override
protected AnnotationInstance getTypeDeterminingAnnotationInstance() {
return JandexHelper.getSingleAnnotation(
mappedAttribute.annotations(),
HibernateDotNames.TYPE
);
}
}

View File

@ -36,48 +36,48 @@ import org.hibernate.internal.util.collections.CollectionHelper;
* @author Strong Liu
*/
public class CompositeAttributeTypeResolver implements AttributeTypeResolver {
private List<AttributeTypeResolver> resolvers = new ArrayList<AttributeTypeResolver>();
private final ExplicitAttributeTypeResolver explicitHibernateTypeResolver;
private List<AttributeTypeResolver> resolvers = new ArrayList<AttributeTypeResolver>();
private final AttributeTypeResolverImpl explicitHibernateTypeResolver;
public CompositeAttributeTypeResolver(ExplicitAttributeTypeResolver explicitHibernateTypeResolver) {
if ( explicitHibernateTypeResolver == null ) {
throw new AssertionFailure( "The Given AttributeTypeResolver is null." );
}
this.explicitHibernateTypeResolver = explicitHibernateTypeResolver;
}
public CompositeAttributeTypeResolver(AttributeTypeResolverImpl explicitHibernateTypeResolver) {
if ( explicitHibernateTypeResolver == null ) {
throw new AssertionFailure( "The Given AttributeTypeResolver is null." );
}
this.explicitHibernateTypeResolver = explicitHibernateTypeResolver;
}
public void addHibernateTypeResolver(AttributeTypeResolver resolver) {
if ( resolver == null ) {
throw new AssertionFailure( "The Given AttributeTypeResolver is null." );
}
resolvers.add( resolver );
}
public void addHibernateTypeResolver(AttributeTypeResolver resolver) {
if ( resolver == null ) {
throw new AssertionFailure( "The Given AttributeTypeResolver is null." );
}
resolvers.add( resolver );
}
@Override
public String getExplicitHibernateTypeName() {
String type = explicitHibernateTypeResolver.getExplicitHibernateTypeName();
if ( StringHelper.isEmpty( type ) ) {
for ( AttributeTypeResolver resolver : resolvers ) {
type = resolver.getExplicitHibernateTypeName();
if ( StringHelper.isNotEmpty( type ) ) {
break;
}
}
}
return type;
}
@Override
public String getExplicitHibernateTypeName() {
String type = explicitHibernateTypeResolver.getExplicitHibernateTypeName();
if ( StringHelper.isEmpty( type ) ) {
for ( AttributeTypeResolver resolver : resolvers ) {
type = resolver.getExplicitHibernateTypeName();
if ( StringHelper.isNotEmpty( type ) ) {
break;
}
}
}
return type;
}
@Override
public Map<String, String> getExplicitHibernateTypeParameters() {
Map<String, String> parameters = explicitHibernateTypeResolver.getExplicitHibernateTypeParameters();
if ( CollectionHelper.isEmpty( parameters ) ) {
for ( AttributeTypeResolver resolver : resolvers ) {
parameters = resolver.getExplicitHibernateTypeParameters();
if ( CollectionHelper.isNotEmpty( parameters ) ) {
break;
}
}
}
return parameters;
}
@Override
public Map<String, String> getExplicitHibernateTypeParameters() {
Map<String, String> parameters = explicitHibernateTypeResolver.getExplicitHibernateTypeParameters();
if ( CollectionHelper.isEmpty( parameters ) ) {
for ( AttributeTypeResolver resolver : resolvers ) {
parameters = resolver.getExplicitHibernateTypeParameters();
if ( CollectionHelper.isNotEmpty( parameters ) ) {
break;
}
}
}
return parameters;
}
}

View File

@ -34,7 +34,6 @@ import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
import org.hibernate.metamodel.source.annotations.JPADotNames;
import org.hibernate.metamodel.source.annotations.JandexHelper;
import org.hibernate.metamodel.source.annotations.attribute.AbstractAttributeTypeResolver;
import org.hibernate.metamodel.source.annotations.attribute.MappedAttribute;
import org.hibernate.type.EnumType;
@ -42,81 +41,62 @@ import org.hibernate.type.EnumType;
* @author Strong Liu
*/
public class EnumeratedTypeResolver extends AbstractAttributeTypeResolver {
private final MappedAttribute mappedAttribute;
private final boolean isMapKey;
private final MappedAttribute mappedAttribute;
private final boolean isMapKey;
public EnumeratedTypeResolver(MappedAttribute mappedAttribute) {
if ( mappedAttribute == null ) {
throw new AssertionFailure( "MappedAttribute is null" );
}
this.mappedAttribute = mappedAttribute;
this.isMapKey = false;//todo
}
public EnumeratedTypeResolver(MappedAttribute mappedAttribute) {
if ( mappedAttribute == null ) {
throw new AssertionFailure( "MappedAttribute is null" );
}
this.mappedAttribute = mappedAttribute;
this.isMapKey = false;//todo
}
@Override
protected AnnotationInstance getAnnotationInstance() {
return JandexHelper.getSingleAnnotation(
mappedAttribute.annotations(),
JPADotNames.ENUMERATED
);
}
@Override
protected AnnotationInstance getTypeDeterminingAnnotationInstance() {
return JandexHelper.getSingleAnnotation(
mappedAttribute.annotations(),
JPADotNames.ENUMERATED
);
}
@Override
public String resolveHibernateTypeName(AnnotationInstance enumeratedAnnotation) {
boolean isEnum = mappedAttribute.getAttributeType().isEnum();
if ( !isEnum ) {
if ( enumeratedAnnotation != null ) {
throw new AnnotationException( "Attribute " + mappedAttribute.getName() + " is not a Enumerated type, but has a @Enumerated annotation." );
}
else {
return null;
}
}
return EnumType.class.getName();
}
@Override
protected Map<String, String> resolveHibernateTypeParameters(AnnotationInstance annotationInstance) {
HashMap<String, String> typeParameters = new HashMap<String, String>();
typeParameters.put( EnumType.ENUM, mappedAttribute.getAttributeType().getName() );
if ( annotationInstance != null ) {
javax.persistence.EnumType enumType = JandexHelper.getEnumValue(
annotationInstance,
"value",
javax.persistence.EnumType.class
);
if ( javax.persistence.EnumType.ORDINAL.equals( enumType ) ) {
typeParameters.put( EnumType.TYPE, String.valueOf( Types.INTEGER ) );
}
else if ( javax.persistence.EnumType.STRING.equals( enumType ) ) {
typeParameters.put( EnumType.TYPE, String.valueOf( Types.VARCHAR ) );
}
else {
throw new AssertionFailure( "Unknown EnumType: " + enumType );
}
}
else {
typeParameters.put( EnumType.TYPE, String.valueOf( Types.INTEGER ) );
}
//todo
// Schema schema = mappedAttribute.getContext().getMetadataImplementor().getDatabase().getDefaultSchema();
// Identifier schemaIdentifier = schema.getName().getSchema();
// Identifier catalogIdentifier = schema.getName().getCatalog();
// String schemaName = schemaIdentifier == null ? "" : schemaIdentifier.getName();
// String catalogName = catalogIdentifier == null ? "" : catalogIdentifier.getName();
// typeParameters.put( EnumType.SCHEMA, schemaName );
// typeParameters.put( EnumType.CATALOG, catalogName );
/**
String schema = columns[0].getTable().getSchema();
schema = schema == null ? "" : schema;
String catalog = columns[0].getTable().getCatalog();
catalog = catalog == null ? "" : catalog;
typeParameters.setProperty( EnumType.SCHEMA, schema );
typeParameters.setProperty( EnumType.CATALOG, catalog );
typeParameters.setProperty( EnumType.TABLE, columns[0].getTable().getName() );
typeParameters.setProperty( EnumType.COLUMN, columns[0].getName() );
*/
return typeParameters;
}
@Override
public String resolveHibernateTypeName(AnnotationInstance enumeratedAnnotation) {
boolean isEnum = mappedAttribute.getAttributeType().isEnum();
if ( !isEnum ) {
if ( enumeratedAnnotation != null ) {
throw new AnnotationException( "Attribute " + mappedAttribute.getName() + " is not a Enumerated type, but has a @Enumerated annotation." );
}
else {
return null;
}
}
return EnumType.class.getName();
}
@Override
protected Map<String, String> resolveHibernateTypeParameters(AnnotationInstance annotationInstance) {
HashMap<String, String> typeParameters = new HashMap<String, String>();
typeParameters.put( EnumType.ENUM, mappedAttribute.getAttributeType().getName() );
if ( annotationInstance != null ) {
javax.persistence.EnumType enumType = JandexHelper.getEnumValue(
annotationInstance,
"value",
javax.persistence.EnumType.class
);
if ( javax.persistence.EnumType.ORDINAL.equals( enumType ) ) {
typeParameters.put( EnumType.TYPE, String.valueOf( Types.INTEGER ) );
}
else if ( javax.persistence.EnumType.STRING.equals( enumType ) ) {
typeParameters.put( EnumType.TYPE, String.valueOf( Types.VARCHAR ) );
}
else {
throw new AssertionFailure( "Unknown EnumType: " + enumType );
}
}
else {
typeParameters.put( EnumType.TYPE, String.valueOf( Types.INTEGER ) );
}
return typeParameters;
}
}

View File

@ -1,86 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. 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.metamodel.source.annotations.attribute.type;
import java.util.HashMap;
import java.util.Map;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
import org.hibernate.metamodel.source.annotations.HibernateDotNames;
import org.hibernate.metamodel.source.annotations.JandexHelper;
import org.hibernate.metamodel.source.annotations.attribute.AbstractAttributeTypeResolver;
import org.hibernate.metamodel.source.annotations.attribute.MappedAttribute;
/**
* @author Strong Liu
*/
public class ExplicitAttributeTypeResolver extends AbstractAttributeTypeResolver {
private final MappedAttribute mappedAttribute;
public ExplicitAttributeTypeResolver(MappedAttribute mappedAttribute) {
this.mappedAttribute = mappedAttribute;
}
@Override
protected String resolveHibernateTypeName(AnnotationInstance typeAnnotation) {
String typeName = null;
if ( typeAnnotation != null ) {
typeName = JandexHelper.getValue( typeAnnotation, "type", String.class );
}
return typeName;
}
@Override
protected Map<String, String> resolveHibernateTypeParameters(AnnotationInstance typeAnnotation) {
HashMap<String, String> typeParameters = new HashMap<String, String>();
AnnotationValue parameterAnnotationValue = typeAnnotation.value( "parameters" );
if ( parameterAnnotationValue != null ) {
AnnotationInstance[] parameterAnnotations = parameterAnnotationValue.asNestedArray();
for ( AnnotationInstance parameterAnnotationInstance : parameterAnnotations ) {
typeParameters.put(
JandexHelper.getValue( parameterAnnotationInstance, "name", String.class ),
JandexHelper.getValue(
parameterAnnotationInstance,
"value",
String.class
)
);
}
}
return typeParameters;
}
@Override
protected AnnotationInstance getAnnotationInstance() {
return JandexHelper.getSingleAnnotation(
mappedAttribute.annotations(),
HibernateDotNames.TYPE
);
}
}

View File

@ -35,7 +35,6 @@ import org.jboss.jandex.AnnotationInstance;
import org.hibernate.AssertionFailure;
import org.hibernate.metamodel.source.annotations.JPADotNames;
import org.hibernate.metamodel.source.annotations.JandexHelper;
import org.hibernate.metamodel.source.annotations.attribute.AbstractAttributeTypeResolver;
import org.hibernate.metamodel.source.annotations.attribute.MappedAttribute;
import org.hibernate.type.CharacterArrayClobType;
import org.hibernate.type.PrimitiveCharacterArrayClobType;
@ -47,67 +46,66 @@ import org.hibernate.type.WrappedMaterializedBlobType;
* @author Strong Liu
*/
public class LobTypeResolver extends AbstractAttributeTypeResolver {
private final MappedAttribute mappedAttribute;
private final MappedAttribute mappedAttribute;
public LobTypeResolver(MappedAttribute mappedAttribute) {
if ( mappedAttribute == null ) {
throw new AssertionFailure( "MappedAttribute is null" );
}
this.mappedAttribute = mappedAttribute;
}
public LobTypeResolver(MappedAttribute mappedAttribute) {
if ( mappedAttribute == null ) {
throw new AssertionFailure( "MappedAttribute is null" );
}
this.mappedAttribute = mappedAttribute;
}
@Override
protected AnnotationInstance getTypeDeterminingAnnotationInstance() {
return JandexHelper.getSingleAnnotation( mappedAttribute.annotations(), JPADotNames.LOB );
}
@Override
protected AnnotationInstance getAnnotationInstance() {
return JandexHelper.getSingleAnnotation( mappedAttribute.annotations(), JPADotNames.LOB );
}
@Override
public String resolveHibernateTypeName(AnnotationInstance annotationInstance) {
if ( annotationInstance == null ) {
return null;
}
String type = null;
if ( Clob.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) {
type = StandardBasicTypes.CLOB.getName();
}
else if ( Blob.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) {
type = StandardBasicTypes.BLOB.getName();
}
else if ( String.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) {
type = StandardBasicTypes.MATERIALIZED_CLOB.getName();
}
else if ( Character[].class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) {
type = CharacterArrayClobType.class.getName();
}
else if ( char[].class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) {
type = PrimitiveCharacterArrayClobType.class.getName();
}
else if ( Byte[].class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) {
type = WrappedMaterializedBlobType.class.getName();
}
else if ( byte[].class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) {
type = StandardBasicTypes.MATERIALIZED_BLOB.getName();
}
else if ( Serializable.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) {
type = SerializableToBlobType.class.getName();
}
else {
type = "blob";
}
return type;
}
@Override
public String resolveHibernateTypeName(AnnotationInstance annotationInstance) {
if ( annotationInstance == null ) {
return null;
}
String type = null;
if ( Clob.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) {
type = StandardBasicTypes.CLOB.getName();
}
else if ( Blob.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) {
type = StandardBasicTypes.BLOB.getName();
}
else if ( String.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) {
type = StandardBasicTypes.MATERIALIZED_CLOB.getName();
}
else if ( Character[].class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) {
type = CharacterArrayClobType.class.getName();
}
else if ( char[].class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) {
type = PrimitiveCharacterArrayClobType.class.getName();
}
else if ( Byte[].class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) {
type = WrappedMaterializedBlobType.class.getName();
}
else if ( byte[].class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) {
type = StandardBasicTypes.MATERIALIZED_BLOB.getName();
}
else if ( Serializable.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) {
type = SerializableToBlobType.class.getName();
}
else {
type = "blob";
}
return type;
}
@Override
protected Map<String, String> resolveHibernateTypeParameters(AnnotationInstance annotationInstance) {
if ( getExplicitHibernateTypeName().equals( SerializableToBlobType.class.getName() ) ) {
HashMap<String, String> typeParameters = new HashMap<String, String>();
typeParameters.put(
SerializableToBlobType.CLASS_NAME,
mappedAttribute.getAttributeType().getName()
);
return typeParameters;
}
return null;
}
@Override
protected Map<String, String> resolveHibernateTypeParameters(AnnotationInstance annotationInstance) {
if ( getExplicitHibernateTypeName().equals( SerializableToBlobType.class.getName() ) ) {
HashMap<String, String> typeParameters = new HashMap<String, String>();
typeParameters.put(
SerializableToBlobType.CLASS_NAME,
mappedAttribute.getAttributeType().getName()
);
return typeParameters;
}
return null;
}
}

View File

@ -35,7 +35,6 @@ import org.hibernate.AssertionFailure;
import org.hibernate.cfg.NotYetImplementedException;
import org.hibernate.metamodel.source.annotations.JPADotNames;
import org.hibernate.metamodel.source.annotations.JandexHelper;
import org.hibernate.metamodel.source.annotations.attribute.AbstractAttributeTypeResolver;
import org.hibernate.metamodel.source.annotations.attribute.MappedAttribute;
import org.hibernate.type.StandardBasicTypes;
@ -43,67 +42,66 @@ import org.hibernate.type.StandardBasicTypes;
* @author Strong Liu
*/
public class TemporalTypeResolver extends AbstractAttributeTypeResolver {
private final MappedAttribute mappedAttribute;
private final boolean isMapKey;
private final MappedAttribute mappedAttribute;
private final boolean isMapKey;
public TemporalTypeResolver(MappedAttribute mappedAttribute) {
if ( mappedAttribute == null ) {
throw new AssertionFailure( "MappedAttribute is null" );
}
this.mappedAttribute = mappedAttribute;
this.isMapKey = false;//todo
}
public TemporalTypeResolver(MappedAttribute mappedAttribute) {
if ( mappedAttribute == null ) {
throw new AssertionFailure( "MappedAttribute is null" );
}
this.mappedAttribute = mappedAttribute;
this.isMapKey = false;//todo
}
@Override
public String resolveHibernateTypeName(AnnotationInstance temporalAnnotation) {
@Override
public String resolveHibernateTypeName(AnnotationInstance temporalAnnotation) {
if ( isTemporalType( mappedAttribute.getAttributeType() ) ) {
if ( temporalAnnotation == null ) {
//SPEC 11.1.47 The Temporal annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar.
throw new AnnotationException( "Attribute " + mappedAttribute.getName() + " is a Temporal type, but no @Temporal annotation found." );
}
TemporalType temporalType = JandexHelper.getEnumValue( temporalAnnotation, "value", TemporalType.class );
boolean isDate = Date.class.isAssignableFrom( mappedAttribute.getAttributeType() );
String type = null;
switch ( temporalType ) {
case DATE:
type = isDate ? StandardBasicTypes.DATE.getName() : StandardBasicTypes.CALENDAR_DATE.getName();
break;
case TIME:
type = StandardBasicTypes.TIME.getName();
if ( !isDate ) {
throw new NotYetImplementedException( "Calendar cannot persist TIME only" );
}
break;
case TIMESTAMP:
type = isDate ? StandardBasicTypes.TIMESTAMP.getName() : StandardBasicTypes.CALENDAR.getName();
break;
default:
throw new AssertionFailure( "Unknown temporal type: " + temporalType );
}
return type;
}
else {
if ( temporalAnnotation != null ) {
throw new AnnotationException(
"@Temporal should only be set on a java.util.Date or java.util.Calendar property: " + mappedAttribute
.getName()
);
}
}
return null;
}
if ( isTemporalType( mappedAttribute.getAttributeType() ) ) {
if ( temporalAnnotation == null ) {
//SPEC 11.1.47 The Temporal annotation must be specified for persistent fields or properties of type java.util.Date and java.util.Calendar.
throw new AnnotationException( "Attribute " + mappedAttribute.getName() + " is a Temporal type, but no @Temporal annotation found." );
}
TemporalType temporalType = JandexHelper.getEnumValue( temporalAnnotation, "value", TemporalType.class );
boolean isDate = Date.class.isAssignableFrom( mappedAttribute.getAttributeType() );
String type;
switch ( temporalType ) {
case DATE:
type = isDate ? StandardBasicTypes.DATE.getName() : StandardBasicTypes.CALENDAR_DATE.getName();
break;
case TIME:
type = StandardBasicTypes.TIME.getName();
if ( !isDate ) {
throw new NotYetImplementedException( "Calendar cannot persist TIME only" );
}
break;
case TIMESTAMP:
type = isDate ? StandardBasicTypes.TIMESTAMP.getName() : StandardBasicTypes.CALENDAR.getName();
break;
default:
throw new AssertionFailure( "Unknown temporal type: " + temporalType );
}
return type;
}
else {
if ( temporalAnnotation != null ) {
throw new AnnotationException(
"@Temporal should only be set on a java.util.Date or java.util.Calendar property: " + mappedAttribute
.getName()
);
}
}
return null;
}
@Override
protected AnnotationInstance getAnnotationInstance() {
return JandexHelper.getSingleAnnotation(
mappedAttribute.annotations(),
JPADotNames.TEMPORAL
);
}
private static boolean isTemporalType(Class type) {
return Date.class.isAssignableFrom( type ) || Calendar.class.isAssignableFrom( type );
}
@Override
protected AnnotationInstance getTypeDeterminingAnnotationInstance() {
return JandexHelper.getSingleAnnotation(
mappedAttribute.annotations(),
JPADotNames.TEMPORAL
);
}
private static boolean isTemporalType(Class type) {
return Date.class.isAssignableFrom( type ) || Calendar.class.isAssignableFrom( type );
}
}

View File

@ -0,0 +1,28 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. 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.metamodel.source.annotations.attribute.type;
/**
* This package contains type binding code for basic attributes.
*/