Skip to content

Commit

Permalink
HHH-4417 - Add annotation support for UserCollectionType
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Mar 5, 2012
1 parent 497f700 commit 36c135a
Show file tree
Hide file tree
Showing 29 changed files with 604 additions and 207 deletions.
@@ -0,0 +1,60 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) ${year}, 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;

/**
* Names a custom collection type for a persistent collection.
*
* @see org.hibernate.type.CollectionType
* @see org.hibernate.usertype.UserCollectionType
*
* @author Steve Ebersole
*/
@java.lang.annotation.Target({FIELD, METHOD})
@Retention(RUNTIME)
public @interface CollectionType {
/**
* Names the type (either {@link org.hibernate.type.CollectionType} or
* {@link org.hibernate.usertype.UserCollectionType} implementation class. Could also name a
* custom type defined via a {@link TypeDef @TypeDef}
*
* @return The implementation class to use.
*/
String type();

/**
* Specifies configuration information for the type. Note that if the named type is a
* {@link org.hibernate.usertype.UserCollectionType}, it must also implement
* {@link org.hibernate.usertype.ParameterizedType} in order to receive these values.
*
* @return The configuration parameters.
*/
Parameter[] parameters() default {};
}
Expand Up @@ -1697,9 +1697,8 @@ else if ( property.isAnnotationPresent( OneToMany.class )
propertyHolder.getEntityName(),
property,
!indexColumn.isImplicit(),
property.isAnnotationPresent( MapKeyType.class )

// || property.isAnnotationPresent( ManyToAny.class )
property.isAnnotationPresent( MapKeyType.class ),
mappings
);
collectionBinder.setIndexColumn( indexColumn );
collectionBinder.setMapKey( property.getAnnotation( MapKey.class ) );
Expand Down
Expand Up @@ -23,14 +23,6 @@
*/
package org.hibernate.cfg.annotations;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.ElementCollection;
Expand All @@ -43,6 +35,14 @@
import javax.persistence.MapKey;
import javax.persistence.MapKeyColumn;
import javax.persistence.OneToMany;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;

import org.jboss.logging.Logger;

Expand All @@ -52,6 +52,7 @@
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CollectionId;
import org.hibernate.annotations.CollectionType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterJoinTable;
Expand All @@ -73,7 +74,6 @@
import org.hibernate.annotations.SQLUpdate;
import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.Where;
import org.hibernate.annotations.WhereJoinTable;
import org.hibernate.annotations.common.AssertionFailure;
Expand Down Expand Up @@ -252,7 +252,8 @@ public static CollectionBinder getCollectionBinder(
String entityName,
XProperty property,
boolean isIndexed,
boolean isHibernateExtensionMapping) {
boolean isHibernateExtensionMapping,
Mappings mappings) {
CollectionBinder result;
if ( property.isArray() ) {
if ( property.getElementClass().isPrimitive() ) {
Expand Down Expand Up @@ -332,11 +333,20 @@ else if ( property.isAnnotationPresent( CollectionId.class ) ) {
}
result.setIsHibernateExtensionMapping( isHibernateExtensionMapping );

final Type typeAnnotation = property.getAnnotation( Type.class );
final CollectionType typeAnnotation = property.getAnnotation( CollectionType.class );
if ( typeAnnotation != null ) {
result.explicitType = typeAnnotation.type();
for ( Parameter param : typeAnnotation.parameters() ) {
result.explicitTypeParameters.setProperty( param.name(), param.value() );
final String typeName = typeAnnotation.type();
// see if it names a type-def
final TypeDef typeDef = mappings.getTypeDef( typeName );
if ( typeDef != null ) {
result.explicitType = typeDef.getTypeClass();
result.explicitTypeParameters.putAll( typeDef.getParameters() );
}
else {
result.explicitType = typeName;
for ( Parameter param : typeAnnotation.parameters() ) {
result.explicitTypeParameters.setProperty( param.name(), param.value() );
}
}
}

Expand All @@ -359,6 +369,7 @@ public void setTableBinder(TableBinder tableBinder) {
}

public void setCollectionType(XClass collectionType) {
// NOTE: really really badly named. This is actually NOT the collection-type, but rather the collection-element-type!
this.collectionType = collectionType;
}

Expand Down
@@ -0,0 +1,75 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) ${year}, 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.test.collection.custom.basic;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
* @author Gavin King
* @author Steve Ebersole
*/
@Entity
public class Email {
private Long id;
private String address;

Email() {
}

public Email(String address) {
this.address = address;
}

@Id
@GeneratedValue( strategy = GenerationType.AUTO )
public Long getId() {
return id;
}

private void setId(Long id) {
this.id = id;
}

public String getAddress() {
return address;
}

public void setAddress(String type) {
this.address = type;
}

public boolean equals(Object that) {
if ( !(that instanceof Email) ) return false;
Email p = (Email) that;
return this.address.equals(p.address);
}

public int hashCode() {
return address.hashCode();
}

}
@@ -0,0 +1,29 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) ${year}, 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.test.collection.custom.basic;

import java.util.List;

public interface IMyList<X> extends List<X> {
}
@@ -0,0 +1,35 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) ${year}, 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.test.collection.custom.basic;

import java.util.ArrayList;

/**
* A custom collection class. We extend a java.util.Collection class, but that is not required.
* It could be totally non-java-collection type, but then we would need to implement all the PersistentCollection methods.
*
* @author max
*/
public class MyList<X> extends ArrayList<X> implements IMyList<X> {
}
@@ -1,7 +1,7 @@
package org.hibernate.test.usercollection.basic;
package org.hibernate.test.collection.custom.basic;
import java.util.Iterator;
import java.util.Map;
import org.hibernate.EntityMode;

import org.hibernate.HibernateException;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.SessionImplementor;
Expand Down
@@ -1,4 +1,4 @@
package org.hibernate.test.usercollection.basic;
package org.hibernate.test.collection.custom.basic;
import org.hibernate.collection.internal.PersistentList;
import org.hibernate.engine.spi.SessionImplementor;

Expand Down

0 comments on commit 36c135a

Please sign in to comment.