HHH-4542 read the collection type from the member rather than the expected Hibernate collection type
git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17901 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
ab26c15c32
commit
66610a567c
|
@ -24,6 +24,8 @@
|
|||
package org.hibernate.ejb.metamodel;
|
||||
|
||||
import java.lang.reflect.Member;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Iterator;
|
||||
import javax.persistence.metamodel.Attribute;
|
||||
import javax.persistence.metamodel.Type;
|
||||
|
@ -83,18 +85,21 @@ public class AttributeFactory {
|
|||
private <X, Y, V, K> AttributeImplementor<X, Y> buildPluralAttribute(AbstractManagedType<X> ownerType, Property property, AttributeContext attrContext, boolean getMember) {
|
||||
AttributeImplementor<X, Y> attribute;
|
||||
final Type<V> attrType = getType( ownerType, attrContext.getElementTypeStatus(), attrContext.getElementValue(), getMember );
|
||||
final Class<Y> collectionClass = (Class<Y>) attrContext.getCollectionClass();
|
||||
final Member member = getMember ? determineStandardJavaMember( ownerType, property ) : null;
|
||||
final Class<Y> collectionClass = (Class<Y>) ( member instanceof Field
|
||||
? ( ( Field ) member ).getType()
|
||||
: ( ( Method ) member ).getReturnType() );
|
||||
if ( java.util.Map.class.isAssignableFrom( collectionClass ) ) {
|
||||
final Type<K> keyType = getType( ownerType, attrContext.getKeyTypeStatus(), attrContext.getKeyValue(), getMember );
|
||||
attribute = PluralAttributeImpl.create( ownerType, attrType, collectionClass, keyType )
|
||||
.member( getMember ? determineStandardJavaMember( ownerType, property ) : null )
|
||||
.member( member )
|
||||
.property( property )
|
||||
.persistentAttributeType( attrContext.getElementAttributeType() )
|
||||
.build();
|
||||
}
|
||||
else {
|
||||
attribute = PluralAttributeImpl.create( ownerType, attrType, collectionClass, null )
|
||||
.member( getMember ? determineStandardJavaMember( ownerType, property ) : null )
|
||||
.member( member )
|
||||
.property( property )
|
||||
.persistentAttributeType( attrContext.getElementAttributeType() )
|
||||
.build();
|
||||
|
@ -318,10 +323,6 @@ public class AttributeFactory {
|
|||
return collectionClass != null;
|
||||
}
|
||||
|
||||
public Class<?> getCollectionClass() {
|
||||
return collectionClass;
|
||||
}
|
||||
|
||||
public Attribute.PersistentAttributeType getElementAttributeType() {
|
||||
return attrType;
|
||||
}
|
||||
|
@ -357,7 +358,6 @@ public class AttributeFactory {
|
|||
final Attribute.PersistentAttributeType elementPAT;
|
||||
final Class<?> collectionClass = collValue.getCollectionType().getReturnedClass();
|
||||
|
||||
|
||||
final Value keyValue;
|
||||
final org.hibernate.type.Type keyType;
|
||||
final AttributeContext.TypeStatus keyTypeStatus;
|
||||
|
|
|
@ -356,10 +356,9 @@ class MetadataContext {
|
|||
// most likely a mismatch in the type we are injecting and the defined field; this represents a
|
||||
// mismatch in how the annotation processor interpretted the attribute and how our metamodel
|
||||
// and/or annotation binder did.
|
||||
//
|
||||
// This does seem to be an issue currently for ListAttribute and CollectionAttribute for @OneToMany List
|
||||
// w/o the @Index definition (which is a bag in Hibernate-terms. So for the time being we simply
|
||||
// log an error
|
||||
|
||||
// This is particularly the case as arrays are nto handled propery by the StaticMetamodel generator
|
||||
|
||||
// throw new AssertionFailure(
|
||||
// "Illegal argument on static metamodel field injection : " + metamodelClass.getName() + '#' + name
|
||||
// + "; expected type : " + attribute.getClass().getName()
|
||||
|
|
|
@ -91,6 +91,33 @@ public abstract class PluralAttributeImpl<X, C, E>
|
|||
|
||||
@SuppressWarnings( "unchecked" )
|
||||
public <K> PluralAttributeImpl<X,C,E> build() {
|
||||
//apply strict spec rules first
|
||||
if ( Map.class.equals( collectionClass ) ) {
|
||||
final Builder<X,Map<K,E>,E,K> builder = (Builder<X,Map<K,E>,E,K>) this;
|
||||
return ( PluralAttributeImpl<X, C, E> ) new MapAttributeImpl<X,K,E>(
|
||||
builder
|
||||
);
|
||||
}
|
||||
else if ( Set.class.equals( collectionClass ) ) {
|
||||
final Builder<X,Set<E>, E,?> builder = (Builder<X, Set<E>, E,?>) this;
|
||||
return ( PluralAttributeImpl<X, C, E> ) new SetAttributeImpl<X,E>(
|
||||
builder
|
||||
);
|
||||
}
|
||||
else if ( List.class.equals( collectionClass ) ) {
|
||||
final Builder<X, List<E>, E,?> builder = (Builder<X, List<E>, E,?>) this;
|
||||
return ( PluralAttributeImpl<X, C, E> ) new ListAttributeImpl<X,E>(
|
||||
builder
|
||||
);
|
||||
}
|
||||
else if ( Collection.class.equals( collectionClass ) ) {
|
||||
final Builder<X, Collection<E>,E,?> builder = (Builder<X, Collection<E>, E,?>) this;
|
||||
return ( PluralAttributeImpl<X, C, E> ) new CollectionAttributeImpl<X, E>(
|
||||
builder
|
||||
);
|
||||
}
|
||||
|
||||
//apply loose rules
|
||||
if ( Map.class.isAssignableFrom( collectionClass ) ) {
|
||||
final Builder<X,Map<K,E>,E,K> builder = (Builder<X,Map<K,E>,E,K>) this;
|
||||
return ( PluralAttributeImpl<X, C, E> ) new MapAttributeImpl<X,K,E>(
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
package org.hibernate.ejb.test.metadata;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.GeneratedValue;
|
||||
|
||||
/**
|
||||
* @author Emmanuel Bernard
|
||||
*/
|
||||
@Entity
|
||||
public class Flower {
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
@Id @GeneratedValue
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package org.hibernate.ejb.test.metadata;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
|
||||
/**
|
||||
* @author Emmanuel Bernard
|
||||
*/
|
||||
@Entity
|
||||
public class Garden {
|
||||
private Long id;
|
||||
private String name;
|
||||
private List<Flower> flowers = new ArrayList<Flower>();
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@OneToMany
|
||||
public List<Flower> getFlowers() {
|
||||
return flowers;
|
||||
}
|
||||
|
||||
public void setFlowers(List<Flower> flowers) {
|
||||
this.flowers = flowers;
|
||||
}
|
||||
}
|
|
@ -136,6 +136,14 @@ public class MetadataTest extends TestCase {
|
|||
assertEquals( Type.PersistenceType.EMBEDDABLE, directType.getPersistenceType() );
|
||||
}
|
||||
|
||||
public void testCollection() throws Exception {
|
||||
final EntityType<Garden> entiytype = factory.getMetamodel().entity( Garden.class );
|
||||
final Set<PluralAttribute<? super Garden, ?, ?>> attributes = entiytype.getPluralAttributes();
|
||||
assertEquals( 1, attributes.size() );
|
||||
PluralAttribute<? super Garden, ?, ?> flowers = attributes.iterator().next();
|
||||
assertTrue( flowers instanceof ListAttribute );
|
||||
}
|
||||
|
||||
public void testElementCollection() throws Exception {
|
||||
final EntityType<House> entityType = factory.getMetamodel().entity( House.class );
|
||||
final SetAttribute<House,Room> rooms = entityType.getDeclaredSet( "rooms", Room.class );
|
||||
|
@ -273,7 +281,9 @@ public class MetadataTest extends TestCase {
|
|||
Dog.class,
|
||||
Cat.class,
|
||||
Cattish.class,
|
||||
Feline.class
|
||||
Feline.class,
|
||||
Garden.class,
|
||||
Flower.class
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -115,6 +115,9 @@ public class StaticMetadataTest extends TestCase {
|
|||
assertTrue( Person_.firstName.isId() );
|
||||
assertTrue( Person_.lastName.isId() );
|
||||
assertTrue( Person_.lastName.isId() );
|
||||
|
||||
//Garden List as bag
|
||||
assertNotNull( Garden_.flowers );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -127,7 +130,9 @@ public class StaticMetadataTest extends TestCase {
|
|||
Dog.class,
|
||||
Cat.class,
|
||||
Cattish.class,
|
||||
Feline.class
|
||||
Feline.class,
|
||||
Garden.class,
|
||||
Flower.class
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue