HHH-6896 - Metamodel.managedType(...) implemented incorrectly

This commit is contained in:
Steve Ebersole 2012-06-05 13:51:16 -05:00
parent 886cf9157d
commit 9b1521c29a
2 changed files with 32 additions and 27 deletions

View File

@ -32,6 +32,7 @@ import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.Metamodel;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.mapping.MappedSuperclass;
import org.hibernate.mapping.PersistentClass;
@ -170,60 +171,55 @@ public class MetamodelImpl implements Metamodel, Serializable {
this.embeddables = embeddables;
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings({ "unchecked" })
public <X> EntityType<X> entity(Class<X> cls) {
final EntityType<?> entityType = entities.get( cls );
if ( entityType == null ) throw new IllegalArgumentException( "Not an entity: " + cls );
//unsafe casting is our map inserts guarantee them
if ( entityType == null ) {
throw new IllegalArgumentException( "Not an entity: " + cls );
}
return (EntityType<X>) entityType;
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings({ "unchecked" })
public <X> ManagedType<X> managedType(Class<X> cls) {
ManagedType<?> type = entities.get( cls );
if ( type == null ) throw new IllegalArgumentException( "Not an managed type: " + cls );
//unsafe casting is our map inserts guarantee them
if ( type == null ) {
type = embeddables.get( cls );
}
if ( type == null ) {
throw new IllegalArgumentException( "Not an managed type: " + cls );
}
return (ManagedType<X>) type;
}
/**
* {@inheritDoc}
*/
@Override
@SuppressWarnings({ "unchecked" })
public <X> EmbeddableType<X> embeddable(Class<X> cls) {
final EmbeddableType<?> embeddableType = embeddables.get( cls );
if ( embeddableType == null ) throw new IllegalArgumentException( "Not an entity: " + cls );
//unsafe casting is our map inserts guarantee them
if ( embeddableType == null ) {
throw new IllegalArgumentException( "Not an embeddable: " + cls );
}
return (EmbeddableType<X>) embeddableType;
}
/**
* {@inheritDoc}
*/
@Override
public Set<ManagedType<?>> getManagedTypes() {
final Set<ManagedType<?>> managedTypes = new HashSet<ManagedType<?>>( entities.size() + embeddables.size() );
final int setSize = CollectionHelper.determineProperSizing( entities.size() + embeddables.size() );
final Set<ManagedType<?>> managedTypes = new HashSet<ManagedType<?>>( setSize );
managedTypes.addAll( entities.values() );
managedTypes.addAll( embeddables.values() );
return managedTypes;
}
/**
* {@inheritDoc}
*/
@Override
public Set<EntityType<?>> getEntities() {
return new HashSet<EntityType<?>>(entities.values());
return new HashSet<EntityType<?>>( entities.values() );
}
/**
* {@inheritDoc}
*/
@Override
public Set<EmbeddableType<?>> getEmbeddables() {
return new HashSet<EmbeddableType<?>>(embeddables.values());
return new HashSet<EmbeddableType<?>>( embeddables.values() );
}
}

View File

@ -25,6 +25,7 @@ package org.hibernate.ejb.metamodel;
import javax.persistence.EntityManager;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.SingularAttribute;
import org.junit.Test;
@ -47,6 +48,14 @@ public class EmbeddedTypeTest extends BaseEntityManagerFunctionalTestCase {
};
}
@Test
@TestForIssue( jiraKey = "HHH-6896" )
public void ensureComponentsReturnedAsManagedType() {
ManagedType<ShelfLife> managedType = entityManagerFactory().getMetamodel().managedType( ShelfLife.class );
// the issue was in regards to throwing an exception, but also check for nullness
assertNotNull( managedType );
}
@Test
@TestForIssue( jiraKey = "HHH-4702" )
public void testSingularAttributeAccessByName() {