HHH-6490 Support @javax.persistence.Lob

HHH-6492 Support @javax.persistence.Enumerated
refact HHH-6489 Support @javax.persistence.Temporal
This commit is contained in:
Strong Liu 2011-07-30 17:16:44 +08:00
parent 2b0e0281b0
commit cc2dab0f18
14 changed files with 739 additions and 115 deletions

View File

@ -43,14 +43,18 @@ public class Datatype {
this.hashCode = generateHashCode();
}
private int generateHashCode() {
int result = typeCode;
result = 31 * result + typeName.hashCode();
result = 31 * result + javaType.hashCode();
return result;
}
private int generateHashCode() {
int result = typeCode;
if ( typeName != null ) {
result = 31 * result + typeName.hashCode();
}
if ( javaType != null ) {
result = 31 * result + javaType.hashCode();
}
return result;
}
public int getTypeCode() {
public int getTypeCode() {
return typeCode;
}

View File

@ -0,0 +1,43 @@
package org.hibernate.metamodel.source.annotations.attribute;
import java.util.Collections;
import java.util.Map;
import org.jboss.jandex.AnnotationInstance;
import org.hibernate.internal.util.StringHelper;
/**
* @author Strong Liu
*/
public abstract class AbstractHibernateTypeResolver implements HibernateTypeResolver {
protected abstract AnnotationInstance getAnnotationInstance();
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() );
}
/**
* Optional type parameters. See {@link #getExplicitHibernateTypeName()}.
*/
@Override
final public Map<String, String> getExplicitHibernateTypeParameters() {
if ( StringHelper.isNotEmpty( getExplicitHibernateTypeName() ) ) {
return resolveHibernateTypeParameters( getAnnotationInstance() );
}
else {
return Collections.emptyMap();
}
}
}

View File

@ -99,6 +99,7 @@ public class BasicAttribute extends MappedAttribute {
private final String customWriteFragment;
private final String customReadFragment;
private final String checkCondition;
private HibernateTypeResolver resolver;
public static BasicAttribute createSimpleAttribute(String name,
Class<?> attributeType,
@ -348,6 +349,27 @@ public class BasicAttribute extends MappedAttribute {
}
return generator;
}
@Override
public HibernateTypeResolver getHibernateTypeResolver() {
if ( resolver == null ) {
resolver = getDefaultHibernateTypeResolver();
}
return resolver;
}
protected HibernateTypeResolver getDefaultHibernateTypeResolver() {
CompositeHibernateTypeResolver resolver = new CompositeHibernateTypeResolver(
new ExplicitHibernateTypeResolver(
this
)
);
resolver.addHibernateTypeResolver( new TemporalTypeResolver( this ) );
resolver.addHibernateTypeResolver( new LobTypeResolver( this ) );
resolver.addHibernateTypeResolver( new EnumeratedTypeResolver( this ) );
return resolver;
}
}

View File

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

View File

@ -0,0 +1,98 @@
package org.hibernate.metamodel.source.annotations.attribute;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import org.jboss.jandex.AnnotationInstance;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
import org.hibernate.metamodel.relational.Identifier;
import org.hibernate.metamodel.relational.Schema;
import org.hibernate.metamodel.source.annotations.JPADotNames;
import org.hibernate.metamodel.source.annotations.JandexHelper;
import org.hibernate.type.EnumType;
/**
* @author Strong Liu
*/
public class EnumeratedTypeResolver extends AbstractHibernateTypeResolver {
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
}
@Override
protected AnnotationInstance getAnnotationInstance() {
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;
}
}

View File

@ -0,0 +1,60 @@
package org.hibernate.metamodel.source.annotations.attribute;
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;
/**
* @author Strong Liu
*/
public class ExplicitHibernateTypeResolver extends AbstractHibernateTypeResolver {
private final MappedAttribute mappedAttribute;
public ExplicitHibernateTypeResolver(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

@ -0,0 +1,26 @@
package org.hibernate.metamodel.source.annotations.attribute;
import java.util.Map;
import org.hibernate.metamodel.source.binder.ExplicitHibernateTypeSource;
/**
* @author Strong Liu
*/
public class ExplicitHibernateTypeSourceImpl implements ExplicitHibernateTypeSource {
private final HibernateTypeResolver typeResolver;
public ExplicitHibernateTypeSourceImpl(HibernateTypeResolver typeResolver) {
this.typeResolver = typeResolver;
}
@Override
public String getName() {
return typeResolver.getExplicitHibernateTypeName();
}
@Override
public Map<String, String> getParameters() {
return typeResolver.getExplicitHibernateTypeParameters();
}
}

View File

@ -0,0 +1,11 @@
package org.hibernate.metamodel.source.annotations.attribute;
import java.util.Map;
/**
* @author Strong Liu
*/
public interface HibernateTypeResolver {
String getExplicitHibernateTypeName();
Map<String, String> getExplicitHibernateTypeParameters();
}

View File

@ -0,0 +1,87 @@
package org.hibernate.metamodel.source.annotations.attribute;
import java.io.Serializable;
import java.sql.Blob;
import java.sql.Clob;
import java.util.HashMap;
import java.util.Map;
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.type.CharacterArrayClobType;
import org.hibernate.type.PrimitiveCharacterArrayClobType;
import org.hibernate.type.SerializableToBlobType;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.WrappedMaterializedBlobType;
/**
* @author Strong Liu
*/
public class LobTypeResolver extends AbstractHibernateTypeResolver {
private final MappedAttribute mappedAttribute;
public LobTypeResolver(MappedAttribute mappedAttribute) {
if ( mappedAttribute == null ) {
throw new AssertionFailure( "MappedAttribute is null" );
}
this.mappedAttribute = mappedAttribute;
}
@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
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

@ -23,6 +23,9 @@
*/
package org.hibernate.metamodel.source.annotations.attribute;
import java.io.Serializable;
import java.sql.Blob;
import java.sql.Clob;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
@ -41,6 +44,11 @@ import org.hibernate.metamodel.source.annotations.AnnotationBindingContext;
import org.hibernate.metamodel.source.annotations.HibernateDotNames;
import org.hibernate.metamodel.source.annotations.JPADotNames;
import org.hibernate.metamodel.source.annotations.JandexHelper;
import org.hibernate.type.CharacterArrayClobType;
import org.hibernate.type.PrimitiveCharacterArrayClobType;
import org.hibernate.type.SerializableToBlobType;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.WrappedMaterializedBlobType;
/**
* Base class for the different types of mapped attributes
@ -69,16 +77,6 @@ public abstract class MappedAttribute implements Comparable<MappedAttribute> {
*/
private final String accessType;
/**
* An optional explicit hibernate type name specified via {@link org.hibernate.annotations.Type}.
*/
private final String explicitHibernateTypeName;
/**
* Optional type parameters. See {@link #explicitHibernateTypeName}.
*/
private final Map<String, String> explicitHibernateTypeParameters;
/**
* The binding context
*/
@ -90,8 +88,6 @@ public abstract class MappedAttribute implements Comparable<MappedAttribute> {
this.name = name;
this.attributeType = attributeType;
this.accessType = accessType;
this.explicitHibernateTypeParameters = new HashMap<String, String>();
this.explicitHibernateTypeName = determineExplicitHibernateTypeName();
}
public String getName() {
@ -106,14 +102,6 @@ public abstract class MappedAttribute implements Comparable<MappedAttribute> {
return accessType;
}
public String getExplicitHibernateTypeName() {
return explicitHibernateTypeName;
}
public Map<String, String> getExplicitHibernateTypeParameters() {
return explicitHibernateTypeParameters;
}
public AnnotationBindingContext getContext() {
return context;
}
@ -127,6 +115,8 @@ public abstract class MappedAttribute implements Comparable<MappedAttribute> {
return name.compareTo( mappedProperty.getName() );
}
public abstract HibernateTypeResolver getHibernateTypeResolver();
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
@ -136,85 +126,8 @@ public abstract class MappedAttribute implements Comparable<MappedAttribute> {
return sb.toString();
}
private String getTemporalType() {
final AnnotationInstance temporalAnnotation = JandexHelper.getSingleAnnotation(
annotations(),
JPADotNames.TEMPORAL
);
if ( isTemporalType( attributeType ) ) {
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 " + name + " is a Temporal type, but no @Temporal annotation found." );
}
TemporalType temporalType = JandexHelper.getEnumValue( temporalAnnotation, "value", TemporalType.class );
boolean isDate = Date.class.isAssignableFrom( attributeType );
String type = null;
switch ( temporalType ) {
case DATE:
type = isDate ? "date" : "calendar_date";
break;
case TIME:
type = "time";
if ( !isDate ) {
throw new NotYetImplementedException( "Calendar cannot persist TIME only" );
}
break;
case TIMESTAMP:
type = isDate ? "timestamp" : "calendar";
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: " + name
);
}
}
return null;
}
private boolean isTemporalType(Class type) {
return Date.class.isAssignableFrom( type ) || Calendar.class.isAssignableFrom( type );
//todo (stliu) java.sql.Date is not listed in spec
// || java.sql.Date.class.isAssignableFrom( type )
}
private Map<String, String> extractTypeParameters(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(
parameterAnnotationInstance.value( "name" ).asString(),
parameterAnnotationInstance.value( "value" ).asString()
);
}
}
return typeParameters;
}
private String determineExplicitHibernateTypeName() {
String typeName = null;
String temporalType = getTemporalType();
final AnnotationInstance typeAnnotation = JandexHelper.getSingleAnnotation(
annotations(),
HibernateDotNames.TYPE
);
if ( typeAnnotation != null ) {
typeName = typeAnnotation.value( "type" ).asString();
this.explicitHibernateTypeParameters.putAll( extractTypeParameters( typeAnnotation ) );
}
else if ( temporalType != null ) {
typeName = temporalType;
}
return typeName;
}
}

View File

@ -47,17 +47,7 @@ public class SingularAttributeSourceImpl implements SingularAttributeSource {
@Override
public ExplicitHibernateTypeSource getTypeInformation() {
return new ExplicitHibernateTypeSource() {
@Override
public String getName() {
return attribute.getExplicitHibernateTypeName();
}
@Override
public Map<String, String> getParameters() {
return attribute.getExplicitHibernateTypeParameters();
}
};
return new ExplicitHibernateTypeSourceImpl( attribute.getHibernateTypeResolver() );
}
@Override

View File

@ -0,0 +1,82 @@
package org.hibernate.metamodel.source.annotations.attribute;
import java.util.Calendar;
import java.util.Date;
import javax.persistence.TemporalType;
import org.jboss.jandex.AnnotationInstance;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
import org.hibernate.cfg.NotYetImplementedException;
import org.hibernate.metamodel.source.annotations.JPADotNames;
import org.hibernate.metamodel.source.annotations.JandexHelper;
/**
* @author Strong Liu
*/
public class TemporalTypeResolver extends AbstractHibernateTypeResolver {
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
}
@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 ? "date" : "calendar_date";
break;
case TIME:
type = "time";
if ( !isDate ) {
throw new NotYetImplementedException( "Calendar cannot persist TIME only" );
}
break;
case TIMESTAMP:
type = isDate ? "timestamp" : "calendar";
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 );
}
}

View File

@ -0,0 +1,83 @@
package org.hibernate.metamodel.source.annotations.entity;
import java.sql.Types;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.junit.Test;
import org.hibernate.metamodel.binding.AttributeBinding;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.binding.HibernateTypeDescriptor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
/**
* @author Strong Liu
*/
public class EnumeratedBindingTests extends BaseAnnotationBindingTestCase {
@Entity
class Item {
@Id
long id;
@Temporal(TemporalType.TIMESTAMP)
Date orderDate;
String name;
@Enumerated(EnumType.STRING)
OrderType orderType;
CustomerType customerType;
}
enum CustomerType {
PROGRAMMER, BOSS;
}
enum OrderType {
B2C, C2C, MAIL, DIRECT;
}
@Test
@Resources(annotatedClasses = Item.class)
public void testEnumeratedTypeAttribute() {
EntityBinding binding = getEntityBinding( Item.class );
AttributeBinding attributeBinding = binding.locateAttributeBinding( "customerType" );
HibernateTypeDescriptor descriptor = attributeBinding.getHibernateTypeDescriptor();
assertEquals( org.hibernate.type.EnumType.class.getName(), descriptor.getExplicitTypeName() );
assertEquals( CustomerType.class.getName(), descriptor.getJavaTypeName() );
assertNotNull( descriptor.getResolvedTypeMapping() );
assertFalse( descriptor.getTypeParameters().isEmpty() );
assertEquals(
CustomerType.class.getName(),
descriptor.getTypeParameters().get( org.hibernate.type.EnumType.ENUM )
);
assertEquals(
String.valueOf( Types.INTEGER ),
descriptor.getTypeParameters().get( org.hibernate.type.EnumType.TYPE )
);
attributeBinding = binding.locateAttributeBinding( "orderType" );
descriptor = attributeBinding.getHibernateTypeDescriptor();
assertEquals( org.hibernate.type.EnumType.class.getName(), descriptor.getExplicitTypeName() );
assertEquals( OrderType.class.getName(), descriptor.getJavaTypeName() );
assertNotNull( descriptor.getResolvedTypeMapping() );
assertFalse( descriptor.getTypeParameters().isEmpty() );
assertEquals(
OrderType.class.getName(),
descriptor.getTypeParameters().get( org.hibernate.type.EnumType.ENUM )
);
assertEquals(
String.valueOf( Types.VARCHAR ),
descriptor.getTypeParameters().get( org.hibernate.type.EnumType.TYPE )
);
}
}

View File

@ -0,0 +1,146 @@
package org.hibernate.metamodel.source.annotations.entity;
import java.io.Serializable;
import java.sql.Blob;
import java.sql.Clob;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import org.junit.Test;
import org.hibernate.metamodel.binding.AttributeBinding;
import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.binding.HibernateTypeDescriptor;
import org.hibernate.type.BlobType;
import org.hibernate.type.CharacterArrayClobType;
import org.hibernate.type.ClobType;
import org.hibernate.type.MaterializedBlobType;
import org.hibernate.type.MaterializedClobType;
import org.hibernate.type.PrimitiveCharacterArrayClobType;
import org.hibernate.type.SerializableToBlobType;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.WrappedMaterializedBlobType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* @author Strong Liu
*/
public class LobBindingTests extends BaseAnnotationBindingTestCase {
@Entity
class Item {
@Id
long id;
@Lob
Clob clob;
@Lob
Blob blob;
@Lob
String str;
@Lob
Character[] characters;
@Lob
char[] chars;
@Lob
Byte[] bytes;
@Lob
byte[] bytes2;
@Lob
Thing serializable;
String noLob;
}
class Thing implements Serializable{
int size;
}
@Test
@Resources(annotatedClasses = Item.class)
public void testLobTypeAttribute() {
EntityBinding binding = getEntityBinding( Item.class );
AttributeBinding attributeBinding = binding.locateAttributeBinding( "clob" );
HibernateTypeDescriptor descriptor = attributeBinding.getHibernateTypeDescriptor();
assertEquals( "clob", descriptor.getExplicitTypeName() );
assertEquals( Clob.class.getName(), descriptor.getJavaTypeName() );
assertNotNull( descriptor.getResolvedTypeMapping() );
assertEquals( ClobType.class, descriptor.getResolvedTypeMapping().getClass() );
assertNotNull( descriptor.getTypeParameters() );
assertTrue( descriptor.getTypeParameters().isEmpty() );
attributeBinding = binding.locateAttributeBinding( "blob" );
descriptor = attributeBinding.getHibernateTypeDescriptor();
assertEquals( "blob", descriptor.getExplicitTypeName() );
assertEquals( Blob.class.getName(), descriptor.getJavaTypeName() );
assertNotNull( descriptor.getResolvedTypeMapping() );
assertEquals( BlobType.class, descriptor.getResolvedTypeMapping().getClass() );
assertNotNull( descriptor.getTypeParameters() );
assertTrue( descriptor.getTypeParameters().isEmpty() );
attributeBinding = binding.locateAttributeBinding( "str" );
descriptor = attributeBinding.getHibernateTypeDescriptor();
assertEquals( "materialized_clob", descriptor.getExplicitTypeName() );
assertEquals( String.class.getName(), descriptor.getJavaTypeName() );
assertNotNull( descriptor.getResolvedTypeMapping() );
assertEquals( MaterializedClobType.class, descriptor.getResolvedTypeMapping().getClass() );
assertNotNull( descriptor.getTypeParameters() );
assertTrue( descriptor.getTypeParameters().isEmpty() );
attributeBinding = binding.locateAttributeBinding( "characters" );
descriptor = attributeBinding.getHibernateTypeDescriptor();
assertEquals( CharacterArrayClobType.class.getName(), descriptor.getExplicitTypeName() );
assertEquals( Character[].class.getName(), descriptor.getJavaTypeName() );
assertNotNull( descriptor.getResolvedTypeMapping() );
assertEquals( CharacterArrayClobType.class, descriptor.getResolvedTypeMapping().getClass() );
assertNotNull( descriptor.getTypeParameters() );
assertTrue( descriptor.getTypeParameters().isEmpty() );
attributeBinding = binding.locateAttributeBinding( "chars" );
descriptor = attributeBinding.getHibernateTypeDescriptor();
assertEquals( PrimitiveCharacterArrayClobType.class.getName(), descriptor.getExplicitTypeName() );
assertEquals( char[].class.getName(), descriptor.getJavaTypeName() );
assertNotNull( descriptor.getResolvedTypeMapping() );
assertEquals( PrimitiveCharacterArrayClobType.class, descriptor.getResolvedTypeMapping().getClass() );
assertNotNull( descriptor.getTypeParameters() );
assertTrue( descriptor.getTypeParameters().isEmpty() );
attributeBinding = binding.locateAttributeBinding( "bytes" );
descriptor = attributeBinding.getHibernateTypeDescriptor();
assertEquals( WrappedMaterializedBlobType.class.getName(), descriptor.getExplicitTypeName() );
assertEquals( Byte[].class.getName(), descriptor.getJavaTypeName() );
assertNotNull( descriptor.getResolvedTypeMapping() );
assertEquals( WrappedMaterializedBlobType.class, descriptor.getResolvedTypeMapping().getClass() );
assertNotNull( descriptor.getTypeParameters() );
assertTrue( descriptor.getTypeParameters().isEmpty() );
attributeBinding = binding.locateAttributeBinding( "bytes2" );
descriptor = attributeBinding.getHibernateTypeDescriptor();
assertEquals( StandardBasicTypes.MATERIALIZED_BLOB.getName(), descriptor.getExplicitTypeName() );
assertEquals( byte[].class.getName(), descriptor.getJavaTypeName() );
assertNotNull( descriptor.getResolvedTypeMapping() );
assertEquals( MaterializedBlobType.class, descriptor.getResolvedTypeMapping().getClass() );
assertNotNull( descriptor.getTypeParameters() );
assertTrue( descriptor.getTypeParameters().isEmpty() );
attributeBinding = binding.locateAttributeBinding( "serializable" );
descriptor = attributeBinding.getHibernateTypeDescriptor();
assertEquals( SerializableToBlobType.class.getName(), descriptor.getExplicitTypeName() );
assertEquals( Thing.class.getName(), descriptor.getJavaTypeName() );
assertNotNull( descriptor.getResolvedTypeMapping() );
assertEquals( SerializableToBlobType.class, descriptor.getResolvedTypeMapping().getClass() );
assertNotNull( descriptor.getTypeParameters() );
assertEquals( 1,descriptor.getTypeParameters().size() );
assertTrue( descriptor.getTypeParameters().get( SerializableToBlobType.CLASS_NAME ).equals( Thing.class.getName() ) );
attributeBinding = binding.locateAttributeBinding( "noLob" );
descriptor = attributeBinding.getHibernateTypeDescriptor();
assertNull( descriptor.getExplicitTypeName() );
assertTrue( descriptor.getTypeParameters().isEmpty() );
}
}