HHH-5507 - Add @MapKeyType annotation

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@20271 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2010-08-26 21:00:47 +00:00
parent 07092e1caf
commit 232a77e5dd
4 changed files with 56 additions and 6 deletions

View File

@ -0,0 +1,41 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, 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.annotations;
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Allows defining the type of the key of a persistent map.
*
* @author Steve Ebersole
*/
@java.lang.annotation.Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface MapKeyType {
Type value();
}

View File

@ -112,6 +112,7 @@ import org.hibernate.annotations.Index;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;
import org.hibernate.annotations.ManyToAny;
import org.hibernate.annotations.MapKeyType;
import org.hibernate.annotations.NaturalId;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
@ -1682,11 +1683,12 @@ public final class AnnotationBinder {
!indexColumn.isImplicit(),
property.isAnnotationPresent( CollectionOfElements.class )
|| property.isAnnotationPresent( org.hibernate.annotations.MapKey.class )
|| property.isAnnotationPresent( MapKeyType.class )
// || property.isAnnotationPresent( ManyToAny.class )
);
collectionBinder.setIndexColumn( indexColumn );
MapKey mapKeyAnn = property.getAnnotation( MapKey.class );
collectionBinder.setMapKey( mapKeyAnn );
collectionBinder.setMapKey( property.getAnnotation( MapKey.class ) );
collectionBinder.setPropertyName( inferredData.getPropertyName() );
BatchSize batchAnn = property.getAnnotation( BatchSize.class );
collectionBinder.setBatchSize( batchAnn );

View File

@ -37,6 +37,7 @@ import org.hibernate.FetchMode;
import org.hibernate.MappingException;
import org.hibernate.annotations.MapKey;
import org.hibernate.annotations.MapKeyManyToMany;
import org.hibernate.annotations.MapKeyType;
import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.cfg.AccessType;
@ -288,7 +289,13 @@ public class MapBinder extends CollectionBinder {
elementBinder.setExplicitType( mapKeyAnn.type() );
}
else {
elementBinder.setType( property, elementClass );
MapKeyType mapKeyTypeAnnotation = property.getAnnotation( MapKeyType.class );
if ( mapKeyTypeAnnotation != null && ! BinderHelper.isDefault( mapKeyTypeAnnotation.value().type() ) ) {
elementBinder.setExplicitType( mapKeyTypeAnnotation.value() );
}
else {
elementBinder.setType( property, elementClass );
}
}
mapValue.setIndex( elementBinder.make() );
}

View File

@ -9,7 +9,7 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.MapKey;
import org.hibernate.annotations.MapKeyType;
import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;
import org.hibernate.annotations.Type;
@ -22,8 +22,8 @@ public class Matrix {
@Id
@GeneratedValue
private Integer id;
@MapKey(type = @Type(type="integer") )
@MapKeyType( @Type(type="integer") )
@ElementCollection
@Sort(type = SortType.NATURAL)
@Type(type = "float")