HHH-8893 HBM transform/mock

This commit is contained in:
Brett Meyer 2014-04-03 16:37:20 -04:00
parent 77f0c9a1ed
commit 01ece111a0
4 changed files with 202 additions and 52 deletions

View File

@ -29,6 +29,7 @@ import org.hibernate.annotations.AnyMetaDefs;
import org.hibernate.annotations.AttributeAccessor;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheModeType;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.Check;
import org.hibernate.annotations.CollectionId;
@ -49,6 +50,7 @@ import org.hibernate.annotations.FilterDefs;
import org.hibernate.annotations.FilterJoinTable;
import org.hibernate.annotations.FilterJoinTables;
import org.hibernate.annotations.Filters;
import org.hibernate.annotations.FlushModeType;
import org.hibernate.annotations.Formula;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenericGenerator;
@ -106,7 +108,6 @@ import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
import org.hibernate.annotations.Where;
import org.hibernate.annotations.WhereJoinTable;
import org.jboss.jandex.DotName;
/**
@ -121,6 +122,7 @@ public interface HibernateDotNames {
DotName ATTRIBUTE_ACCESSOR = DotName.createSimple( AttributeAccessor.class.getName() );
DotName BATCH_SIZE = DotName.createSimple( BatchSize.class.getName() );
DotName CACHE = DotName.createSimple( Cache.class.getName() );
DotName CACHE_MODE_TYPE = DotName.createSimple( CacheModeType.class.getName() );
DotName CASCADE = DotName.createSimple( Cascade.class.getName() );
DotName CHECK = DotName.createSimple( Check.class.getName() );
DotName COLLECTION_ID = DotName.createSimple( CollectionId.class.getName() );
@ -142,6 +144,7 @@ public interface HibernateDotNames {
DotName FILTER_DEFS = DotName.createSimple( FilterDefs.class.getName() );
DotName FILTER_JOIN_TABLE = DotName.createSimple( FilterJoinTable.class.getName() );
DotName FILTER_JOIN_TABLES = DotName.createSimple( FilterJoinTables.class.getName() );
DotName FLUSH_MODE_TYPE = DotName.createSimple( FlushModeType.class.getName() );
DotName FORMULA = DotName.createSimple( Formula.class.getName() );
DotName GENERATED = DotName.createSimple( Generated.class.getName() );
DotName GENERIC_GENERATOR = DotName.createSimple( GenericGenerator.class.getName() );

View File

@ -26,16 +26,22 @@ package org.hibernate.metamodel.source.internal.jandex;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.persistence.AccessType;
import org.hibernate.annotations.CacheModeType;
import org.hibernate.annotations.FlushModeType;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.metamodel.source.internal.annotations.util.HibernateDotNames;
import org.hibernate.metamodel.source.internal.jaxb.JaxbAttributes;
import org.hibernate.metamodel.source.internal.jaxb.JaxbDiscriminatorColumn;
import org.hibernate.metamodel.source.internal.jaxb.JaxbEntity;
import org.hibernate.metamodel.source.internal.jaxb.JaxbEntityListeners;
import org.hibernate.metamodel.source.internal.jaxb.JaxbIdClass;
import org.hibernate.metamodel.source.internal.jaxb.JaxbInheritance;
import org.hibernate.metamodel.source.internal.jaxb.JaxbNamedNativeQuery;
import org.hibernate.metamodel.source.internal.jaxb.JaxbNamedQuery;
import org.hibernate.metamodel.source.internal.jaxb.JaxbPostLoad;
import org.hibernate.metamodel.source.internal.jaxb.JaxbPostPersist;
import org.hibernate.metamodel.source.internal.jaxb.JaxbPostRemove;
@ -46,7 +52,6 @@ import org.hibernate.metamodel.source.internal.jaxb.JaxbPreUpdate;
import org.hibernate.metamodel.source.internal.jaxb.JaxbSecondaryTable;
import org.hibernate.metamodel.source.internal.jaxb.JaxbTable;
import org.hibernate.metamodel.source.internal.jaxb.ManagedType;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationValue;
@ -57,6 +62,7 @@ import org.jboss.jandex.DotName;
* Mock <entity> to {@link javax.persistence.Entity @Entity}
*
* @author Strong Liu
* @author Brett Meyer
*/
public class EntityMocker extends AbstractEntityObjectMocker {
private final JaxbEntity entity;
@ -97,6 +103,10 @@ public class EntityMocker extends AbstractEntityObjectMocker {
parsePrimaryKeyJoinColumnList( entity.getPrimaryKeyJoinColumn(), getTarget() );
parseSecondaryTableList( entity.getSecondaryTable(), getTarget() );
// @NamedQuery
parseNamedQueries( entity.getNamedQuery() );
// @NamedNativeQuery
parseNamedNativeQueries( entity.getNamedNativeQuery() );
}
//@Table (entity only)
@ -287,4 +297,75 @@ public class EntityMocker extends AbstractEntityObjectMocker {
return MockHelper.EMPTY_ANNOTATION_VALUE_ARRAY;
}
private void parseNamedQueries( List<JaxbNamedQuery> namedQueries ) {
if (! namedQueries.isEmpty() ) {
AnnotationValue[] namedQueryAnnotations = new AnnotationValue[namedQueries.size()];
for ( int i = 0; i < namedQueries.size(); i++ ) {
JaxbNamedQuery namedQuery = namedQueries.get( i );
List<AnnotationValue> annotationValueList = new ArrayList<AnnotationValue>();
MockHelper.stringValue( "name", namedQuery.getName(), annotationValueList );
MockHelper.stringValue( "query", namedQuery.getQuery(), annotationValueList );
MockHelper.stringValue( "cacheRegion", namedQuery.getCacheRegion(), annotationValueList );
MockHelper.stringValue( "comment", namedQuery.getComment(), annotationValueList );
MockHelper.booleanValue( "cacheable", namedQuery.isCacheable(), annotationValueList );
MockHelper.booleanValue( "readOnly", namedQuery.isReadOnly(), annotationValueList );
MockHelper.integerValue( "fetchSize", namedQuery.getFetchSize(), annotationValueList );
MockHelper.integerValue( "timeout", namedQuery.getTimeout(), annotationValueList );
MockHelper.enumValue( "cacheMode", HibernateDotNames.CACHE_MODE_TYPE,
MockHelper.convert( namedQuery.getCacheMode() ), annotationValueList );
MockHelper.enumValue( "flushMode", HibernateDotNames.FLUSH_MODE_TYPE,
MockHelper.convert( namedQuery.getFlushMode() ), annotationValueList );
AnnotationInstance annotationInstance = create(
HibernateDotNames.NAMED_QUERY, null, annotationValueList );
namedQueryAnnotations[i] = MockHelper.nestedAnnotationValue( "", annotationInstance );
}
List<AnnotationValue> annotationValueList = new ArrayList<AnnotationValue>();
MockHelper.addToCollectionIfNotNull( annotationValueList,
AnnotationValue.createArrayValue( "value", namedQueryAnnotations ) );
create( HibernateDotNames.NAMED_QUERIES, getTarget(), annotationValueList );
}
}
private void parseNamedNativeQueries( List<JaxbNamedNativeQuery> namedQueries ) {
if (! namedQueries.isEmpty() ) {
AnnotationValue[] namedQueryAnnotations = new AnnotationValue[namedQueries.size()];
for ( int i = 0; i < namedQueries.size(); i++ ) {
JaxbNamedNativeQuery namedQuery = namedQueries.get( i );
List<AnnotationValue> annotationValueList = new ArrayList<AnnotationValue>();
MockHelper.stringValue( "name", namedQuery.getName(), annotationValueList );
MockHelper.stringValue( "query", namedQuery.getQuery(), annotationValueList );
MockHelper.stringValue( "cacheRegion", namedQuery.getCacheRegion(), annotationValueList );
MockHelper.stringValue( "comment", namedQuery.getComment(), annotationValueList );
MockHelper.stringValue( "resultSetMapping", namedQuery.getResultSetMapping(), annotationValueList );
MockHelper.booleanValue( "cacheable", namedQuery.isCacheable(), annotationValueList );
MockHelper.booleanValue( "readOnly", namedQuery.isReadOnly(), annotationValueList );
// TODO: add #callable to the schema?
// MockHelper.booleanValue( "callable", namedQuery.isCallable(), annotationValueList );
MockHelper.integerValue( "fetchSize", namedQuery.getFetchSize(), annotationValueList );
MockHelper.integerValue( "timeout", namedQuery.getTimeout(), annotationValueList );
MockHelper.enumValue( "cacheMode", HibernateDotNames.CACHE_MODE_TYPE,
MockHelper.convert( namedQuery.getCacheMode() ), annotationValueList );
MockHelper.enumValue( "flushMode", HibernateDotNames.FLUSH_MODE_TYPE,
MockHelper.convert( namedQuery.getFlushMode() ), annotationValueList );
MockHelper.classValue( "resultClass", namedQuery.getResultClass(), annotationValueList,
indexBuilder.getServiceRegistry() );
AnnotationInstance annotationInstance = create(
HibernateDotNames.NAMED_NATIVE_QUERY, null, annotationValueList );
namedQueryAnnotations[i] = MockHelper.nestedAnnotationValue( "", annotationInstance );
}
List<AnnotationValue> annotationValueList = new ArrayList<AnnotationValue>();
MockHelper.addToCollectionIfNotNull( annotationValueList,
AnnotationValue.createArrayValue( "value", namedQueryAnnotations ) );
create( HibernateDotNames.NAMED_NATIVE_QUERIES, getTarget(), annotationValueList );
}
}
}

View File

@ -29,16 +29,21 @@ import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.CascadeType;
import org.hibernate.AssertionFailure;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.annotations.CacheModeType;
import org.hibernate.annotations.FlushModeType;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper;
import org.hibernate.metamodel.source.internal.annotations.util.JPADotNames;
import org.hibernate.metamodel.source.internal.jaxb.JaxbCacheModeType;
import org.hibernate.metamodel.source.internal.jaxb.JaxbCascadeType;
import org.hibernate.service.ServiceRegistry;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationValue;
@ -457,4 +462,43 @@ public class MockHelper {
return kind;
}
public static FlushModeType convert(FlushMode flushMode) {
if (flushMode == null) {
return null;
}
switch ( flushMode ) {
case ALWAYS:
return FlushModeType.ALWAYS;
case AUTO:
return FlushModeType.AUTO;
case COMMIT:
return FlushModeType.COMMIT;
case MANUAL:
return FlushModeType.MANUAL;
default:
throw new AssertionFailure( "Unknown flushMode: " + flushMode );
}
}
public static CacheModeType convert(JaxbCacheModeType jaxbCacheMode) {
if (jaxbCacheMode == null) {
return null;
}
switch ( jaxbCacheMode ) {
case GET:
return CacheModeType.GET;
case IGNORE:
return CacheModeType.IGNORE;
case NORMAL:
return CacheModeType.NORMAL;
case PUT:
return CacheModeType.PUT;
case REFRESH:
return CacheModeType.REFRESH;
default:
throw new AssertionFailure( "Unknown jaxbCacheMode: " + jaxbCacheMode );
}
}
}

View File

@ -39,6 +39,7 @@ import org.jboss.logging.Logger;
* Transforms a JAXB binding of a hbm.xml file into a unified orm.xml representation
*
* @author Steve Ebersole
* @author Brett Meyer
*/
public class HbmXmlTransformer {
private static final Logger log = Logger.getLogger( HbmXmlTransformer.class );
@ -226,33 +227,38 @@ public class HbmXmlTransformer {
}
for ( JaxbQueryElement hbmQuery : hbmXmlMapping.getQuery() ) {
final JaxbNamedQuery query = new JaxbNamedQuery();
ormRoot.getNamedQuery().add( query );
query.setName( hbmQuery.getName() );
query.setCacheable( hbmQuery.isCacheable() );
query.setCacheMode( convert( hbmQuery.getCacheMode() ) );
query.setCacheRegion( hbmQuery.getCacheRegion() );
query.setComment( hbmQuery.getComment() );
query.setFetchSize( hbmQuery.getFetchSize() );
query.setFlushMode( interpret( hbmQuery.getFlushMode() ) );
query.setFetchSize( hbmQuery.getFetchSize() );
query.setReadOnly( hbmQuery.isReadOnly() );
query.setTimeout( hbmQuery.getTimeout() );
ormRoot.getNamedQuery().add( convert( hbmQuery, hbmQuery.getName() ) );
}
}
private JaxbNamedQuery convert(JaxbQueryElement hbmQuery, String name) {
final JaxbNamedQuery query = new JaxbNamedQuery();
query.setName( name );
query.setCacheable( hbmQuery.isCacheable() );
query.setCacheMode( convert( hbmQuery.getCacheMode() ) );
query.setCacheRegion( hbmQuery.getCacheRegion() );
query.setComment( hbmQuery.getComment() );
query.setFetchSize( hbmQuery.getFetchSize() );
query.setFlushMode( interpret( hbmQuery.getFlushMode() ) );
query.setFetchSize( hbmQuery.getFetchSize() );
query.setReadOnly( hbmQuery.isReadOnly() );
query.setTimeout( hbmQuery.getTimeout() );
// JaxbQueryElement#content elements can be either the query or parameters
for ( Object content : hbmQuery.getContent() ) {
if ( String.class.isInstance( content ) ) {
query.setQuery( (String) content );
}
else {
final JaxbQueryParamElement hbmQueryParam = (JaxbQueryParamElement) content;
final JaxbQueryParamType queryParam = new JaxbQueryParamType();
query.getQueryParam().add( queryParam );
queryParam.setName( hbmQueryParam.getName() );
queryParam.setType( hbmQueryParam.getType() );
}
// JaxbQueryElement#content elements can be either the query or parameters
for ( Object content : hbmQuery.getContent() ) {
if ( String.class.isInstance( content ) ) {
query.setQuery( (String) content );
}
else {
final JaxbQueryParamElement hbmQueryParam = (JaxbQueryParamElement) content;
final JaxbQueryParamType queryParam = new JaxbQueryParamType();
query.getQueryParam().add( queryParam );
queryParam.setName( hbmQueryParam.getName() );
queryParam.setType( hbmQueryParam.getType() );
}
}
return query;
}
private JaxbCacheModeType convert(JaxbCacheModeAttribute cacheMode) {
@ -279,33 +285,38 @@ public class HbmXmlTransformer {
}
for ( JaxbSqlQueryElement hbmQuery : hbmXmlMapping.getSqlQuery() ) {
final JaxbNamedNativeQuery query = new JaxbNamedNativeQuery();
ormRoot.getNamedNativeQuery().add( query );
query.setName( hbmQuery.getName() );
query.setCacheable( hbmQuery.isCacheable() );
query.setCacheMode( convert( hbmQuery.getCacheMode() ) );
query.setCacheRegion( hbmQuery.getCacheRegion() );
query.setComment( hbmQuery.getComment() );
query.setFetchSize( hbmQuery.getFetchSize() );
query.setFlushMode( interpret( hbmQuery.getFlushMode() ) );
query.setFetchSize( hbmQuery.getFetchSize() );
query.setReadOnly( hbmQuery.isReadOnly() );
query.setTimeout( hbmQuery.getTimeout() );
ormRoot.getNamedNativeQuery().add( convert( hbmQuery, hbmQuery.getName() ) );
}
}
private JaxbNamedNativeQuery convert(JaxbSqlQueryElement hbmQuery, String name) {
final JaxbNamedNativeQuery query = new JaxbNamedNativeQuery();
query.setName( name );
query.setCacheable( hbmQuery.isCacheable() );
query.setCacheMode( convert( hbmQuery.getCacheMode() ) );
query.setCacheRegion( hbmQuery.getCacheRegion() );
query.setComment( hbmQuery.getComment() );
query.setFetchSize( hbmQuery.getFetchSize() );
query.setFlushMode( interpret( hbmQuery.getFlushMode() ) );
query.setFetchSize( hbmQuery.getFetchSize() );
query.setReadOnly( hbmQuery.isReadOnly() );
query.setTimeout( hbmQuery.getTimeout() );
// JaxbQueryElement#content elements can be either the query or parameters
for ( Object content : hbmQuery.getContent() ) {
if ( String.class.isInstance( content ) ) {
query.setQuery( (String) content );
}
else {
final JaxbQueryParamElement hbmQueryParam = (JaxbQueryParamElement) content;
final JaxbQueryParamType queryParam = new JaxbQueryParamType();
query.getQueryParam().add( queryParam );
queryParam.setName( hbmQueryParam.getName() );
queryParam.setType( hbmQueryParam.getType() );
}
// JaxbQueryElement#content elements can be either the query or parameters
for ( Object content : hbmQuery.getContent() ) {
if ( String.class.isInstance( content ) ) {
query.setQuery( (String) content );
}
else {
final JaxbQueryParamElement hbmQueryParam = (JaxbQueryParamElement) content;
final JaxbQueryParamType queryParam = new JaxbQueryParamType();
query.getQueryParam().add( queryParam );
queryParam.setName( hbmQueryParam.getName() );
queryParam.setType( hbmQueryParam.getType() );
}
}
return query;
}
private void transferDatabaseObjects(JaxbHibernateMapping hbmXmlMapping, JaxbEntityMappings ormRoot) {
@ -460,8 +471,19 @@ public class HbmXmlTransformer {
entity.getCache().setUsage( hbmClass.getCache().getUsage().value() );
entity.getCache().setInclude( hbmClass.getCache().getInclude().value() );
}
if (! hbmClass.getQuery().isEmpty() ) {
for ( JaxbQueryElement hbmQuery : hbmClass.getQuery() ) {
entity.getNamedQuery().add( convert( hbmQuery, entity.getName() + "." + hbmQuery.getName() ) );
}
}
if (! hbmClass.getSqlQuery().isEmpty() ) {
for ( JaxbSqlQueryElement hbmQuery : hbmClass.getSqlQuery() ) {
entity.getNamedNativeQuery().add( convert( hbmQuery, entity.getName() + "." + hbmQuery.getName() ) );
}
}
// todo : transfer named queries
// todo : transfer filters
// todo : transfer fetch-profiles