HHH-7482 auto import entities with annotation mapping

This commit is contained in:
Strong Liu 2012-07-30 11:46:44 +08:00
parent b2ef3b67b2
commit e4bf3e4583
6 changed files with 38 additions and 17 deletions

View File

@ -393,8 +393,8 @@ public class EntityBinder {
}
LOG.debugf( "Import with entity name %s", name );
try {
mappings.addImport( persistentClass.getEntityName(), name );
String entityName = persistentClass.getEntityName();
final String entityName = persistentClass.getEntityName();
mappings.addImport( entityName, name );
if ( !entityName.equals( name ) ) {
mappings.addImport( entityName, entityName );
}

View File

@ -923,10 +923,7 @@ public final class SessionFactoryImpl
namedSqlQueries.put( namedNativeQueryDefinition.getName(), namedNativeQueryDefinition );
}
sqlResultSetMappings = Collections.unmodifiableMap( new HashMap<String, ResultSetMappingDefinition>( metadata.getResultSetMappingDefinitions() ) );
imports = new HashMap<String,String>();
for ( Map.Entry<String,String> importEntry : metadata.getImports() ) {
imports.put( importEntry.getKey(), importEntry.getValue() );
}
imports = new HashMap<String,String>(metadata.getImports());
// after *all* persisters and named queries are registered
Iterator iter = entityPersisters.values().iterator();

View File

@ -109,7 +109,7 @@ public interface Metadata {
public Map<String, ResultSetMappingDefinition> getResultSetMappingDefinitions();
public Iterable<Map.Entry<String, String>> getImports();
public Map<String,String> getImports();
public Iterable<FetchProfile> getFetchProfiles();

View File

@ -556,7 +556,7 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
}
collectionBindingMap.put( collectionRole, pluralAttributeBinding );
}
@Override
public void addImport(String importName, String entityName) {
if ( importName == null || entityName == null ) {
throw new IllegalArgumentException( "Import name or entity name is null" );
@ -569,8 +569,8 @@ public class MetadataImpl implements MetadataImplementor, Serializable {
}
@Override
public Iterable<Map.Entry<String, String>> getImports() {
return imports.entrySet();
public Map<String,String> getImports() {
return imports;
}
@Override

View File

@ -29,8 +29,11 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.hibernate.AnnotationException;
import org.hibernate.MappingException;
import org.hibernate.cfg.NotYetImplementedException;
import org.hibernate.internal.jaxb.Origin;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.internal.source.annotations.attribute.AssociationAttribute;
import org.hibernate.metamodel.internal.source.annotations.attribute.BasicAttribute;
import org.hibernate.metamodel.internal.source.annotations.attribute.PluralAssociationAttribute;
@ -44,6 +47,7 @@ import org.hibernate.metamodel.spi.source.EntitySource;
import org.hibernate.metamodel.spi.source.JpaCallbackSource;
import org.hibernate.metamodel.spi.source.LocalBindingContext;
import org.hibernate.metamodel.spi.source.MetaAttributeSource;
import org.hibernate.metamodel.spi.source.MetadataImplementor;
import org.hibernate.metamodel.spi.source.SecondaryTableSource;
import org.hibernate.metamodel.spi.source.SubclassEntitySource;
import org.hibernate.metamodel.spi.source.TableSpecificationSource;
@ -54,10 +58,28 @@ import org.hibernate.metamodel.spi.source.TableSpecificationSource;
public class EntitySourceImpl implements EntitySource {
private final EntityClass entityClass;
private final Set<SubclassEntitySource> subclassEntitySources;
private final String jpaEntityName;
public EntitySourceImpl(EntityClass entityClass) {
this.entityClass = entityClass;
this.subclassEntitySources = new HashSet<SubclassEntitySource>();
this.jpaEntityName = StringHelper.isNotEmpty( entityClass.getExplicitEntityName() ) ? entityClass.getExplicitEntityName() : StringHelper
.unqualify( entityClass.getName() );
addImports();
}
private void addImports() {
try {
final MetadataImplementor metadataImplementor = entityClass.getLocalBindingContext()
.getMetadataImplementor();
metadataImplementor.addImport( getJpaEntityName(), getEntityName() );
if ( !getEntityName().equals( getJpaEntityName() ) ) {
metadataImplementor.addImport( getEntityName(), getEntityName() );
}
}
catch ( MappingException e ) {
throw new AnnotationException( "Use of the same entity name twice: " + getJpaEntityName(), e );
}
}
public EntityClass getEntityClass() {
@ -76,7 +98,7 @@ public class EntitySourceImpl implements EntitySource {
@Override
public String getEntityName() {
return entityClass.getName();
return getClassName();
}
@Override
@ -86,7 +108,7 @@ public class EntitySourceImpl implements EntitySource {
@Override
public String getJpaEntityName() {
return entityClass.getExplicitEntityName();
return jpaEntityName;
}
@Override

View File

@ -30,6 +30,7 @@ import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.testing.FailureExpectedWithNewMetamodel;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
@ -57,8 +58,13 @@ public class InsertedDataTest extends BaseCoreFunctionalTestCase {
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
}
@Override
protected void prepareBasicRegistryBuilder(ServiceRegistryBuilder serviceRegistryBuilder) {
serviceRegistryBuilder.applySetting( Environment.CACHE_REGION_PREFIX, "" );
serviceRegistryBuilder.applySetting( Environment.GENERATE_STATISTICS, "true" );
}
@Test
@FailureExpectedWithNewMetamodel
public void testInsert() {
sessionFactory().getCache().evictEntityRegions();
sessionFactory().getStatistics().clear();
@ -98,7 +104,6 @@ public class InsertedDataTest extends BaseCoreFunctionalTestCase {
}
@Test
@FailureExpectedWithNewMetamodel
public void testInsertThenUpdate() {
sessionFactory().getCache().evictEntityRegions();
sessionFactory().getStatistics().clear();
@ -123,7 +128,6 @@ public class InsertedDataTest extends BaseCoreFunctionalTestCase {
}
@Test
@FailureExpectedWithNewMetamodel
public void testInsertThenUpdateThenRollback() {
sessionFactory().getCache().evictEntityRegions();
sessionFactory().getStatistics().clear();
@ -148,7 +152,6 @@ public class InsertedDataTest extends BaseCoreFunctionalTestCase {
}
@Test
@FailureExpectedWithNewMetamodel
public void testInsertWithRefresh() {
sessionFactory().getCache().evictEntityRegions();
sessionFactory().getStatistics().clear();
@ -199,7 +202,6 @@ public class InsertedDataTest extends BaseCoreFunctionalTestCase {
}
@Test
@FailureExpectedWithNewMetamodel
public void testInsertWithClear() {
sessionFactory().getCache().evictEntityRegions();
sessionFactory().getStatistics().clear();