Skip to content

Commit

Permalink
HHH-8126 migrate HEM to metamodel branch
Browse files Browse the repository at this point in the history
  • Loading branch information
stliu committed Apr 2, 2013
1 parent 5700142 commit 65f204f
Show file tree
Hide file tree
Showing 102 changed files with 969 additions and 522 deletions.
Expand Up @@ -27,6 +27,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -181,4 +182,17 @@ public static <X,Y> Map<X, Y> makeCopy(Map<X, Y> map) {
copy.putAll( map );
return copy;
}

public static void cleanUpNullValue(Map map) {
if ( isEmpty( map ) ) {
return;
}
Iterator itr = map.entrySet().iterator();
while ( itr.hasNext() ) {
final Map.Entry entry = (Map.Entry) itr.next();
if ( entry.getValue() == null ) {
itr.remove();
}
}
}
}
Expand Up @@ -40,7 +40,6 @@

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.Index;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.Indexer;
import org.jboss.logging.Logger;
Expand Down Expand Up @@ -75,7 +74,10 @@
*/
public class MetadataSources {
private static final Logger LOG = Logger.getLogger( MetadataSources.class );

/**
* temporary option
*/
public static final String USE_NEW_METADATA_MAPPINGS = "hibernate.test.new_metadata_mappings";
private final ServiceRegistry serviceRegistry;
private final JaxbMappingProcessor jaxbProcessor;
private final List<CacheRegionDefinition> externalCacheRegionDefinitions = new ArrayList<CacheRegionDefinition>();
Expand Down Expand Up @@ -444,12 +446,14 @@ public MetadataSources addJar(File jar) {
*/
public MetadataSources addDirectory(File dir) {
File[] files = dir.listFiles();
for ( File file : files ) {
if ( file.isDirectory() ) {
addDirectory( file );
}
else if ( file.getName().endsWith( ".hbm.xml" ) ) {
addFile( file );
if ( files != null && files.length > 0 ) {
for ( File file : files ) {
if ( file.isDirectory() ) {
addDirectory( file );
}
else if ( file.getName().endsWith( ".hbm.xml" ) ) {
addFile( file );
}
}
}
return this;
Expand Down Expand Up @@ -526,7 +530,7 @@ private void indexClass(Class clazz, Indexer indexer, Set<Class<?>> processedCla
for ( Class<?> fieldType : ReflectHelper.getFieldTypes( clazz ) ) {
if ( !fieldType.isPrimitive() && fieldType != Object.class ) {
try {
Index fieldIndex = JandexHelper.indexForClass(
IndexView fieldIndex = JandexHelper.indexForClass(
serviceRegistry.getService( ClassLoaderService.class ),
fieldType );
if ( !fieldIndex.getAnnotations(
Expand Down
Expand Up @@ -299,6 +299,7 @@ private void applyToAllEntityHierarchies(LocalBindingContextExecutor rootExecuto
entityHierarchyHelper.applyToAllEntityHierarchies( entityHierarchiesByRootEntityName.values(), rootExecutor, subExecutor );
}


private void bindEntityHierarchiesExcludingNonIdAttributeBindings() {
LocalBindingContextExecutor rootEntityCallback = new LocalBindingContextExecutor() {
@Override
Expand Down
Expand Up @@ -24,6 +24,8 @@
package org.hibernate.metamodel.internal;

import java.io.Serializable;
import java.sql.Blob;
import java.sql.Clob;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -39,6 +41,7 @@
import org.hibernate.SessionFactory;
import org.hibernate.annotations.common.util.StringHelper;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.boot.spi.CacheRegionDefinition;
import org.hibernate.cache.spi.access.AccessType;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.ObjectNameNormalizer;
Expand Down Expand Up @@ -66,8 +69,10 @@
import org.hibernate.metamodel.spi.MetadataSourceProcessor;
import org.hibernate.metamodel.spi.TypeContributions;
import org.hibernate.metamodel.spi.TypeContributor;
import org.hibernate.metamodel.spi.binding.AbstractPluralAttributeBinding;
import org.hibernate.metamodel.spi.binding.AttributeBinding;
import org.hibernate.metamodel.spi.binding.BackRefAttributeBinding;
import org.hibernate.metamodel.spi.binding.Caching;
import org.hibernate.metamodel.spi.binding.EntityBinding;
import org.hibernate.metamodel.spi.binding.FetchProfile;
import org.hibernate.metamodel.spi.binding.IdGenerator;
Expand Down Expand Up @@ -249,7 +254,7 @@ public void contributeType(CompositeUserType type, String[] keys) {
binder.addEntityHierarchies( processor.extractEntityHierarchies() );
binder.bindEntityHierarchies();

secondPass();
secondPass(metadataSources);
}


Expand All @@ -271,7 +276,7 @@ private void processTypeDefinitions(MetadataSourceProcessor[] metadataSourceProc
}
}

private void secondPass() {
private void secondPass(MetadataSources metadataSources) {
// This must be done outside of Table, rather than statically, to ensure
// deterministic alias names. See HHH-2448.
int uniqueInteger = 0;
Expand All @@ -280,6 +285,44 @@ private void secondPass() {
table.setTableNumber( uniqueInteger++ );
}
}

if ( metadataSources.getExternalCacheRegionDefinitions().isEmpty() ) {
return;
}
for ( CacheRegionDefinition cacheRegionDefinition : metadataSources.getExternalCacheRegionDefinitions() ) {
final String role = cacheRegionDefinition.getRole();
if ( cacheRegionDefinition.getRegionType() == CacheRegionDefinition.CacheRegionType.ENTITY ) {
EntityBinding entityBinding = entityBindingMap.get( role );
if ( entityBinding != null ) {
entityBinding.getHierarchyDetails()
.setCaching(
new Caching(
cacheRegionDefinition.getRegion(),
AccessType.fromExternalName( cacheRegionDefinition.getUsage() ),
cacheRegionDefinition.isCacheLazy()
)
);
}else{
//logging?
throw new MappingException( "Can't find entitybinding for role " + role +" to apply cache configuration" );
}

}
else if ( cacheRegionDefinition.getRegionType() == CacheRegionDefinition.CacheRegionType.COLLECTION ) {
PluralAttributeBinding pluralAttributeBinding = collectionBindingMap.get( role );
if(pluralAttributeBinding!=null){
AbstractPluralAttributeBinding.class.cast( pluralAttributeBinding ).setCaching( new Caching(
cacheRegionDefinition.getRegion(),
AccessType.fromExternalName( cacheRegionDefinition.getUsage() ),
cacheRegionDefinition.isCacheLazy()
) );
} else {
//logging?
throw new MappingException( "Can't find entitybinding for role " + role +" to apply cache configuration" );
}
}
}

}

@Override
Expand Down
Expand Up @@ -38,6 +38,7 @@
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.Index;
import org.jboss.jandex.IndexView;
import org.jboss.logging.Logger;

import org.hibernate.annotations.FetchMode;
Expand Down Expand Up @@ -494,7 +495,7 @@ private void determineJoinTableAnnotations(
// with the owning side.
if ( isJpaInverse ) {
// TODO: Pull some of this into JandexHelper.
Index index = JandexHelper.indexForClass(
IndexView index = JandexHelper.indexForClass(
getContext().getServiceRegistry().getService(
ClassLoaderService.class ), referencedAttributeType );
ClassInfo classInfo = index.getClassByName( DotName.createSimple(
Expand Down
Expand Up @@ -25,6 +25,7 @@

import java.util.Collection;
import java.util.HashMap;
import javax.persistence.LockModeType;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.NamedQueries;
Expand All @@ -35,17 +36,22 @@
import org.jboss.logging.Logger;

import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
import org.hibernate.CacheMode;
import org.hibernate.FlushMode;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.MappingException;
import org.hibernate.annotations.CacheModeType;
import org.hibernate.annotations.FlushModeType;
import org.hibernate.annotations.QueryHints;
import org.hibernate.cfg.NotYetImplementedException;
import org.hibernate.engine.query.spi.sql.NativeSQLQueryRootReturn;
import org.hibernate.engine.spi.NamedQueryDefinitionBuilder;
import org.hibernate.engine.spi.NamedSQLQueryDefinition;
import org.hibernate.engine.spi.NamedSQLQueryDefinitionBuilder;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.LockModeConverter;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.internal.source.annotations.AnnotationBindingContext;
import org.hibernate.metamodel.internal.source.annotations.util.HibernateDotNames;
Expand Down Expand Up @@ -135,13 +141,98 @@ public static void bind(AnnotationBindingContext bindingContext) {
* @param annotation the named query annotation
*/
private static void bindNamedQuery(MetadataImplementor metadata, AnnotationInstance annotation) {
String name = JandexHelper.getValue( annotation, "name", String.class );
final String name = JandexHelper.getValue( annotation, "name", String.class );
if ( StringHelper.isEmpty( name ) ) {
throw new AnnotationException( "A named query must have a name when used in class or package level" );
}
NamedQueryDefinitionBuilder builder = new NamedQueryDefinitionBuilder();
builder.setName( name );

final String query = JandexHelper.getValue( annotation, "query", String.class );
builder.setQuery( query );
if ( annotation.name().equals( JPADotNames.NAMED_QUERY ) ) {
bindJPANamedQuery( annotation, builder, name, query );
} else {
builder.setFlushMode(
getFlushMode(
JandexHelper.getEnumValue(
annotation,
"flushMode",
FlushModeType.class
)
)
)
.setCacheable( JandexHelper.getValue( annotation, "cacheable", Boolean.class ) )
.setCacheRegion( JandexHelper.getValue( annotation, "cacheRegion", String.class ) )
.setFetchSize( JandexHelper.getValue( annotation, "fetchSize", Integer.class ) )
.setTimeout( JandexHelper.getValue( annotation, "timeout", Integer.class ) )
.setComment( JandexHelper.getValue( annotation, "comment", String.class ) )
.setCacheMode(
getCacheMode(
JandexHelper.getValue(
annotation,
"cacheMode",
CacheModeType.class
)
)
)
.setReadOnly( JandexHelper.getValue( annotation, "readOnly", Boolean.class ) );
}

String query = JandexHelper.getValue( annotation, "query", String.class );

metadata.addNamedQuery(builder.createNamedQueryDefinition());
LOG.debugf( "Binding named query: %s => %s", name, query );
}

public static FlushMode getFlushMode(FlushModeType flushModeType) {
FlushMode flushMode;
switch ( flushModeType ) {
case ALWAYS:
flushMode = FlushMode.ALWAYS;
break;
case AUTO:
flushMode = FlushMode.AUTO;
break;
case COMMIT:
flushMode = FlushMode.COMMIT;
break;
case NEVER:
flushMode = FlushMode.MANUAL;
break;
case MANUAL:
flushMode = FlushMode.MANUAL;
break;
case PERSISTENCE_CONTEXT:
flushMode = null;
break;
default:
throw new AssertionFailure( "Unknown flushModeType: " + flushModeType );
}
return flushMode;
}
private static CacheMode getCacheMode(CacheModeType cacheModeType) {
switch ( cacheModeType ) {
case GET:
return CacheMode.GET;
case IGNORE:
return CacheMode.IGNORE;
case NORMAL:
return CacheMode.NORMAL;
case PUT:
return CacheMode.PUT;
case REFRESH:
return CacheMode.REFRESH;
default:
throw new AssertionFailure( "Unknown cacheModeType: " + cacheModeType );
}
}


private static void bindJPANamedQuery(
AnnotationInstance annotation,
NamedQueryDefinitionBuilder builder,
String name,
String query){
AnnotationInstance[] hints = JandexHelper.getValue( annotation, "hints", AnnotationInstance[].class );

String cacheRegion = getString( hints, QueryHints.CACHE_REGION );
Expand All @@ -159,6 +250,12 @@ private static void bindNamedQuery(MetadataImplementor metadata, AnnotationInsta
if ( lockTimeout != null && lockTimeout < 0 ) {
lockTimeout = null;
}
LockOptions lockOptions = new LockOptions( LockModeConverter.convertToLockMode( JandexHelper.getEnumValue( annotation, "lockMode",
LockModeType.class
) ) );
if ( lockTimeout != null ) {
lockOptions.setTimeOut( lockTimeout );
}
Integer fetchSize = getInteger( hints, QueryHints.FETCH_SIZE, name );
if ( fetchSize != null && fetchSize < 0 ) {
fetchSize = null;
Expand All @@ -168,23 +265,16 @@ private static void bindNamedQuery(MetadataImplementor metadata, AnnotationInsta
if ( StringHelper.isEmpty( comment ) ) {
comment = null;
}

metadata.addNamedQuery(
new NamedQueryDefinitionBuilder()
.setName( name )
.setQuery( query )
.setCacheable( getBoolean( hints, QueryHints.CACHEABLE, name ) )
.setCacheRegion( cacheRegion )
.setTimeout( timeout )
.setFetchSize( fetchSize )
.setFlushMode( getFlushMode( hints, QueryHints.FLUSH_MODE, name ) )
.setCacheMode( getCacheMode( hints, QueryHints.CACHE_MODE, name ) )
.setReadOnly( getBoolean( hints, QueryHints.READ_ONLY, name ) )
.setComment( comment )
.setParameterTypes( null )
.createNamedQueryDefinition()
);
LOG.debugf( "Binding named query: %s => %s", name, query );
builder.setCacheable( getBoolean( hints, QueryHints.CACHEABLE, name ) )
.setCacheRegion( cacheRegion )
.setTimeout( timeout )
.setLockOptions( lockOptions )
.setFetchSize( fetchSize )
.setFlushMode( getFlushMode( hints, QueryHints.FLUSH_MODE, name ) )
.setCacheMode( getCacheMode( hints, QueryHints.CACHE_MODE, name ) )
.setReadOnly( getBoolean( hints, QueryHints.READ_ONLY, name ) )
.setComment( comment )
.setParameterTypes( null );
}

private static void bindNamedNativeQuery(MetadataImplementor metadata, AnnotationInstance annotation) {
Expand Down
Expand Up @@ -120,14 +120,14 @@ private static void bindSqlResultSetMapping(final AnnotationBindingContext bindi
private static void bindEntityResult(final AnnotationBindingContext bindingContext,
final AnnotationInstance entityResult,
final ResultSetMappingDefinition definition) {
final Class entityClass = JandexHelper.getValue( entityResult, "entityClass", Class.class );
final String className = entityClass.getName();
// final Class entityClass = JandexHelper.getValue( entityResult, "entityClass", Class.class );
final String className = JandexHelper.getValue( entityResult, "entityClass", String.class );
//todo look up the whole entitybindings to find the right one seems stupid, but there is no way to look entitybinding
//by class name, since with hbm, hibernate actually supports map one class to multi entities.
final Iterable<EntityBinding> entityBindings = bindingContext.getMetadataImplementor().getEntityBindings();
EntityBinding targetEntityBinding = null;
for ( final EntityBinding entityBinding : entityBindings ) {
if ( className.equals( entityBinding.getEntity().getClass() ) ) {
if ( className.equals( entityBinding.getEntity().getClassName() ) ) {
targetEntityBinding = entityBinding;
break;
}
Expand Down

0 comments on commit 65f204f

Please sign in to comment.