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

View File

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

View File

@ -27,9 +27,23 @@ package org.hibernate.metamodel.source.annotations.attribute.type;
import java.util.Map; 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 * @author Strong Liu
*/ */
public interface AttributeTypeResolver { 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 * @author Strong Liu
*/ */
public class CompositeAttributeTypeResolver implements AttributeTypeResolver { public class CompositeAttributeTypeResolver implements AttributeTypeResolver {
private List<AttributeTypeResolver> resolvers = new ArrayList<AttributeTypeResolver>(); private List<AttributeTypeResolver> resolvers = new ArrayList<AttributeTypeResolver>();
private final ExplicitAttributeTypeResolver explicitHibernateTypeResolver; private final AttributeTypeResolverImpl explicitHibernateTypeResolver;
public CompositeAttributeTypeResolver(ExplicitAttributeTypeResolver explicitHibernateTypeResolver) { public CompositeAttributeTypeResolver(AttributeTypeResolverImpl explicitHibernateTypeResolver) {
if ( explicitHibernateTypeResolver == null ) { if ( explicitHibernateTypeResolver == null ) {
throw new AssertionFailure( "The Given AttributeTypeResolver is null." ); throw new AssertionFailure( "The Given AttributeTypeResolver is null." );
} }
this.explicitHibernateTypeResolver = explicitHibernateTypeResolver; this.explicitHibernateTypeResolver = explicitHibernateTypeResolver;
} }
public void addHibernateTypeResolver(AttributeTypeResolver resolver) { public void addHibernateTypeResolver(AttributeTypeResolver resolver) {
if ( resolver == null ) { if ( resolver == null ) {
throw new AssertionFailure( "The Given AttributeTypeResolver is null." ); throw new AssertionFailure( "The Given AttributeTypeResolver is null." );
} }
resolvers.add( resolver ); resolvers.add( resolver );
} }
@Override @Override
public String getExplicitHibernateTypeName() { public String getExplicitHibernateTypeName() {
String type = explicitHibernateTypeResolver.getExplicitHibernateTypeName(); String type = explicitHibernateTypeResolver.getExplicitHibernateTypeName();
if ( StringHelper.isEmpty( type ) ) { if ( StringHelper.isEmpty( type ) ) {
for ( AttributeTypeResolver resolver : resolvers ) { for ( AttributeTypeResolver resolver : resolvers ) {
type = resolver.getExplicitHibernateTypeName(); type = resolver.getExplicitHibernateTypeName();
if ( StringHelper.isNotEmpty( type ) ) { if ( StringHelper.isNotEmpty( type ) ) {
break; break;
} }
} }
} }
return type; return type;
} }
@Override @Override
public Map<String, String> getExplicitHibernateTypeParameters() { public Map<String, String> getExplicitHibernateTypeParameters() {
Map<String, String> parameters = explicitHibernateTypeResolver.getExplicitHibernateTypeParameters(); Map<String, String> parameters = explicitHibernateTypeResolver.getExplicitHibernateTypeParameters();
if ( CollectionHelper.isEmpty( parameters ) ) { if ( CollectionHelper.isEmpty( parameters ) ) {
for ( AttributeTypeResolver resolver : resolvers ) { for ( AttributeTypeResolver resolver : resolvers ) {
parameters = resolver.getExplicitHibernateTypeParameters(); parameters = resolver.getExplicitHibernateTypeParameters();
if ( CollectionHelper.isNotEmpty( parameters ) ) { if ( CollectionHelper.isNotEmpty( parameters ) ) {
break; break;
} }
} }
} }
return parameters; return parameters;
} }
} }

View File

@ -34,7 +34,6 @@ import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure; import org.hibernate.AssertionFailure;
import org.hibernate.metamodel.source.annotations.JPADotNames; import org.hibernate.metamodel.source.annotations.JPADotNames;
import org.hibernate.metamodel.source.annotations.JandexHelper; 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.metamodel.source.annotations.attribute.MappedAttribute;
import org.hibernate.type.EnumType; import org.hibernate.type.EnumType;
@ -42,81 +41,62 @@ import org.hibernate.type.EnumType;
* @author Strong Liu * @author Strong Liu
*/ */
public class EnumeratedTypeResolver extends AbstractAttributeTypeResolver { public class EnumeratedTypeResolver extends AbstractAttributeTypeResolver {
private final MappedAttribute mappedAttribute; private final MappedAttribute mappedAttribute;
private final boolean isMapKey; private final boolean isMapKey;
public EnumeratedTypeResolver(MappedAttribute mappedAttribute) { public EnumeratedTypeResolver(MappedAttribute mappedAttribute) {
if ( mappedAttribute == null ) { if ( mappedAttribute == null ) {
throw new AssertionFailure( "MappedAttribute is null" ); throw new AssertionFailure( "MappedAttribute is null" );
} }
this.mappedAttribute = mappedAttribute; this.mappedAttribute = mappedAttribute;
this.isMapKey = false;//todo this.isMapKey = false;//todo
} }
@Override @Override
protected AnnotationInstance getAnnotationInstance() { protected AnnotationInstance getTypeDeterminingAnnotationInstance() {
return JandexHelper.getSingleAnnotation( return JandexHelper.getSingleAnnotation(
mappedAttribute.annotations(), mappedAttribute.annotations(),
JPADotNames.ENUMERATED JPADotNames.ENUMERATED
); );
} }
@Override @Override
public String resolveHibernateTypeName(AnnotationInstance enumeratedAnnotation) { public String resolveHibernateTypeName(AnnotationInstance enumeratedAnnotation) {
boolean isEnum = mappedAttribute.getAttributeType().isEnum(); boolean isEnum = mappedAttribute.getAttributeType().isEnum();
if ( !isEnum ) { if ( !isEnum ) {
if ( enumeratedAnnotation != null ) { if ( enumeratedAnnotation != null ) {
throw new AnnotationException( "Attribute " + mappedAttribute.getName() + " is not a Enumerated type, but has a @Enumerated annotation." ); throw new AnnotationException( "Attribute " + mappedAttribute.getName() + " is not a Enumerated type, but has a @Enumerated annotation." );
} }
else { else {
return null; return null;
} }
} }
return EnumType.class.getName(); 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
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.AssertionFailure;
import org.hibernate.metamodel.source.annotations.JPADotNames; import org.hibernate.metamodel.source.annotations.JPADotNames;
import org.hibernate.metamodel.source.annotations.JandexHelper; 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.metamodel.source.annotations.attribute.MappedAttribute;
import org.hibernate.type.CharacterArrayClobType; import org.hibernate.type.CharacterArrayClobType;
import org.hibernate.type.PrimitiveCharacterArrayClobType; import org.hibernate.type.PrimitiveCharacterArrayClobType;
@ -47,67 +46,66 @@ import org.hibernate.type.WrappedMaterializedBlobType;
* @author Strong Liu * @author Strong Liu
*/ */
public class LobTypeResolver extends AbstractAttributeTypeResolver { 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) { @Override
if ( mappedAttribute == null ) { protected AnnotationInstance getTypeDeterminingAnnotationInstance() {
throw new AssertionFailure( "MappedAttribute is null" ); return JandexHelper.getSingleAnnotation( mappedAttribute.annotations(), JPADotNames.LOB );
} }
this.mappedAttribute = mappedAttribute;
}
@Override @Override
protected AnnotationInstance getAnnotationInstance() { public String resolveHibernateTypeName(AnnotationInstance annotationInstance) {
return JandexHelper.getSingleAnnotation( mappedAttribute.annotations(), JPADotNames.LOB ); 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 @Override
public String resolveHibernateTypeName(AnnotationInstance annotationInstance) { protected Map<String, String> resolveHibernateTypeParameters(AnnotationInstance annotationInstance) {
if ( annotationInstance == null ) { if ( getExplicitHibernateTypeName().equals( SerializableToBlobType.class.getName() ) ) {
return null; HashMap<String, String> typeParameters = new HashMap<String, String>();
} typeParameters.put(
String type = null; SerializableToBlobType.CLASS_NAME,
if ( Clob.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) { mappedAttribute.getAttributeType().getName()
type = StandardBasicTypes.CLOB.getName(); );
} return typeParameters;
else if ( Blob.class.isAssignableFrom( mappedAttribute.getAttributeType() ) ) { }
type = StandardBasicTypes.BLOB.getName(); return null;
} }
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;
}
} }

View File

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