diff --git a/build.gradle b/build.gradle index 33b4e61574..1c6745db33 100644 --- a/build.gradle +++ b/build.gradle @@ -163,6 +163,7 @@ subprojects { subProject -> compilerArgs: [ "-nowarn", "-proc:only", + "-encoding", "UTF-8", "-processor", "org.jboss.logging.LoggingToolsProcessor", "-s", "$sourceSets.main.generatedLoggingSrcDir.absolutePath" ] @@ -177,7 +178,7 @@ subprojects { subProject -> generateMainLoggingClasses.logging.captureStandardError(LogLevel.INFO) compileJava.dependsOn generateMainLoggingClasses - compileJava.options.define(compilerArgs: ["-proc:none"]) + compileJava.options.define(compilerArgs: ["-proc:none", "-encoding", "UTF-8"]) manifest.mainAttributes( provider: 'gradle', diff --git a/hibernate-core/src/main/java/org/hibernate/annotations/OptimisticLock.java b/hibernate-core/src/main/java/org/hibernate/annotations/OptimisticLock.java index 407426008c..876efa84c6 100644 --- a/hibernate-core/src/main/java/org/hibernate/annotations/OptimisticLock.java +++ b/hibernate-core/src/main/java/org/hibernate/annotations/OptimisticLock.java @@ -22,24 +22,23 @@ * Boston, MA 02110-1301 USA */ package org.hibernate.annotations; + import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** - * Whether or not update entity's version on property's change - * If the annotation is not present, the property is involved in the optimistic lock srategy (default) + * Whether or not a change of the annotated property will trigger a entity version increment. + * If the annotation is not present, the property is involved in the optimistic lock strategy (default). * * @author Logi Ragnarsson */ -@Target( {ElementType.METHOD, ElementType.FIELD} ) -@Retention( RetentionPolicy.RUNTIME ) +@Target( { ElementType.METHOD, ElementType.FIELD }) +@Retention(RetentionPolicy.RUNTIME) public @interface OptimisticLock { - /** - * If true, the annotated property change will not trigger a version upgrade + * @return If {@code true}, the annotated property change will not trigger a version increment. */ boolean excluded(); - } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java b/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java index 11cd772599..95f67098e9 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/CoreMessageLogger.java @@ -59,6 +59,7 @@ import org.hibernate.type.BasicType; import org.hibernate.type.SerializationException; import org.hibernate.type.Type; +import static org.jboss.logging.Logger.Level.DEBUG; import static org.jboss.logging.Logger.Level.ERROR; import static org.jboss.logging.Logger.Level.INFO; import static org.jboss.logging.Logger.Level.WARN; @@ -115,7 +116,7 @@ public interface CoreMessageLogger extends BasicLogger { @Message(value = "Cleaning up connection pool [%s]", id = 30) void cleaningUpConnectionPool(String url); - @LogMessage(level = INFO) + @LogMessage(level = DEBUG) @Message(value = "Closing", id = 31) void closing(); @@ -539,11 +540,11 @@ public interface CoreMessageLogger extends BasicLogger { @Message(value = "Logging statistics....", id = 161) void loggingStatistics(); - @LogMessage(level = INFO) + @LogMessage(level = DEBUG) @Message(value = "*** Logical connection closed ***", id = 162) void logicalConnectionClosed(); - @LogMessage(level = INFO) + @LogMessage(level = DEBUG) @Message(value = "Logical connection releasing its physical connection", id = 163) void logicalConnectionReleasingPhysicalConnection(); diff --git a/hibernate-core/src/main/java/org/hibernate/internal/util/ReflectHelper.java b/hibernate-core/src/main/java/org/hibernate/internal/util/ReflectHelper.java index 1927d78b36..39ba0dcc33 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/util/ReflectHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/util/ReflectHelper.java @@ -232,6 +232,18 @@ public final class ReflectHelper { } } + /** + * Attempt to resolve the specified property type through reflection. + * + * @param clazz The class owning the property. + * @param name The name of the property. + * @return The type of the property. + * @throws MappingException Indicates we were unable to locate the property. + */ + public static Class reflectedPropertyClass(Class clazz, String name) throws MappingException { + return getter( clazz, name ).getReturnType(); + } + private static Getter getter(Class clazz, String name) throws MappingException { try { return BASIC_PROPERTY_ACCESSOR.getGetter( clazz, name ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/Metadata.java b/hibernate-core/src/main/java/org/hibernate/metamodel/Metadata.java index 3affc9ea69..0c2b2cf7d4 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/Metadata.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/Metadata.java @@ -24,9 +24,28 @@ package org.hibernate.metamodel; +import javax.persistence.SharedCacheMode; + +import org.hibernate.SessionFactory; +import org.hibernate.cache.spi.access.AccessType; +import org.hibernate.cfg.NamingStrategy; + /** * @author Steve Ebersole */ public interface Metadata { + /** + * Exposes the options used to produce a {@link Metadata} instance. + */ + public static interface Options { + public SourceProcessingOrder getSourceProcessingOrder(); + public NamingStrategy getNamingStrategy(); + public SharedCacheMode getSharedCacheMode(); + public AccessType getDefaultAccessType(); + public boolean useNewIdentifierGenerators(); + } + public Options getOptions(); + + public SessionFactory buildSessionFactory(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/MetadataBuilder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/MetadataBuilder.java index 1499f53c6d..0746b152c2 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/MetadataBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/MetadataBuilder.java @@ -25,6 +25,7 @@ package org.hibernate.metamodel; import javax.persistence.SharedCacheMode; +import org.hibernate.cache.spi.access.AccessType; import org.hibernate.cfg.NamingStrategy; /** @@ -38,5 +39,9 @@ public interface MetadataBuilder { public MetadataBuilder with(SharedCacheMode cacheMode); + public MetadataBuilder with(AccessType accessType); + + public MetadataBuilder withNewIdentifierGeneratorsEnabled(boolean enabled); + public Metadata buildMetadata(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/AbstractAttributeBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/AbstractAttributeBinding.java index db72ad9424..5e746a0358 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/AbstractAttributeBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/AbstractAttributeBinding.java @@ -30,6 +30,8 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.hibernate.MappingException; +import org.hibernate.metamodel.binding.state.AttributeBindingState; import org.hibernate.metamodel.domain.Attribute; import org.hibernate.metamodel.domain.MetaAttribute; import org.hibernate.metamodel.relational.Column; @@ -38,10 +40,10 @@ import org.hibernate.metamodel.relational.SimpleValue; import org.hibernate.metamodel.relational.TableSpecification; import org.hibernate.metamodel.relational.Tuple; import org.hibernate.metamodel.relational.Value; -import org.hibernate.metamodel.relational.ValueFactory; -import org.hibernate.metamodel.state.domain.AttributeDomainState; -import org.hibernate.metamodel.state.relational.ValueRelationalState; -import org.hibernate.metamodel.state.relational.TupleRelationalState; +import org.hibernate.metamodel.relational.state.SimpleValueRelationalState; +import org.hibernate.metamodel.relational.state.TupleRelationalState; +import org.hibernate.metamodel.relational.state.ValueCreator; +import org.hibernate.metamodel.relational.state.ValueRelationalState; /** * TODO : javadoc @@ -49,11 +51,9 @@ import org.hibernate.metamodel.state.relational.TupleRelationalState; * @author Steve Ebersole */ public abstract class AbstractAttributeBinding implements AttributeBinding { - private final HibernateTypeDescriptor hibernateTypeDescriptor = new HibernateTypeDescriptor(); private final EntityBinding entityBinding; - private final Set entityReferencingAttributeBindings = - new HashSet(); + private final Set entityReferencingAttributeBindings = new HashSet(); private Attribute attribute; private Value value; @@ -73,9 +73,9 @@ public abstract class AbstractAttributeBinding implements AttributeBinding { this.entityBinding = entityBinding; } - protected void initialize(AttributeDomainState state) { - hibernateTypeDescriptor.initialize( state.getHibernateTypeDescriptor() ); - attribute = state.getAttribute(); + protected void initialize(AttributeBindingState state) { + hibernateTypeDescriptor.setTypeName( state.getTypeName() ); + hibernateTypeDescriptor.setTypeParameters( state.getTypeParameters() ); isLazy = state.isLazy(); propertyAccessorName = state.getPropertyAccessorName(); isAlternateUniqueKey = state.isAlternateUniqueKey(); @@ -107,24 +107,37 @@ public abstract class AbstractAttributeBinding implements AttributeBinding { return false; } - protected void initializeValue(ValueRelationalState state) { - value = ValueFactory.createValue( getEntityBinding().getBaseTable(), + protected final boolean isPrimaryKey() { + return this == getEntityBinding().getEntityIdentifier().getValueBinding(); + } + + protected void initializeValueRelationalState(ValueRelationalState state) { + // TODO: change to have ValueRelationalState generate the value + value = ValueCreator.createValue( + getEntityBinding().getBaseTable(), getAttribute().getName(), - convertToSimpleRelationalStateIfPossible( state ), + state, forceNonNullable(), forceUnique() ); - } - - // TODO: should a single-valued tuple always be converted??? - protected ValueRelationalState convertToSimpleRelationalStateIfPossible(ValueRelationalState state) { - if ( !TupleRelationalState.class.isInstance( state ) ) { - return state; + // TODO: not sure I like this here... + if ( isPrimaryKey() ) { + if ( SimpleValue.class.isInstance( value ) ) { + if ( !Column.class.isInstance( value ) ) { + // this should never ever happen.. + throw new MappingException( "Simple ID is not a column." ); + } + entityBinding.getBaseTable().getPrimaryKey().addColumn( Column.class.cast( value ) ); + } + else { + for ( SimpleValueRelationalState val : TupleRelationalState.class.cast( state ) + .getRelationalStates() ) { + if ( Column.class.isInstance( val ) ) { + entityBinding.getBaseTable().getPrimaryKey().addColumn( Column.class.cast( val ) ); + } + } + } } - TupleRelationalState tupleRelationalState = TupleRelationalState.class.cast( state ); - return tupleRelationalState.getRelationalStates().size() == 1 ? - tupleRelationalState.getRelationalStates().get( 0 ) : - state; } @Override @@ -145,10 +158,6 @@ public abstract class AbstractAttributeBinding implements AttributeBinding { return optimisticLockable; } - public String getNodeName() { - return nodeName; - } - @Override public Map getMetaAttributes() { return metaAttributes; @@ -159,13 +168,8 @@ public abstract class AbstractAttributeBinding implements AttributeBinding { return value == null ? Collections.emptyList() : value instanceof Tuple - ? ( (Tuple) value ).values() - : Collections.singletonList( (SimpleValue) value ); - } - - @Override - public TableSpecification getTable() { - return getValue().getTable(); + ? ( (Tuple) value ).values() + : Collections.singletonList( (SimpleValue) value ); } @Override @@ -210,9 +214,9 @@ public abstract class AbstractAttributeBinding implements AttributeBinding { public boolean[] getColumnInsertability() { List tmp = new ArrayList(); for ( SimpleValue simpleValue : getValues() ) { - tmp.add( ! ( simpleValue instanceof DerivedValue ) ); + tmp.add( !( simpleValue instanceof DerivedValue ) ); } - boolean[] rtn = new boolean[ tmp.size() ]; + boolean[] rtn = new boolean[tmp.size()]; int i = 0; for ( Boolean insertable : tmp ) { rtn[i++] = insertable.booleanValue(); @@ -243,11 +247,10 @@ public abstract class AbstractAttributeBinding implements AttributeBinding { } public void validate() { - if ( ! entityReferencingAttributeBindings.isEmpty() ) { + if ( !entityReferencingAttributeBindings.isEmpty() ) { // TODO; validate that this AttributeBinding can be a target of an entity reference // (e.g., this attribute is the primary key or there is a unique-key) // can a unique attribute be used as a target? if so, does it need to be non-null? } } - } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/AttributeBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/AttributeBinding.java index c68b18b298..ef616db93e 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/AttributeBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/AttributeBinding.java @@ -74,27 +74,14 @@ public interface AttributeBinding { public Map getMetaAttributes(); /** - * In the case that {@link #getValue()} represents a {@link org.hibernate.metamodel.relational.Tuple} this method + * @return In the case that {@link #getValue()} represents a {@link org.hibernate.metamodel.relational.Tuple} this method * gives access to its compound values. In the case of {@link org.hibernate.metamodel.relational.SimpleValue}, * we return an Iterable over that single simple value. - * - * @return */ public Iterable getValues(); - /** - * @return - * - * @deprecated Use {@link #getValue()}.{@link Value#getTable() getTable()} instead; to be removed on completion of new metamodel code - */ - @Deprecated - public TableSpecification getTable(); - public String getPropertyAccessorName(); - /** - * @return - */ public boolean hasFormula(); public boolean isAlternateUniqueKey(); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/BagBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/BagBinding.java index eb97820ef9..c00bfc332c 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/BagBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/BagBinding.java @@ -23,6 +23,8 @@ */ package org.hibernate.metamodel.binding; +import org.hibernate.metamodel.binding.state.PluralAttributeBindingState; + /** * TODO : javadoc * @@ -32,4 +34,9 @@ public class BagBinding extends PluralAttributeBinding { protected BagBinding(EntityBinding entityBinding) { super( entityBinding ); } + + public BagBinding initialize(PluralAttributeBindingState bindingState) { + super.initialize( bindingState ); + return this; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/CollectionElement.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/CollectionElement.java index 0ffe67b82b..22edb0f25a 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/CollectionElement.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/CollectionElement.java @@ -24,7 +24,6 @@ package org.hibernate.metamodel.binding; import org.hibernate.metamodel.relational.Value; -import org.hibernate.metamodel.state.domain.CollectionElementDomainState; /** * TODO : javadoc @@ -44,8 +43,13 @@ public class CollectionElement { this.collectionBinding = collectionBinding; } - public void initialize(CollectionElementDomainState state) { - hibernateTypeDescriptor.initialize( state.getHibernateTypeDescriptor() ); - nodeName = state.getNodeName(); + /* package-protected */ + void setTypeName(String typeName) { + hibernateTypeDescriptor.setTypeName( typeName ); + } + + /* package-protected */ + void setNodeName(String nodeName) { + this.nodeName = nodeName; } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/EntityBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/EntityBinding.java index 5370101cf5..83effb1d8b 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/EntityBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/EntityBinding.java @@ -39,12 +39,13 @@ import org.hibernate.metamodel.domain.MetaAttribute; import org.hibernate.metamodel.relational.Column; import org.hibernate.metamodel.relational.TableSpecification; import org.hibernate.metamodel.source.hbm.HbmHelper; +import org.hibernate.metamodel.source.hbm.MappingDefaults; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSqlDeleteElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSqlInsertElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSqlUpdateElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSynchronizeElement; -import org.hibernate.metamodel.source.util.MappingHelper; +import org.hibernate.metamodel.source.hbm.util.MappingHelper; /** * Provides the link between the domain and the relational model for an entity. @@ -207,16 +208,6 @@ public class EntityBinding { return entityDiscriminator; } - public void bindEntityDiscriminator(SimpleAttributeBinding attributeBinding) { - if ( !Column.class.isInstance( attributeBinding.getValue() ) ) { - throw new MappingException( - "Identifier value must be a Column; instead it is: " + attributeBinding.getValue().getClass() - ); - } - entityDiscriminator.setValueBinding( attributeBinding ); - baseTable.getPrimaryKey().addColumn( Column.class.cast( attributeBinding.getValue() ) ); - } - public void setInheritanceType(InheritanceType entityInheritanceType) { this.entityInheritanceType = entityInheritanceType; } @@ -241,27 +232,26 @@ public class EntityBinding { return entityReferencingAttributeBindings; } - public SimpleAttributeBinding makeSimplePrimaryKeyAttributeBinding(String name) { + public SimpleAttributeBinding makeSimpleIdAttributeBinding(String name) { final SimpleAttributeBinding binding = makeSimpleAttributeBinding( name, true, true ); getEntityIdentifier().setValueBinding( binding ); return binding; } - public SimpleAttributeBinding makeEntityDiscriminatorBinding(String name) { + public EntityDiscriminator makeEntityDiscriminator(String attributeName) { if ( entityDiscriminator != null ) { throw new AssertionFailure( "Creation of entity discriminator was called more than once" ); } entityDiscriminator = new EntityDiscriminator(); - entityDiscriminator.setValueBinding( makeSimpleAttributeBinding( name, true, false ) ); - return entityDiscriminator.getValueBinding(); + entityDiscriminator.setValueBinding( makeSimpleAttributeBinding( attributeName, true, false ) ); + return entityDiscriminator; } - public SimpleAttributeBinding makeVersionBinding(String name) { - versionBinding = makeSimpleAttributeBinding( name, true, false ); + public SimpleAttributeBinding makeVersionBinding(String attributeName) { + versionBinding = makeSimpleAttributeBinding( attributeName, true, false ); return versionBinding; } - public SimpleAttributeBinding makeSimpleAttributeBinding(String name) { return makeSimpleAttributeBinding( name, false, false ); } @@ -273,17 +263,17 @@ public class EntityBinding { return binding; } - public ManyToOneAttributeBinding makeManyToOneAttributeBinding(String name) { + public ManyToOneAttributeBinding makeManyToOneAttributeBinding(String attributeName) { final ManyToOneAttributeBinding binding = new ManyToOneAttributeBinding( this ); - registerAttributeBinding( name, binding ); - binding.setAttribute( entity.getAttribute( name ) ); + registerAttributeBinding( attributeName, binding ); + binding.setAttribute( entity.getAttribute( attributeName ) ); return binding; } - public BagBinding makeBagAttributeBinding(String name) { + public BagBinding makeBagAttributeBinding(String attributeName) { final BagBinding binding = new BagBinding( this ); - registerAttributeBinding( name, binding ); - binding.setAttribute( entity.getAttribute( name ) ); + registerAttributeBinding( attributeName, binding ); + binding.setAttribute( entity.getAttribute( attributeName ) ); return binding; } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/EntityDiscriminator.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/EntityDiscriminator.java index 39e4cb2131..fd35ac7505 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/EntityDiscriminator.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/EntityDiscriminator.java @@ -23,6 +23,9 @@ */ package org.hibernate.metamodel.binding; +import org.hibernate.metamodel.binding.state.DiscriminatorBindingState; +import org.hibernate.metamodel.relational.state.ValueRelationalState; + /** * Binding of the discriminator in a entity hierarchy * @@ -40,10 +43,26 @@ public class EntityDiscriminator { return valueBinding; } - public void setValueBinding(SimpleAttributeBinding valueBinding) { + /* package-protected */ + void setValueBinding(SimpleAttributeBinding valueBinding) { this.valueBinding = valueBinding; } + public EntityDiscriminator initialize(DiscriminatorBindingState state) { + if ( valueBinding == null ) { + throw new IllegalStateException( "Cannot bind state because the value binding has not been initialized." ); + } + this.valueBinding.initialize( state ); + this.forced = state.isForced(); + this.inserted = state.isInsertable(); + return this; + } + + public EntityDiscriminator initialize(ValueRelationalState state) { + valueBinding.initialize( state ); + return this; + } + public boolean isForced() { return forced; } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/HibernateTypeDescriptor.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/HibernateTypeDescriptor.java index 4c3aca15e3..d65925f1b6 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/HibernateTypeDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/HibernateTypeDescriptor.java @@ -23,7 +23,7 @@ */ package org.hibernate.metamodel.binding; -import java.util.Properties; +import java.util.Map; import org.hibernate.type.Type; @@ -35,12 +35,13 @@ import org.hibernate.type.Type; public class HibernateTypeDescriptor { private String typeName; private Type explicitType; - private Properties typeParameters; + private Map typeParameters; public String getTypeName() { return typeName; } + /* package-protected */ public void setTypeName(String typeName) { this.typeName = typeName; } @@ -49,13 +50,17 @@ public class HibernateTypeDescriptor { return explicitType; } + /* package-protected */ public void setExplicitType(Type explicitType) { this.explicitType = explicitType; } - public void initialize(HibernateTypeDescriptor hibernateTypeDescriptor) { - typeName = hibernateTypeDescriptor.typeName; - explicitType = hibernateTypeDescriptor.explicitType; - typeParameters = hibernateTypeDescriptor.typeParameters; + public Map getTypeParameters() { + return typeParameters; + } + + /* package-protected */ + void setTypeParameters(Map typeParameters) { + this.typeParameters = typeParameters; } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/KeyValueBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/KeyValueBinding.java index 50cd12250a..e1d2489769 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/KeyValueBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/KeyValueBinding.java @@ -29,9 +29,9 @@ package org.hibernate.metamodel.binding; * @author Steve Ebersole */ public interface KeyValueBinding extends AttributeBinding { - public boolean isKeyCasadeDeleteEnabled(); + public boolean isKeyCascadeDeleteEnabled(); public String getUnsavedValue(); - public boolean isUpdateable(); + public boolean isUpdatable(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/ManyToOneAttributeBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/ManyToOneAttributeBinding.java index 8eea6ecadd..a00197ae1e 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/ManyToOneAttributeBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/ManyToOneAttributeBinding.java @@ -26,19 +26,18 @@ package org.hibernate.metamodel.binding; import java.util.Iterator; import org.hibernate.MappingException; -import org.hibernate.internal.util.ReflectHelper; +import org.hibernate.metamodel.binding.state.ManyToOneAttributeBindingState; +import org.hibernate.metamodel.relational.Column; import org.hibernate.metamodel.relational.ForeignKey; import org.hibernate.metamodel.relational.SimpleValue; -import org.hibernate.metamodel.relational.Column; -import org.hibernate.metamodel.state.domain.ManyToOneAttributeDomainState; -import org.hibernate.metamodel.state.relational.ManyToOneRelationalState; +import org.hibernate.metamodel.relational.state.ManyToOneRelationalState; /** * TODO : javadoc * * @author Gail Badner */ -public class ManyToOneAttributeBinding extends SingularAttributeBinding implements EntityReferencingAttributeBinding { +public class ManyToOneAttributeBinding extends SimpleAttributeBinding implements EntityReferencingAttributeBinding { private boolean isLogicalOneToOne; private boolean isPropertyReference; private String foreignKeyName; @@ -51,24 +50,19 @@ public class ManyToOneAttributeBinding extends SingularAttributeBinding implemen super( entityBinding, false, false ); } - public final void initialize(ManyToOneAttributeDomainState state) { + public final ManyToOneAttributeBinding initialize(ManyToOneAttributeBindingState state) { super.initialize( state ); isPropertyReference = state.getReferencedAttributeName() != null; referencedAttributeName = state.getReferencedAttributeName(); referencedEntityName = state.getReferencedEntityName(); - if ( referencedEntityName == null ) { - referencedEntityName = - ReflectHelper.reflectedPropertyClass( - getEntityBinding().getEntity().getName(), - state.getAttribute().getName() - ).getName(); - } + return this; } - public final void initialize(ManyToOneRelationalState state) { - super.initializeValue( state ); + public final ManyToOneAttributeBinding initialize(ManyToOneRelationalState state) { + super.initializeValueRelationalState( state ); isLogicalOneToOne = state.isLogicalOneToOne(); foreignKeyName = state.getForeignKeyName(); + return this; } public final boolean isPropertyReference() { @@ -82,7 +76,7 @@ public class ManyToOneAttributeBinding extends SingularAttributeBinding implemen public final String getReferencedAttributeName() { if ( referencedAttributeName == null ) { throw new IllegalStateException( - "Referenced attribute name is not available." + "Referenced attribute name is not available." ); } return referencedAttributeName; @@ -93,7 +87,7 @@ public class ManyToOneAttributeBinding extends SingularAttributeBinding implemen } public final EntityBinding getReferencedEntityBinding() { - if ( ! isReferenceResolved() ) { + if ( !isReferenceResolved() ) { throw new IllegalStateException( "EntityBinding reference has not be referenced." ); } // TODO: throw exception if referencedEntityBinding is null? @@ -101,7 +95,7 @@ public class ManyToOneAttributeBinding extends SingularAttributeBinding implemen } public final void resolveReference(AttributeBinding referencedAttributeBinding) { - if ( ! referencedEntityName.equals( referencedAttributeBinding.getEntityBinding().getEntity().getName() ) ) { + if ( !referencedEntityName.equals( referencedAttributeBinding.getEntityBinding().getEntity().getName() ) ) { throw new IllegalStateException( "attempt to set EntityBinding with name: [" + referencedAttributeBinding.getEntityBinding().getEntity().getName() + @@ -111,10 +105,10 @@ public class ManyToOneAttributeBinding extends SingularAttributeBinding implemen if ( referencedAttributeName == null ) { referencedAttributeName = referencedAttributeBinding.getAttribute().getName(); } - else if ( ! referencedAttributeName.equals( referencedAttributeBinding.getAttribute().getName() ) ) { + else if ( !referencedAttributeName.equals( referencedAttributeBinding.getAttribute().getName() ) ) { throw new IllegalStateException( "Inconsistent attribute name; expected: " + referencedAttributeName + - "actual: " + referencedAttributeBinding.getAttribute().getName() + "actual: " + referencedAttributeBinding.getAttribute().getName() ); } this.referencedAttributeBinding = referencedAttributeBinding; @@ -123,18 +117,21 @@ public class ManyToOneAttributeBinding extends SingularAttributeBinding implemen private void buildForeignKey() { // TODO: move this stuff to relational model - ForeignKey foreignKey = getTable().createForeignKey( referencedAttributeBinding.getTable(), foreignKeyName ); + ForeignKey foreignKey = getValue().getTable() + .createForeignKey( referencedAttributeBinding.getValue().getTable(), foreignKeyName ); Iterator referencingValueIterator = getValues().iterator(); - Iterator targetValueIterator = referencedAttributeBinding.getValues().iterator(); + Iterator targetValueIterator = referencedAttributeBinding.getValues().iterator(); while ( referencingValueIterator.hasNext() ) { - if ( ! targetValueIterator.hasNext() ) { + if ( !targetValueIterator.hasNext() ) { // TODO: improve this message - throw new MappingException( "number of values in many-to-one reference is greater than number of values in target" ); + throw new MappingException( + "number of values in many-to-one reference is greater than number of values in target" + ); } SimpleValue referencingValue = referencingValueIterator.next(); SimpleValue targetValue = targetValueIterator.next(); if ( Column.class.isInstance( referencingValue ) ) { - if ( ! Column.class.isInstance( targetValue ) ) { + if ( !Column.class.isInstance( targetValue ) ) { // TODO improve this message throw new MappingException( "referencing value is a column, but target is not a column" ); } @@ -157,7 +154,7 @@ public class ManyToOneAttributeBinding extends SingularAttributeBinding implemen public void validate() { // can't check this until both the domain and relational states are initialized... if ( getCascade() != null && getCascade().indexOf( "delete-orphan" ) >= 0 ) { - if ( ! isLogicalOneToOne ) { + if ( !isLogicalOneToOne ) { throw new MappingException( "many-to-one attribute [" + getAttribute().getName() + "] does not support orphan delete as it is not unique" ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/PluralAttributeBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/PluralAttributeBinding.java index 64ac55a8c5..c34b1e8b50 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/PluralAttributeBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/PluralAttributeBinding.java @@ -31,8 +31,8 @@ import org.jboss.logging.Logger; import org.hibernate.FetchMode; import org.hibernate.internal.CoreMessageLogger; +import org.hibernate.metamodel.binding.state.PluralAttributeBindingState; import org.hibernate.metamodel.relational.Table; -import org.hibernate.metamodel.state.domain.PluralAttributeDomainState; /** * TODO : javadoc @@ -84,12 +84,13 @@ public abstract class PluralAttributeBinding extends AbstractAttributeBinding { collectionElement = new CollectionElement( this ); } - public void initialize(PluralAttributeDomainState state) { + protected void initializeBinding(PluralAttributeBindingState state) { super.initialize( state ); fetchMode = state.getFetchMode(); extraLazy = state.isExtraLazy(); collectionElement = new ElementCollectionElement( this ); - collectionElement.initialize( state.getCollectionElementDomainState() ); + collectionElement.setNodeName( state.getElementNodeName() ); + collectionElement.setTypeName( state.getElementTypeName() ); inverse = state.isInverse(); mutable = state.isMutable(); subselectLoadable = state.isSubselectLoadable(); @@ -118,10 +119,6 @@ public abstract class PluralAttributeBinding extends AbstractAttributeBinding { loaderName = state.getLoaderName(); } - protected boolean isLazyDefault(MappingDefaults defaults) { - return defaults.isDefaultLazy(); - } - @Override public boolean isSimpleValue() { return false; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/SimpleAttributeBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/SimpleAttributeBinding.java index 88f57b94e7..f458f986ec 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/SimpleAttributeBinding.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/SimpleAttributeBinding.java @@ -24,38 +24,43 @@ package org.hibernate.metamodel.binding; import org.hibernate.mapping.PropertyGeneration; -import org.hibernate.metamodel.state.domain.SimpleAttributeDomainState; -import org.hibernate.metamodel.state.relational.ColumnRelationalState; -import org.hibernate.metamodel.state.relational.DerivedValueRelationalState; -import org.hibernate.metamodel.state.relational.TupleRelationalState; +import org.hibernate.metamodel.binding.state.SimpleAttributeBindingState; +import org.hibernate.metamodel.relational.state.ColumnRelationalState; +import org.hibernate.metamodel.relational.state.ValueRelationalState; /** * TODO : javadoc * * @author Steve Ebersole */ -public class SimpleAttributeBinding extends SingularAttributeBinding { +public class SimpleAttributeBinding extends AbstractAttributeBinding implements KeyValueBinding { + private final boolean forceNonNullable; + private final boolean forceUnique; + private boolean insertable; + private boolean updatable; + private boolean keyCascadeDeleteEnabled; + private String unsavedValue; private PropertyGeneration generation; SimpleAttributeBinding(EntityBinding entityBinding, boolean forceNonNullable, boolean forceUnique) { - super( entityBinding, forceNonNullable, forceUnique ); + super( entityBinding ); + this.forceNonNullable = forceNonNullable; + this.forceUnique = forceUnique; } - public final void initialize(SimpleAttributeDomainState state) { + public final SimpleAttributeBinding initialize(SimpleAttributeBindingState state) { super.initialize( state ); - generation = state.getPropertyGeneration(); + insertable = state.isInsertable(); + updatable = state.isUpdatable(); + keyCascadeDeleteEnabled = state.isKeyCascadeDeleteEnabled(); + unsavedValue = state.getUnsavedValue(); + generation = state.getPropertyGeneration() == null ? PropertyGeneration.NEVER : state.getPropertyGeneration(); + return this; } - public void initializeColumnValue(ColumnRelationalState state) { - super.initializeValue( state ); - } - - public void initializeDerivedValue(DerivedValueRelationalState state) { - super.initializeValue( state ); - } - - public void initializeTupleValue(TupleRelationalState state) { - super.initializeValue( state ); + public SimpleAttributeBinding initialize(ValueRelationalState state) { + super.initializeValueRelationalState( state ); + return this; } private boolean isUnique(ColumnRelationalState state) { @@ -67,12 +72,49 @@ public class SimpleAttributeBinding extends SingularAttributeBinding { return true; } - private boolean isPrimaryKey() { - return this == getEntityBinding().getEntityIdentifier().getValueBinding(); + public boolean isInsertable() { + return insertable; + } + + protected void setInsertable(boolean insertable) { + this.insertable = insertable; + } + + public boolean isUpdatable() { + return updatable; + } + + protected void setUpdatable(boolean updatable) { + this.updatable = updatable; + } + + @Override + public boolean isKeyCascadeDeleteEnabled() { + return keyCascadeDeleteEnabled; + } + + public void setKeyCascadeDeleteEnabled(boolean keyCascadeDeleteEnabled) { + this.keyCascadeDeleteEnabled = keyCascadeDeleteEnabled; + } + + @Override + public String getUnsavedValue() { + return unsavedValue; + } + + public void setUnsavedValue(String unsaveValue) { + this.unsavedValue = unsaveValue; + } + + public boolean forceNonNullable() { + return forceNonNullable; + } + + public boolean forceUnique() { + return forceUnique; } public PropertyGeneration getGeneration() { return generation; } - } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/SingularAttributeBinding.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/SingularAttributeBinding.java deleted file mode 100644 index 24afd97c4b..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/SingularAttributeBinding.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2010, Red Hat Inc. or third-party contributors as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. All third-party contributions are - * distributed under license by Red Hat Inc. - * - * This copyrighted material is made available to anyone wishing to use, modify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ -package org.hibernate.metamodel.binding; - -import org.hibernate.metamodel.state.domain.SingularAttributeDomainState; - -/** - * TODO : javadoc - * - * @author Gail Badner - */ -public abstract class SingularAttributeBinding extends AbstractAttributeBinding implements KeyValueBinding { - private final boolean forceNonNullable; - private final boolean forceUnique; - private boolean insertable; - private boolean updateable; - private boolean keyCasadeDeleteEnabled; - private String unsavedValue; - - SingularAttributeBinding(EntityBinding entityBinding, boolean forceNonNullable, boolean forceUnique) { - super( entityBinding ); - this.forceNonNullable = forceNonNullable; - this.forceUnique = forceUnique; - } - - public final void initialize(SingularAttributeDomainState state) { - super.initialize( state ); - insertable = state.isInsertable(); - updateable = state.isUpdateable(); - keyCasadeDeleteEnabled = state.isKeyCasadeDeleteEnabled(); - unsavedValue = state.getUnsavedValue(); - } - - public boolean isInsertable() { - return insertable; - } - - protected void setInsertable(boolean insertable) { - this.insertable = insertable; - } - - public boolean isUpdateable() { - return updateable; - } - - protected void setUpdateable(boolean updateable) { - this.updateable = updateable; - } - - @Override - public boolean isKeyCasadeDeleteEnabled() { - return keyCasadeDeleteEnabled; - } - - public void setKeyCasadeDeleteEnabled(boolean keyCasadeDeleteEnabled) { - this.keyCasadeDeleteEnabled = keyCasadeDeleteEnabled; - } - - @Override - public String getUnsavedValue() { - return unsavedValue; - } - - public void setUnsavedValue(String unsaveValue) { - this.unsavedValue = unsaveValue; - } - - public boolean forceNonNullable() { - return forceNonNullable; - } - - public boolean forceUnique() { - return forceUnique; - } -} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/state/domain/AttributeDomainState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/AttributeBindingState.java similarity index 82% rename from hibernate-core/src/main/java/org/hibernate/metamodel/state/domain/AttributeDomainState.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/AttributeBindingState.java index 0241b8a2e7..845e895f50 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/state/domain/AttributeDomainState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/AttributeBindingState.java @@ -21,25 +21,33 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.state.domain; +package org.hibernate.metamodel.binding.state; import java.util.Map; -import org.hibernate.metamodel.binding.HibernateTypeDescriptor; -import org.hibernate.metamodel.domain.Attribute; import org.hibernate.metamodel.domain.MetaAttribute; /** * @author Gail Badner */ -public interface AttributeDomainState { - HibernateTypeDescriptor getHibernateTypeDescriptor(); - Attribute getAttribute(); +public interface AttributeBindingState { + String getAttributeName(); + + String getTypeName(); + + Map getTypeParameters(); + boolean isLazy(); + String getPropertyAccessorName(); + boolean isAlternateUniqueKey(); + String getCascade(); + boolean isOptimisticLockable(); + String getNodeName(); + Map getMetaAttributes(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/state/domain/SingularAttributeDomainState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/DiscriminatorBindingState.java similarity index 82% rename from hibernate-core/src/main/java/org/hibernate/metamodel/state/domain/SingularAttributeDomainState.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/DiscriminatorBindingState.java index d88724b311..67089b04a5 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/state/domain/SingularAttributeDomainState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/DiscriminatorBindingState.java @@ -21,14 +21,11 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.state.domain; +package org.hibernate.metamodel.binding.state; /** * @author Gail Badner */ -public interface SingularAttributeDomainState extends AttributeDomainState{ - boolean isInsertable(); - boolean isUpdateable(); - boolean isKeyCasadeDeleteEnabled(); - String getUnsavedValue(); +public interface DiscriminatorBindingState extends SimpleAttributeBindingState { + boolean isForced(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/state/domain/ManyToOneAttributeDomainState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/ManyToOneAttributeBindingState.java similarity index 89% rename from hibernate-core/src/main/java/org/hibernate/metamodel/state/domain/ManyToOneAttributeDomainState.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/ManyToOneAttributeBindingState.java index 77bc88d452..7cd420348c 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/state/domain/ManyToOneAttributeDomainState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/ManyToOneAttributeBindingState.java @@ -21,14 +21,17 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.state.domain; +package org.hibernate.metamodel.binding.state; /** * @author Gail Badner */ -public interface ManyToOneAttributeDomainState extends SingularAttributeDomainState { +public interface ManyToOneAttributeBindingState extends SimpleAttributeBindingState { boolean isUnwrapProxy(); + String getReferencedAttributeName(); + String getReferencedEntityName(); + boolean ignoreNotFound(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/state/domain/PluralAttributeDomainState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/PluralAttributeBindingState.java similarity index 78% rename from hibernate-core/src/main/java/org/hibernate/metamodel/state/domain/PluralAttributeDomainState.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/PluralAttributeBindingState.java index 21477d46db..67be920cb5 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/state/domain/PluralAttributeDomainState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/PluralAttributeBindingState.java @@ -21,48 +21,68 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.state.domain; +package org.hibernate.metamodel.binding.state; import java.util.Comparator; import org.hibernate.FetchMode; -import org.hibernate.metamodel.binding.AbstractAttributeBinding; -import org.hibernate.metamodel.binding.CollectionElement; import org.hibernate.metamodel.binding.CustomSQL; -import org.hibernate.metamodel.binding.PluralAttributeBinding; /** - * Created by IntelliJ IDEA. - * User: gbadner - * Date: 5/9/11 - * Time: 4:49 PM - * To change this template use File | Settings | File Templates. + * @author gbadner */ -public interface PluralAttributeDomainState extends AttributeDomainState { +public interface PluralAttributeBindingState extends AttributeBindingState { FetchMode getFetchMode(); + boolean isExtraLazy(); - CollectionElementDomainState getCollectionElementDomainState(); + + String getElementTypeName(); + + String getElementNodeName(); + boolean isInverse(); + boolean isMutable(); + boolean isSubselectLoadable(); + String getCacheConcurrencyStrategy(); + String getCacheRegionName(); + String getOrderBy(); + String getWhere(); + String getReferencedPropertyName(); + boolean isSorted(); + Comparator getComparator(); + String getComparatorClassName(); + boolean isOrphanDelete(); + int getBatchSize(); + boolean isEmbedded(); + boolean isOptimisticLocked(); + Class getCollectionPersisterClass(); + java.util.Map getFilters(); + java.util.Set getSynchronizedTables(); + CustomSQL getCustomSQLInsert(); + CustomSQL getCustomSQLUpdate(); + CustomSQL getCustomSQLDelete(); + CustomSQL getCustomSQLDeleteAll(); + String getLoaderName(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/SimpleAttributeBindingState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/SimpleAttributeBindingState.java new file mode 100644 index 0000000000..92df91297c --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/binding/state/SimpleAttributeBindingState.java @@ -0,0 +1,43 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.binding.state; + +import org.hibernate.mapping.PropertyGeneration; + +/** + * @author Gail Badner + * @todo - We need to get a better split into the states. For example this SimpleAttributeBindingState contains + * state which is only relevant for primary/foreign keys. This should be in a different interface. (HF) + */ +public interface SimpleAttributeBindingState extends AttributeBindingState { + boolean isInsertable(); + + boolean isUpdatable(); + + boolean isKeyCascadeDeleteEnabled(); + + String getUnsavedValue(); + + public PropertyGeneration getPropertyGeneration(); +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/domain/AbstractAttributeContainer.java b/hibernate-core/src/main/java/org/hibernate/metamodel/domain/AbstractAttributeContainer.java index 7cf8c90b08..a043c22ce8 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/domain/AbstractAttributeContainer.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/domain/AbstractAttributeContainer.java @@ -35,11 +35,11 @@ import java.util.Set; * * @author Steve Ebersole */ -public abstract class AbstractAttributeContainer implements AttributeContainer, Hierarchical { +public abstract class AbstractAttributeContainer implements AttributeContainer, Hierarchical { private final String name; private final Hierarchical superType; private LinkedHashSet attributeSet = new LinkedHashSet(); - private HashMap attributeMap = new HashMap(); + private HashMap attributeMap = new HashMap(); public AbstractAttributeContainer(String name, Hierarchical superType) { this.name = name; @@ -108,6 +108,16 @@ public abstract class AbstractAttributeContainer implements AttributeContainer, return attribute; } + @Override + public String toString() { + final StringBuilder sb = new StringBuilder(); + sb.append( "AbstractAttributeContainer" ); + sb.append( "{name='" ).append( name ).append( '\'' ); + sb.append( ", superType=" ).append( superType ); + sb.append( '}' ); + return sb.toString(); + } + protected void addAttribute(Attribute attribute) { // todo : how to best "secure" this? if ( attributeMap.put( attribute.getName(), attribute ) != null ) { @@ -121,15 +131,27 @@ public abstract class AbstractAttributeContainer implements AttributeContainer, public static class SingularAttributeImpl implements SingularAttribute { private final AttributeContainer attributeContainer; private final String name; + private Type type; public SingularAttributeImpl(String name, AttributeContainer attributeContainer) { this.name = name; this.attributeContainer = attributeContainer; } + boolean isTypeResolved() { + return type != null; + } + + void resolveType(Type type) { + if ( type == null ) { + throw new IllegalArgumentException( "Attempt to resolve with null type" ); + } + this.type = type; + } + @Override public Type getSingularAttributeType() { - return null; + return type; } @Override @@ -152,10 +174,11 @@ public abstract class AbstractAttributeContainer implements AttributeContainer, private final AttributeContainer attributeContainer; private final PluralAttributeNature nature; private final String name; + private String nodeName; private Type elementType; - public PluralAttributeImpl(String name, PluralAttributeNature nature, AttributeContainer attributeContainer) { + public PluralAttributeImpl(String name, PluralAttributeNature nature, AttributeContainer attributeContainer) { this.name = name; this.nature = nature; this.attributeContainer = attributeContainer; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/domain/Type.java b/hibernate-core/src/main/java/org/hibernate/metamodel/domain/Type.java index e9a55636ff..75c332f305 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/domain/Type.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/domain/Type.java @@ -42,5 +42,4 @@ public interface Type { * @return persistence type */ public TypeNature getNature(); - } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Column.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Column.java index 1696a57323..80c08d2b01 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Column.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/Column.java @@ -23,10 +23,7 @@ */ package org.hibernate.metamodel.relational; -import java.util.Set; - -import org.hibernate.cfg.NamingStrategy; -import org.hibernate.metamodel.state.relational.ColumnRelationalState; +import org.hibernate.metamodel.relational.state.ColumnRelationalState; /** * Models a physical column diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/state/relational/ColumnRelationalState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/ColumnRelationalState.java similarity index 96% rename from hibernate-core/src/main/java/org/hibernate/metamodel/state/relational/ColumnRelationalState.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/ColumnRelationalState.java index cbf465f2c4..17687d1d92 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/state/relational/ColumnRelationalState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/ColumnRelationalState.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.state.relational; +package org.hibernate.metamodel.relational.state; import java.util.Set; @@ -33,16 +33,28 @@ import org.hibernate.metamodel.relational.Size; */ public interface ColumnRelationalState extends SimpleValueRelationalState { NamingStrategy getNamingStrategy(); + String getExplicitColumnName(); + boolean isUnique(); + Size getSize(); + boolean isNullable(); + String getCheckCondition(); + String getDefault(); + String getSqlType(); + String getCustomWriteFragment(); + String getCustomReadFragment(); + String getComment(); + Set getUniqueKeys(); + Set getIndexes(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/state/relational/DerivedValueRelationalState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/DerivedValueRelationalState.java similarity index 82% rename from hibernate-core/src/main/java/org/hibernate/metamodel/state/relational/DerivedValueRelationalState.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/DerivedValueRelationalState.java index 1a3aba9166..19021f58c3 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/state/relational/DerivedValueRelationalState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/DerivedValueRelationalState.java @@ -21,16 +21,10 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.state.relational; - -import org.hibernate.metamodel.relational.SimpleValue; +package org.hibernate.metamodel.relational.state; /** - * Created by IntelliJ IDEA. - * User: gbadner - * Date: 5/9/11 - * Time: 2:15 PM - * To change this template use File | Settings | File Templates. + * @author Gail Badner */ public interface DerivedValueRelationalState extends SimpleValueRelationalState { String getFormula(); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/state/relational/ManyToOneRelationalState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/ManyToOneRelationalState.java similarity index 90% rename from hibernate-core/src/main/java/org/hibernate/metamodel/state/relational/ManyToOneRelationalState.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/ManyToOneRelationalState.java index f7d4a7401f..d21ded3a34 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/state/relational/ManyToOneRelationalState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/ManyToOneRelationalState.java @@ -21,12 +21,15 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.state.relational; +package org.hibernate.metamodel.relational.state; + +import org.hibernate.metamodel.relational.state.ValueRelationalState; /** * @author Gail Badner */ public interface ManyToOneRelationalState extends ValueRelationalState { boolean isLogicalOneToOne(); + String getForeignKeyName(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/state/relational/SimpleValueRelationalState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/SimpleValueRelationalState.java similarity index 90% rename from hibernate-core/src/main/java/org/hibernate/metamodel/state/relational/SimpleValueRelationalState.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/SimpleValueRelationalState.java index ff38ba96f0..b287c7e936 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/state/relational/SimpleValueRelationalState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/SimpleValueRelationalState.java @@ -21,7 +21,9 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.state.relational; +package org.hibernate.metamodel.relational.state; + +import org.hibernate.metamodel.relational.state.ValueRelationalState; /** * @author Gail Badner diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/state/relational/TupleRelationalState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/TupleRelationalState.java similarity index 96% rename from hibernate-core/src/main/java/org/hibernate/metamodel/state/relational/TupleRelationalState.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/TupleRelationalState.java index f52bc76557..6bec20e59e 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/state/relational/TupleRelationalState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/TupleRelationalState.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.state.relational; +package org.hibernate.metamodel.relational.state; import java.util.List; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/ValueFactory.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/ValueCreator.java similarity index 86% rename from hibernate-core/src/main/java/org/hibernate/metamodel/relational/ValueFactory.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/ValueCreator.java index c0f5129a99..58439d563f 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/ValueFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/ValueCreator.java @@ -21,19 +21,20 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.relational; +package org.hibernate.metamodel.relational.state; import org.hibernate.MappingException; -import org.hibernate.metamodel.state.relational.ColumnRelationalState; -import org.hibernate.metamodel.state.relational.DerivedValueRelationalState; -import org.hibernate.metamodel.state.relational.ValueRelationalState; -import org.hibernate.metamodel.state.relational.SimpleValueRelationalState; -import org.hibernate.metamodel.state.relational.TupleRelationalState; +import org.hibernate.metamodel.relational.Column; +import org.hibernate.metamodel.relational.DerivedValue; +import org.hibernate.metamodel.relational.SimpleValue; +import org.hibernate.metamodel.relational.TableSpecification; +import org.hibernate.metamodel.relational.Tuple; +import org.hibernate.metamodel.relational.Value; /** * @author Gail Badner */ -public class ValueFactory { +public class ValueCreator { public static Column createColumn(TableSpecification table, String attributeName, @@ -58,7 +59,7 @@ public class ValueFactory { return value; } - public static DerivedValue createDerivedValue(TableSpecification table, + public static DerivedValue createDerivedValue(TableSpecification table, DerivedValueRelationalState state) { return table.createDerivedValue( state.getFormula() ); } @@ -68,7 +69,7 @@ public class ValueFactory { SimpleValueRelationalState state, boolean forceNonNullable, boolean forceUnique - ) { + ) { if ( state instanceof ColumnRelationalState ) { ColumnRelationalState columnRelationalState = ColumnRelationalState.class.cast( state ); return createColumn( table, attributeName, columnRelationalState, forceNonNullable, forceUnique ); @@ -87,7 +88,7 @@ public class ValueFactory { boolean forceNonNullable, boolean forceUnique ) { - Tuple tuple = table.createTuple( "[" + attributeName + "]" ); + Tuple tuple = table.createTuple( "[" + attributeName + "]" ); for ( SimpleValueRelationalState valueState : state.getRelationalStates() ) { tuple.addValue( createSimpleValue( table, attributeName, valueState, forceNonNullable, forceUnique ) ); } @@ -101,7 +102,7 @@ public class ValueFactory { boolean forceUnique) { Value value = null; if ( SimpleValueRelationalState.class.isInstance( state ) ) { - value = createSimpleValue( + value = createSimpleValue( table, attributeName, SimpleValueRelationalState.class.cast( state ), diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/state/relational/ValueRelationalState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/ValueRelationalState.java similarity index 95% rename from hibernate-core/src/main/java/org/hibernate/metamodel/state/relational/ValueRelationalState.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/ValueRelationalState.java index 23fbcf4e97..d8fb1ff8bd 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/state/relational/ValueRelationalState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/state/ValueRelationalState.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.state.relational; +package org.hibernate.metamodel.relational.state; /** * @author Gail Badner diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/AnnotationBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/AnnotationBinder.java index 7adc497b8b..56ecb82edd 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/AnnotationBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/AnnotationBinder.java @@ -23,13 +23,15 @@ */ package org.hibernate.metamodel.source.annotations; -import java.util.Iterator; import java.util.Set; import org.jboss.jandex.Index; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.hibernate.metamodel.source.annotations.entity.ConfiguredClass; +import org.hibernate.metamodel.source.annotations.entity.ConfiguredClassHierarchy; +import org.hibernate.metamodel.source.annotations.entity.EntityBinder; import org.hibernate.metamodel.source.annotations.global.FetchProfileBinder; import org.hibernate.metamodel.source.annotations.global.TableBinder; import org.hibernate.metamodel.source.annotations.util.ConfiguredClassHierarchyBuilder; @@ -41,36 +43,37 @@ import org.hibernate.metamodel.source.internal.MetadataImpl; * are added to the annotation index. * * @author Hardy Ferentschik - * @todo On top of the index we probably needs to pass some sort of XMLContext for global configuration data - * @todo The annotation index should really be passed at construction time */ public class AnnotationBinder { private static final Logger log = LoggerFactory.getLogger( AnnotationBinder.class ); private final MetadataImpl metadata; + private final Index index; - public AnnotationBinder(MetadataImpl metadata) { + public AnnotationBinder(MetadataImpl metadata, Index index) { this.metadata = metadata; + this.index = index; } - public void bind(Index annotationIndex) { - preEntityBindings( annotationIndex ); - bindMappedClasses( annotationIndex ); - postEntityBindings( annotationIndex ); + public void bind() { + preEntityBindings(); + bindMappedClasses(); + postEntityBindings(); } /** - * Binds global configuration data prior to entity binding. This includes generators and type definitions - * - * @param annotationIndex the annotation repository/index + * Binds global configuration data prior to entity binding. This includes generators and type definitions. */ - private void preEntityBindings(Index annotationIndex) { - FetchProfileBinder.bind( metadata, annotationIndex ); + private void preEntityBindings() { + FetchProfileBinder.bind( metadata, index ); } - private void bindMappedClasses(Index annotationIndex) { + /** + * Does the actual entity binding (see {@link org.hibernate.metamodel.binding.EntityBinding}. + */ + private void bindMappedClasses() { // need to order our annotated entities into an order we can process Set hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( - annotationIndex, metadata.getServiceRegistry() + index, metadata.getServiceRegistry() ); // now we process each hierarchy one at the time @@ -86,11 +89,9 @@ public class AnnotationBinder { /** * Binds global configuration data post entity binding. This includes mappings which live outside of the configuration for a single * entity or entity hierarchy, for example sequence generators, fetch profiles, etc - * - * @param annotationIndex the annotation repository/index */ - private void postEntityBindings(Index annotationIndex) { - TableBinder.bind( metadata, annotationIndex ); + private void postEntityBindings() { + TableBinder.bind( metadata, index ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/MappedAttribute.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/MappedAttribute.java deleted file mode 100644 index b873512617..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/MappedAttribute.java +++ /dev/null @@ -1,186 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2011, Red Hat Inc. or third-party contributors as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. All third-party contributions are - * distributed under license by Red Hat Inc. - * - * This copyrighted material is made available to anyone wishing to use, modify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ -package org.hibernate.metamodel.source.annotations; - -import java.util.List; -import java.util.Map; -import javax.persistence.DiscriminatorType; - -import org.jboss.jandex.AnnotationInstance; -import org.jboss.jandex.DotName; - -import org.hibernate.AnnotationException; -import org.hibernate.AssertionFailure; -import org.hibernate.metamodel.source.annotations.util.JandexHelper; - - -/** - * Represent a mapped attribute (explicitly or implicitly mapped). Also used for synthetic attributes likes a - * discriminator column. - * - * @author Hardy Ferentschik - */ -public class MappedAttribute implements Comparable { - private final String name; - private final Class type; - private final Map> annotations; - private final ColumnValues columnValues; - private final boolean isId; - private final boolean isVersioned; - private final boolean isDiscriminator; - - static MappedAttribute createMappedAttribute(String name, Class type, Map> annotations) { - return new MappedAttribute( name, type, annotations, false ); - } - - static MappedAttribute createDiscriminatorAttribute(Map> annotations) { - Map> discriminatorAnnotations = JandexHelper.filterAnnotations( - annotations, - JPADotNames.DISCRIMINATOR_COLUMN, - JPADotNames.DISCRIMINATOR_VALUE, - HibernateDotNames.DISCRIMINATOR_FORMULA, - HibernateDotNames.DISCRIMINATOR_OPTIONS - ); - - AnnotationInstance discriminatorOptionsAnnotation = JandexHelper.getSingleAnnotation( - annotations, JPADotNames.DISCRIMINATOR_COLUMN - ); - String name = DiscriminatorColumnValues.DEFAULT_DISCRIMINATOR_COLUMN_NAME; - Class type = String.class; // string is the discriminator default - if ( discriminatorOptionsAnnotation != null ) { - name = discriminatorOptionsAnnotation.value( "name" ).asString(); - - DiscriminatorType discriminatorType = Enum.valueOf( - DiscriminatorType.class, discriminatorOptionsAnnotation.value( "discriminatorType" ).asEnum() - ); - switch ( discriminatorType ) { - case STRING: { - type = String.class; - break; - } - case CHAR: { - type = Character.class; - break; - } - case INTEGER: { - type = Integer.class; - break; - } - default: { - throw new AnnotationException( "Unsupported discriminator type: " + discriminatorType ); - } - } - } - return new MappedAttribute( name, type, discriminatorAnnotations, true ); - } - - private MappedAttribute(String name, Class type, Map> annotations, boolean isDiscriminator) { - this.name = name; - this.type = type; - this.annotations = annotations; - this.isDiscriminator = isDiscriminator; - - AnnotationInstance idAnnotation = JandexHelper.getSingleAnnotation( annotations, JPADotNames.ID ); - isId = idAnnotation != null; - - AnnotationInstance versionAnnotation = JandexHelper.getSingleAnnotation( annotations, JPADotNames.VERSION ); - isVersioned = versionAnnotation != null; - - if ( isDiscriminator ) { - columnValues = new DiscriminatorColumnValues( annotations ); - } - else { - AnnotationInstance columnAnnotation = JandexHelper.getSingleAnnotation( annotations, JPADotNames.COLUMN ); - columnValues = new ColumnValues( columnAnnotation ); - } - - if ( isId ) { - // an id must be unique and cannot be nullable - columnValues.setUnique( true ); - columnValues.setNullable( false ); - } - } - - public final String getName() { - return name; - } - - public final Class getType() { - return type; - } - - public final ColumnValues getColumnValues() { - return columnValues; - } - - public boolean isId() { - return isId; - } - - public boolean isVersioned() { - return isVersioned; - } - - public boolean isDiscriminator() { - return isDiscriminator; - } - - /** - * Returns the annotation with the specified name or {@code null} - * - * @param annotationDotName The annotation to retrieve/check - * - * @return Returns the annotation with the specified name or {@code null}. Note, since these are the - * annotations defined on a single attribute there can never be more than one. - */ - public final AnnotationInstance annotations(DotName annotationDotName) { - if ( annotations.containsKey( annotationDotName ) ) { - List instanceList = annotations.get( annotationDotName ); - if ( instanceList.size() > 1 ) { - throw new AssertionFailure( "There cannot be more than one @" + annotationDotName.toString() + " annotation per mapped attribute" ); - } - return instanceList.get( 0 ); - } - else { - return null; - } - } - - @Override - public int compareTo(MappedAttribute mappedProperty) { - return name.compareTo( mappedProperty.getName() ); - } - - @Override - public String toString() { - final StringBuilder sb = new StringBuilder(); - sb.append( "MappedProperty" ); - sb.append( "{name='" ).append( name ).append( '\'' ); - sb.append( ", type=" ).append( type ); - sb.append( '}' ); - return sb.toString(); - } -} - - diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/state/domain/CollectionElementDomainState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/AssociationAttribute.java similarity index 74% rename from hibernate-core/src/main/java/org/hibernate/metamodel/state/domain/CollectionElementDomainState.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/AssociationAttribute.java index b3dd653c6d..a2bd557a73 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/state/domain/CollectionElementDomainState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/AssociationAttribute.java @@ -21,14 +21,21 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.state.domain; - -import org.hibernate.metamodel.binding.HibernateTypeDescriptor; +package org.hibernate.metamodel.source.annotations.entity; /** - * @author Gail Badner + * @author Hardy Ferentschik */ -public interface CollectionElementDomainState { - HibernateTypeDescriptor getHibernateTypeDescriptor(); - String getNodeName(); +public class AssociationAttribute { + private final MappedAttribute mappedAttribute; + + public AssociationAttribute(MappedAttribute mappedAttribute) { + this.mappedAttribute = mappedAttribute; + } + + public MappedAttribute getMappedAttribute() { + return mappedAttribute; + } } + + diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ColumnValues.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ColumnValues.java similarity index 97% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ColumnValues.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ColumnValues.java index 99b5c0f717..62fa322477 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ColumnValues.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ColumnValues.java @@ -21,12 +21,13 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.annotations; +package org.hibernate.metamodel.source.annotations.entity; import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationValue; import org.hibernate.AssertionFailure; +import org.hibernate.metamodel.source.annotations.JPADotNames; /** * Container for the properties defined by {@code @Column}. diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClass.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClass.java similarity index 98% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClass.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClass.java index 4c022f6376..f032c4533c 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClass.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClass.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.annotations; +package org.hibernate.metamodel.source.annotations.entity; import java.lang.reflect.Field; import java.lang.reflect.Member; @@ -52,6 +52,7 @@ import org.hibernate.AssertionFailure; import org.hibernate.HibernateException; import org.hibernate.MappingException; import org.hibernate.metamodel.binding.InheritanceType; +import org.hibernate.metamodel.source.annotations.JPADotNames; import org.hibernate.metamodel.source.annotations.util.JandexHelper; import org.hibernate.metamodel.source.annotations.util.ReflectionHelper; import org.hibernate.service.ServiceRegistry; @@ -379,7 +380,7 @@ public class ConfiguredClass { final Map> annotations = JandexHelper.getMemberAnnotations( classInfo, member.getName() ); - return MappedAttribute.createMappedAttribute( name, (Class) type, annotations ); + return MappedAttribute.createMappedAttribute( name, ( (Class) type ).getName(), annotations ); } private Type findResolvedType(String name, ResolvedMember[] resolvedMembers) { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchy.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClassHierarchy.java similarity index 97% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchy.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClassHierarchy.java index c2e3630ee7..b5fadf47a9 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchy.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClassHierarchy.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.annotations; +package org.hibernate.metamodel.source.annotations.entity; import java.util.ArrayList; import java.util.Iterator; @@ -36,6 +36,7 @@ import org.jboss.jandex.MethodInfo; import org.hibernate.AnnotationException; import org.hibernate.metamodel.binding.InheritanceType; +import org.hibernate.metamodel.source.annotations.JPADotNames; import org.hibernate.metamodel.source.annotations.util.JandexHelper; import org.hibernate.metamodel.source.annotations.util.ReflectionHelper; import org.hibernate.service.ServiceRegistry; @@ -195,7 +196,7 @@ public class ConfiguredClassHierarchy implements Iterable { private AccessType throwIdNotFoundAnnotationException(List classes) { StringBuilder builder = new StringBuilder(); - builder.append( "Unable to determine identifier attribute for class hierarchy " ); + builder.append( "Unable to determine identifier attribute for class hierarchy consisting of the classe(s) " ); builder.append( hierarchyListString( classes ) ); throw new AnnotationException( builder.toString() ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClassType.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClassType.java similarity index 95% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClassType.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClassType.java index 553646830b..cf7d0207ad 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/ConfiguredClassType.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/ConfiguredClassType.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.annotations; +package org.hibernate.metamodel.source.annotations.entity; /** * @author Hardy Ferentschik diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/DiscriminatorColumnValues.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/DiscriminatorColumnValues.java similarity index 95% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/DiscriminatorColumnValues.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/DiscriminatorColumnValues.java index 0e9f40b66e..7ca84331a4 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/DiscriminatorColumnValues.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/DiscriminatorColumnValues.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.annotations; +package org.hibernate.metamodel.source.annotations.entity; import java.util.List; import java.util.Map; @@ -29,6 +29,8 @@ import java.util.Map; import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.DotName; +import org.hibernate.metamodel.source.annotations.HibernateDotNames; +import org.hibernate.metamodel.source.annotations.JPADotNames; import org.hibernate.metamodel.source.annotations.util.JandexHelper; /** diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/EntityBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/EntityBinder.java similarity index 84% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/EntityBinder.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/EntityBinder.java index e7e5f1f92a..9af7c5c608 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/EntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/EntityBinder.java @@ -21,10 +21,14 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.annotations; +package org.hibernate.metamodel.source.annotations.entity; + +import java.util.List; +import java.util.Map; import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationValue; +import org.jboss.jandex.DotName; import org.hibernate.AnnotationException; import org.hibernate.AssertionFailure; @@ -33,19 +37,22 @@ import org.hibernate.annotations.OptimisticLockType; import org.hibernate.annotations.PolymorphismType; import org.hibernate.cache.spi.RegionFactory; import org.hibernate.cache.spi.access.AccessType; -import org.hibernate.internal.util.StringHelper; import org.hibernate.metamodel.binding.Caching; import org.hibernate.metamodel.binding.EntityBinding; import org.hibernate.metamodel.binding.SimpleAttributeBinding; +import org.hibernate.metamodel.binding.state.SimpleAttributeBindingState; import org.hibernate.metamodel.domain.Entity; import org.hibernate.metamodel.domain.Hierarchical; import org.hibernate.metamodel.relational.Identifier; import org.hibernate.metamodel.relational.Schema; -import org.hibernate.metamodel.source.annotations.state.domain.AttributeDomainState; -import org.hibernate.metamodel.source.annotations.state.relational.AttributeColumnRelationalState; -import org.hibernate.metamodel.source.annotations.state.relational.AttributeTupleRelationalState; +import org.hibernate.metamodel.source.annotations.HibernateDotNames; +import org.hibernate.metamodel.source.annotations.JPADotNames; +import org.hibernate.metamodel.source.annotations.entity.state.binding.AttributeBindingStateImpl; +import org.hibernate.metamodel.source.annotations.entity.state.binding.DiscriminatorBindingStateImpl; +import org.hibernate.metamodel.source.annotations.entity.state.relational.ColumnRelationalStateImpl; +import org.hibernate.metamodel.source.annotations.entity.state.relational.TupleRelationalStateImpl; import org.hibernate.metamodel.source.annotations.util.JandexHelper; -import org.hibernate.metamodel.source.internal.MetadataImpl; +import org.hibernate.metamodel.source.spi.MetadataImplementor; import org.hibernate.service.classloading.spi.ClassLoaderService; /** @@ -55,11 +62,11 @@ import org.hibernate.service.classloading.spi.ClassLoaderService; */ public class EntityBinder { private final ConfiguredClass configuredClass; - private final MetadataImpl meta; + private final MetadataImplementor meta; private Schema.Name schemaName; - public EntityBinder(MetadataImpl metadata, ConfiguredClass configuredClass) { + public EntityBinder(MetadataImplementor metadata, ConfiguredClass configuredClass) { this.configuredClass = configuredClass; this.meta = metadata; } @@ -81,11 +88,13 @@ public class EntityBinder { bindJpaCaching( entityBinding ); bindHibernateCaching( entityBinding ); + // take care of the id, attributes and relations if ( configuredClass.isRoot() ) { bindId( entityBinding ); } bindAttributes( entityBinding ); + // last, but not least we register the new EntityBinding with the metadata meta.addEntity( entityBinding ); } @@ -110,18 +119,19 @@ public class EntityBinder { } private void bindDiscriminatorColumn(EntityBinding entityBinding) { - MappedAttribute discriminatorAttribute = MappedAttribute.createDiscriminatorAttribute( - configuredClass.getClassInfo().annotations() + final Map> typeAnnotations = JandexHelper.getTypeAnnotations( + configuredClass.getClassInfo() ); + MappedAttribute discriminatorAttribute = MappedAttribute.createDiscriminatorAttribute( typeAnnotations ); bindSingleMappedAttribute( entityBinding, discriminatorAttribute ); if ( !( discriminatorAttribute.getColumnValues() instanceof DiscriminatorColumnValues ) ) { throw new AssertionFailure( "Expected discriminator column values" ); } + + // TODO: move this into DiscriminatorBindingState DiscriminatorColumnValues discriminatorColumnvalues = (DiscriminatorColumnValues) discriminatorAttribute.getColumnValues(); - entityBinding.getEntityDiscriminator().setForced( discriminatorColumnvalues.isForced() ); - entityBinding.getEntityDiscriminator().setInserted( discriminatorColumnvalues.isIncludedInSql() ); entityBinding.setDiscriminatorValue( discriminatorColumnvalues.getDiscriminatorValue() ); } @@ -186,7 +196,7 @@ public class EntityBinder { } Caching caching = null; - switch ( meta.getSharedCacheMode() ) { + switch ( meta.getOptions().getSharedCacheMode() ) { case ALL: { caching = createCachingForCacheableAnnotation( entityBinding ); break; @@ -280,7 +290,7 @@ public class EntityBinder { ); String name; if ( jpaEntityAnnotation.value( "name" ) == null ) { - name = StringHelper.unqualify( configuredClass.getName() ); + name = configuredClass.getName(); } else { name = jpaEntityAnnotation.value( "name" ).asString(); @@ -294,18 +304,13 @@ public class EntityBinder { ); String idName = JandexHelper.getPropertyName( idAnnotation.target() ); - entityBinding.getEntity().getOrCreateSingularAttribute( idName ); - SimpleAttributeBinding idBinding = entityBinding.makeSimplePrimaryKeyAttributeBinding( idName ); - MappedAttribute idAttribute = configuredClass.getMappedProperty( idName ); - AttributeDomainState domainState = new AttributeDomainState( entityBinding, idAttribute ); - idBinding.initialize( domainState ); + entityBinding.getEntity().getOrCreateSingularAttribute( idName ); - AttributeColumnRelationalState columnRelationsState = new AttributeColumnRelationalState( idAttribute, meta ); - AttributeTupleRelationalState relationalState = new AttributeTupleRelationalState(); - relationalState.addValueState( columnRelationsState ); - idBinding.initializeTupleValue( relationalState ); + SimpleAttributeBinding attributeBinding = entityBinding.makeSimpleIdAttributeBinding( idName ); + attributeBinding.initialize( new AttributeBindingStateImpl( idAttribute ) ); + attributeBinding.initialize( new ColumnRelationalStateImpl( idAttribute, meta ) ); } private void bindAttributes(EntityBinding entityBinding) { @@ -322,27 +327,32 @@ public class EntityBinder { String attributeName = mappedAttribute.getName(); entityBinding.getEntity().getOrCreateSingularAttribute( attributeName ); SimpleAttributeBinding attributeBinding; + SimpleAttributeBindingState bindingState; - if ( mappedAttribute.isVersioned() ) { - attributeBinding = entityBinding.makeVersionBinding( attributeName ); + if ( mappedAttribute.isDiscriminator() ) { + attributeBinding = entityBinding.makeEntityDiscriminator( attributeName ).getValueBinding(); + bindingState = new DiscriminatorBindingStateImpl( mappedAttribute ); } - else if ( mappedAttribute.isDiscriminator() ) { - attributeBinding = entityBinding.makeEntityDiscriminatorBinding( attributeName ); + else if ( mappedAttribute.isVersioned() ) { + attributeBinding = entityBinding.makeVersionBinding( attributeName ); + bindingState = new AttributeBindingStateImpl( mappedAttribute ); } else { attributeBinding = entityBinding.makeSimpleAttributeBinding( attributeName ); + bindingState = new AttributeBindingStateImpl( mappedAttribute ); } - - AttributeDomainState domainState = new AttributeDomainState( entityBinding, mappedAttribute ); - attributeBinding.initialize( domainState ); + attributeBinding.initialize( bindingState ); if ( configuredClass.hasOwnTable() ) { - AttributeColumnRelationalState columnRelationsState = new AttributeColumnRelationalState( + ColumnRelationalStateImpl columnRelationsState = new ColumnRelationalStateImpl( mappedAttribute, meta ); - AttributeTupleRelationalState relationalState = new AttributeTupleRelationalState(); + TupleRelationalStateImpl relationalState = new TupleRelationalStateImpl(); relationalState.addValueState( columnRelationsState ); - attributeBinding.initializeTupleValue( relationalState ); + + // TODO: if this really just binds a column, then it can be changed to + // attributeBinding.initialize( columnRelationsState ); + attributeBinding.initialize( relationalState ); } } @@ -417,7 +427,7 @@ public class EntityBinder { return null; } - EntityBinding parentBinding = meta.getEntityBinding( parent.getSimpleName() ); + EntityBinding parentBinding = meta.getEntityBinding( parent.getName() ); if ( parentBinding == null ) { throw new AssertionFailure( "Parent entity " + parent.getName() + " of entity " + configuredClass.getName() + " not yet created!" diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/IdType.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/IdType.java similarity index 95% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/IdType.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/IdType.java index edd72e0d12..164e756af7 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/IdType.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/IdType.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.annotations; +package org.hibernate.metamodel.source.annotations.entity; /** * An emum for the type of id configuration for an entity. diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/MappedAttribute.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/MappedAttribute.java new file mode 100644 index 0000000000..51fa9245c5 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/MappedAttribute.java @@ -0,0 +1,347 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.source.annotations.entity; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.persistence.DiscriminatorType; +import javax.persistence.FetchType; + +import org.jboss.jandex.AnnotationInstance; +import org.jboss.jandex.AnnotationValue; +import org.jboss.jandex.DotName; + +import org.hibernate.AnnotationException; +import org.hibernate.AssertionFailure; +import org.hibernate.annotations.GenerationTime; +import org.hibernate.mapping.PropertyGeneration; +import org.hibernate.metamodel.source.annotations.HibernateDotNames; +import org.hibernate.metamodel.source.annotations.JPADotNames; +import org.hibernate.metamodel.source.annotations.util.JandexHelper; + +/** + * Represent a mapped attribute (explicitly or implicitly mapped). Also used for synthetic attributes like a + * discriminator column. + * + * @author Hardy Ferentschik + */ +public class MappedAttribute implements Comparable { + /** + * Annotations defined on the attribute, keyed against the annotation dot name. + */ + private final Map> annotations; + + /** + * The property name. + */ + private final String name; + + /** + * The property type as string. + */ + private final String type; + + /** + * Optional type parameters for custom types. + */ + private final Map typeParameters; + + /** + * Is this property an id property (or part thereof). + */ + private final boolean isId; + + /** + * Is this a versioned property (annotated w/ {@code @Version}. + */ + private final boolean isVersioned; + + /** + * Is this property a discriminator property. + */ + private final boolean isDiscriminator; + + /** + * Whether a change of the property's value triggers a version increment of the entity (in case of optimistic + * locking). + */ + private final boolean isOptimisticLockable; + + /** + * Is this property lazy loaded (see {@link javax.persistence.Basic}). + */ + private boolean isLazy = false; + + /** + * Is this property optional (see {@link javax.persistence.Basic}). + */ + private boolean isOptional = true; + + private PropertyGeneration propertyGeneration; + private boolean isInsertable = true; + private boolean isUpdatable = true; + + /** + * Defines the column values (relational values) for this property. + */ + private final ColumnValues columnValues; + + static MappedAttribute createMappedAttribute(String name, String type, Map> annotations) { + return new MappedAttribute( name, type, annotations, false ); + } + + static MappedAttribute createDiscriminatorAttribute(Map> annotations) { + AnnotationInstance discriminatorOptionsAnnotation = JandexHelper.getSingleAnnotation( + annotations, JPADotNames.DISCRIMINATOR_COLUMN + ); + String name = DiscriminatorColumnValues.DEFAULT_DISCRIMINATOR_COLUMN_NAME; + String type = String.class.toString(); // string is the discriminator default + if ( discriminatorOptionsAnnotation != null ) { + name = discriminatorOptionsAnnotation.value( "name" ).asString(); + + DiscriminatorType discriminatorType = Enum.valueOf( + DiscriminatorType.class, discriminatorOptionsAnnotation.value( "discriminatorType" ).asEnum() + ); + switch ( discriminatorType ) { + case STRING: { + type = String.class.toString(); + break; + } + case CHAR: { + type = Character.class.toString(); + break; + } + case INTEGER: { + type = Integer.class.toString(); + break; + } + default: { + throw new AnnotationException( "Unsupported discriminator type: " + discriminatorType ); + } + } + } + return new MappedAttribute( name, type, annotations, true ); + } + + private MappedAttribute(String name, String type, Map> annotations, boolean isDiscriminator) { + this.name = name; + this.annotations = annotations; + this.isDiscriminator = isDiscriminator; + + this.typeParameters = new HashMap(); + this.type = determineType( type, typeParameters ); + + AnnotationInstance idAnnotation = JandexHelper.getSingleAnnotation( annotations, JPADotNames.ID ); + isId = idAnnotation != null; + + AnnotationInstance versionAnnotation = JandexHelper.getSingleAnnotation( annotations, JPADotNames.VERSION ); + isVersioned = versionAnnotation != null; + + if ( isDiscriminator ) { + columnValues = new DiscriminatorColumnValues( annotations ); + } + else { + AnnotationInstance columnAnnotation = JandexHelper.getSingleAnnotation( annotations, JPADotNames.COLUMN ); + columnValues = new ColumnValues( columnAnnotation ); + } + + if ( isId ) { + // an id must be unique and cannot be nullable + columnValues.setUnique( true ); + columnValues.setNullable( false ); + } + + this.isOptimisticLockable = checkOptimisticLockAnnotation(); + + checkBasicAnnotation(); + checkGeneratedAnnotation(); + } + + public final String getName() { + return name; + } + + public final String getType() { + return type; + } + + public final ColumnValues getColumnValues() { + return columnValues; + } + + public Map getTypeParameters() { + return typeParameters; + } + + public boolean isId() { + return isId; + } + + public boolean isVersioned() { + return isVersioned; + } + + public boolean isDiscriminator() { + return isDiscriminator; + } + + public boolean isLazy() { + return isLazy; + } + + public boolean isOptional() { + return isOptional; + } + + public boolean isInsertable() { + return isInsertable; + } + + public boolean isUpdatable() { + return isUpdatable; + } + + public PropertyGeneration getPropertyGeneration() { + return propertyGeneration; + } + + public boolean isOptimisticLockable() { + return isOptimisticLockable; + } + + /** + * Returns the annotation with the specified name or {@code null} + * + * @param annotationDotName The annotation to retrieve/check + * + * @return Returns the annotation with the specified name or {@code null}. Note, since these are the + * annotations defined on a single attribute there can never be more than one. + */ + public final AnnotationInstance getIfExists(DotName annotationDotName) { + if ( annotations.containsKey( annotationDotName ) ) { + List instanceList = annotations.get( annotationDotName ); + if ( instanceList.size() > 1 ) { + throw new AssertionFailure( "There cannot be more than one @" + annotationDotName.toString() + " annotation per mapped attribute" ); + } + return instanceList.get( 0 ); + } + else { + return null; + } + } + + @Override + public int compareTo(MappedAttribute mappedProperty) { + return name.compareTo( mappedProperty.getName() ); + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder(); + sb.append( "MappedAttribute" ); + sb.append( "{name='" ).append( name ).append( '\'' ); + sb.append( ", type='" ).append( type ).append( '\'' ); + sb.append( ", isId=" ).append( isId ); + sb.append( ", isVersioned=" ).append( isVersioned ); + sb.append( ", isDiscriminator=" ).append( isDiscriminator ); + sb.append( '}' ); + return sb.toString(); + } + + /** + * We need to check whether the is an explicit type specified via {@link org.hibernate.annotations.Type}. + * + * @param type the type specified via the constructor + * @param typeParameters map for type parameters in case there are any + * + * @return the final type for this mapped attribute + */ + private String determineType(String type, Map typeParameters) { + AnnotationInstance typeAnnotation = getIfExists( HibernateDotNames.TYPE ); + if ( typeAnnotation == null ) { + // return discovered type + return type; + } + + AnnotationValue parameterAnnotationValue = typeAnnotation.value( "parameters" ); + if ( parameterAnnotationValue != null ) { + AnnotationInstance[] parameterAnnotations = parameterAnnotationValue.asNestedArray(); + for ( AnnotationInstance parameterAnnotationInstance : parameterAnnotations ) { + typeParameters.put( + parameterAnnotationInstance.value( "name" ).asString(), + parameterAnnotationInstance.value( "value" ).asString() + ); + } + } + + return typeAnnotation.value( "type" ).asString(); + } + + private boolean checkOptimisticLockAnnotation() { + boolean triggersVersionIncrement = true; + AnnotationInstance optimisticLockAnnotation = getIfExists( HibernateDotNames.OPTIMISTIC_LOCK ); + if ( optimisticLockAnnotation != null ) { + boolean exclude = optimisticLockAnnotation.value( "excluded" ).asBoolean(); + triggersVersionIncrement = !exclude; + } + return triggersVersionIncrement; + } + + private void checkBasicAnnotation() { + AnnotationInstance basicAnnotation = getIfExists( JPADotNames.BASIC ); + if ( basicAnnotation != null ) { + FetchType fetchType = FetchType.LAZY; + AnnotationValue fetchValue = basicAnnotation.value( "fetch" ); + if ( fetchValue != null ) { + fetchType = Enum.valueOf( FetchType.class, fetchValue.asEnum() ); + } + this.isLazy = fetchType == FetchType.LAZY; + + AnnotationValue optionalValue = basicAnnotation.value( "optional" ); + if ( optionalValue != null ) { + this.isOptional = optionalValue.asBoolean(); + } + } + } + + // TODO - there is more todo for updatable and insertable. Checking the @Generated annotation is only one part (HF) + private void checkGeneratedAnnotation() { + AnnotationInstance generatedAnnotation = getIfExists( HibernateDotNames.GENERATED ); + if ( generatedAnnotation != null ) { + this.isInsertable = false; + + AnnotationValue generationTimeValue = generatedAnnotation.value(); + if ( generationTimeValue != null ) { + GenerationTime genTime = Enum.valueOf( GenerationTime.class, generationTimeValue.asEnum() ); + if ( GenerationTime.ALWAYS.equals( genTime ) ) { + this.isUpdatable = false; + this.propertyGeneration = PropertyGeneration.parse( genTime.toString().toLowerCase() ); + } + } + } + } +} + + diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/package-info.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/package-info.java new file mode 100644 index 0000000000..3bdf584ff3 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/package-info.java @@ -0,0 +1,30 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.source.annotations.entity; + +/** + * This package contains binding code for entities. In particular it contains classes like {@code ConfiguredClass} and + * {@code MappedAttribute} which are populated from annotations to make it easier to bind to the Hibernate metamodel. + * The configured classes (entities) are also ordered in a ways that they can be bound in the right order. + */ \ No newline at end of file diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/binding/AttributeBindingStateImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/binding/AttributeBindingStateImpl.java new file mode 100644 index 0000000000..fc3de5a155 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/binding/AttributeBindingStateImpl.java @@ -0,0 +1,122 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.source.annotations.entity.state.binding; + +import java.util.Map; + +import org.hibernate.mapping.PropertyGeneration; +import org.hibernate.metamodel.binding.state.SimpleAttributeBindingState; +import org.hibernate.metamodel.domain.MetaAttribute; +import org.hibernate.metamodel.source.annotations.entity.MappedAttribute; + +/** + * Implementation of the attribute binding state via annotation configuration. + * + * @author Hardy Ferentschik + * @todo in the end we can maybe just let MappedAttribute implement SimpleAttributeBindingState. (HF) + */ +public class AttributeBindingStateImpl implements SimpleAttributeBindingState { + private final MappedAttribute mappedAttribute; + + public AttributeBindingStateImpl(MappedAttribute mappedAttribute) { + this.mappedAttribute = mappedAttribute; + } + + @Override + public String getAttributeName() { + return mappedAttribute.getName(); + } + + @Override + public PropertyGeneration getPropertyGeneration() { + return mappedAttribute.getPropertyGeneration(); + } + + @Override + public boolean isInsertable() { + return mappedAttribute.isInsertable(); + } + + @Override + public boolean isUpdatable() { + return mappedAttribute.isUpdatable(); + } + + @Override + public String getTypeName() { + return mappedAttribute.getType(); + } + + @Override + public Map getTypeParameters() { + return mappedAttribute.getTypeParameters(); + } + + @Override + public boolean isLazy() { + return mappedAttribute.isLazy(); + } + + @Override + public boolean isOptimisticLockable() { + return mappedAttribute.isOptimisticLockable(); + } + + @Override + public boolean isKeyCascadeDeleteEnabled() { + return false; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public String getUnsavedValue() { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public String getPropertyAccessorName() { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public boolean isAlternateUniqueKey() { + return false; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public String getCascade() { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public String getNodeName() { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public Map getMetaAttributes() { + return null; //To change body of implemented methods use File | Settings | File Templates. + } +} + + diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/domain/HbmCollectionElementDomainState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/binding/DiscriminatorBindingStateImpl.java similarity index 53% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/domain/HbmCollectionElementDomainState.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/binding/DiscriminatorBindingStateImpl.java index 539d23e9e0..64ab54b000 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/domain/HbmCollectionElementDomainState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/binding/DiscriminatorBindingStateImpl.java @@ -21,30 +21,35 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.hbm.state.domain; +package org.hibernate.metamodel.source.annotations.entity.state.binding; -import org.hibernate.metamodel.binding.CollectionElement; -import org.hibernate.metamodel.binding.HibernateTypeDescriptor; -import org.hibernate.metamodel.source.hbm.xml.mapping.XMLElementElement; -import org.hibernate.metamodel.state.domain.CollectionElementDomainState; +import org.hibernate.metamodel.source.annotations.entity.DiscriminatorColumnValues; +import org.hibernate.metamodel.source.annotations.entity.MappedAttribute; /** * @author Gail Badner + * + * TODO: extract a superclass that sets defaults for other stuff */ -public class HbmCollectionElementDomainState implements CollectionElementDomainState { - private final XMLElementElement element; +public class DiscriminatorBindingStateImpl + extends AttributeBindingStateImpl implements org.hibernate.metamodel.binding.state.DiscriminatorBindingState { + private final boolean isForced; + private final boolean isInserted; - HbmCollectionElementDomainState(XMLElementElement element) { - this.element = element; + public DiscriminatorBindingStateImpl(MappedAttribute mappedAttribute) { + super( mappedAttribute ); + DiscriminatorColumnValues columnValues = DiscriminatorColumnValues.class.cast( mappedAttribute.getColumnValues() ); + isForced = columnValues.isForced(); + isInserted = columnValues.isIncludedInSql(); } - public final HibernateTypeDescriptor getHibernateTypeDescriptor() { - HibernateTypeDescriptor hibernateTypeDescriptor = new HibernateTypeDescriptor(); - hibernateTypeDescriptor.setTypeName( element.getTypeAttribute() ); - return hibernateTypeDescriptor; + @Override + public boolean isForced() { + return isForced; } - public final String getNodeName() { - return element.getNode(); + @Override + public boolean isInsertable() { + return isInserted; } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/binding/ManyToOneBindingStateImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/binding/ManyToOneBindingStateImpl.java new file mode 100644 index 0000000000..5d981f89a8 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/binding/ManyToOneBindingStateImpl.java @@ -0,0 +1,58 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.source.annotations.entity.state.binding; + +import org.hibernate.metamodel.binding.state.ManyToOneAttributeBindingState; +import org.hibernate.metamodel.source.annotations.entity.AssociationAttribute; + +/** + * @author Hardy Ferentschik + */ +public class ManyToOneBindingStateImpl extends AttributeBindingStateImpl implements ManyToOneAttributeBindingState { + public ManyToOneBindingStateImpl(AssociationAttribute associationAttribute) { + super( associationAttribute.getMappedAttribute() ); + } + + @Override + public boolean isUnwrapProxy() { + return false; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public String getReferencedAttributeName() { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public String getReferencedEntityName() { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public boolean ignoreNotFound() { + return false; //To change body of implemented methods use File | Settings | File Templates. + } +} + + diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/state/relational/AttributeColumnRelationalState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/relational/ColumnRelationalStateImpl.java similarity index 86% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/state/relational/AttributeColumnRelationalState.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/relational/ColumnRelationalStateImpl.java index 1a86766c44..fa6aee40dc 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/state/relational/AttributeColumnRelationalState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/relational/ColumnRelationalStateImpl.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.annotations.state.relational; +package org.hibernate.metamodel.source.annotations.entity.state.relational; import java.util.ArrayList; import java.util.Arrays; @@ -33,18 +33,17 @@ import org.jboss.jandex.AnnotationInstance; import org.hibernate.AnnotationException; import org.hibernate.cfg.NamingStrategy; -import org.hibernate.metamodel.relational.Column; import org.hibernate.metamodel.relational.Size; -import org.hibernate.metamodel.source.annotations.ColumnValues; +import org.hibernate.metamodel.relational.state.ColumnRelationalState; import org.hibernate.metamodel.source.annotations.HibernateDotNames; -import org.hibernate.metamodel.source.annotations.MappedAttribute; -import org.hibernate.metamodel.source.internal.MetadataImpl; -import org.hibernate.metamodel.state.relational.ColumnRelationalState; +import org.hibernate.metamodel.source.annotations.entity.ColumnValues; +import org.hibernate.metamodel.source.annotations.entity.MappedAttribute; +import org.hibernate.metamodel.source.spi.MetadataImplementor; /** * @author Hardy Ferentschik */ -public class AttributeColumnRelationalState implements ColumnRelationalState { +public class ColumnRelationalStateImpl implements ColumnRelationalState { private final NamingStrategy namingStrategy; private final String columnName; private final boolean unique; @@ -62,9 +61,9 @@ public class AttributeColumnRelationalState implements ColumnRelationalState { private Set uniqueKeys = new HashSet(); - public AttributeColumnRelationalState(MappedAttribute attribute, MetadataImpl meta) { + public ColumnRelationalStateImpl(MappedAttribute attribute, MetadataImplementor meta) { ColumnValues columnValues = attribute.getColumnValues(); - namingStrategy = meta.getNamingStrategy(); + namingStrategy = meta.getOptions().getNamingStrategy(); columnName = columnValues.getName().isEmpty() ? attribute.getName() : columnValues.getName(); unique = columnValues.isUnique(); nullable = columnValues.isNullable(); @@ -156,13 +155,13 @@ public class AttributeColumnRelationalState implements ColumnRelationalState { List allColumnTransformerAnnotations = new ArrayList(); // not quite sure about the usefulness of @ColumnTransformers (HF) - AnnotationInstance columnTransformersAnnotations = attribute.annotations( HibernateDotNames.COLUMN_TRANSFORMERS ); + AnnotationInstance columnTransformersAnnotations = attribute.getIfExists( HibernateDotNames.COLUMN_TRANSFORMERS ); if ( columnTransformersAnnotations != null ) { AnnotationInstance[] annotationInstances = allColumnTransformerAnnotations.get( 0 ).value().asNestedArray(); allColumnTransformerAnnotations.addAll( Arrays.asList( annotationInstances ) ); } - AnnotationInstance columnTransformerAnnotation = attribute.annotations( HibernateDotNames.COLUMN_TRANSFORMER ); + AnnotationInstance columnTransformerAnnotation = attribute.getIfExists( HibernateDotNames.COLUMN_TRANSFORMER ); if ( columnTransformerAnnotation != null ) { allColumnTransformerAnnotations.add( columnTransformerAnnotation ); } @@ -197,7 +196,7 @@ public class AttributeColumnRelationalState implements ColumnRelationalState { private String parseCheckAnnotation(MappedAttribute attribute) { String checkCondition = null; - AnnotationInstance checkAnnotation = attribute.annotations( HibernateDotNames.CHECK ); + AnnotationInstance checkAnnotation = attribute.getIfExists( HibernateDotNames.CHECK ); if ( checkAnnotation != null ) { checkCondition = checkAnnotation.value( "constraints" ).toString(); } @@ -206,7 +205,7 @@ public class AttributeColumnRelationalState implements ColumnRelationalState { private Set parseIndexAnnotation(MappedAttribute attribute) { Set indexNames = new HashSet(); - AnnotationInstance indexAnnotation = attribute.annotations( HibernateDotNames.INDEX ); + AnnotationInstance indexAnnotation = attribute.getIfExists( HibernateDotNames.INDEX ); if ( indexAnnotation != null ) { String indexName = indexAnnotation.value( "name" ).toString(); indexNames.add( indexName ); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/relational/ManyToOneRelationalStateImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/relational/ManyToOneRelationalStateImpl.java new file mode 100644 index 0000000000..370c4f251a --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/relational/ManyToOneRelationalStateImpl.java @@ -0,0 +1,20 @@ +package org.hibernate.metamodel.source.annotations.entity.state.relational; + +import org.hibernate.metamodel.relational.state.ManyToOneRelationalState; + +/** + * @author Hardy Ferentschik + */ +public class ManyToOneRelationalStateImpl extends TupleRelationalStateImpl implements ManyToOneRelationalState { + @Override + public boolean isLogicalOneToOne() { + return false; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public String getForeignKeyName() { + return null; //To change body of implemented methods use File | Settings | File Templates. + } +} + + diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/state/relational/AttributeTupleRelationalState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/relational/TupleRelationalStateImpl.java similarity index 83% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/state/relational/AttributeTupleRelationalState.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/relational/TupleRelationalStateImpl.java index 946bca056c..111f6d7129 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/state/relational/AttributeTupleRelationalState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/entity/state/relational/TupleRelationalStateImpl.java @@ -21,18 +21,18 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.annotations.state.relational; +package org.hibernate.metamodel.source.annotations.entity.state.relational; import java.util.ArrayList; import java.util.List; -import org.hibernate.metamodel.state.relational.SimpleValueRelationalState; -import org.hibernate.metamodel.state.relational.TupleRelationalState; +import org.hibernate.metamodel.relational.state.SimpleValueRelationalState; +import org.hibernate.metamodel.relational.state.TupleRelationalState; /** * @author Hardy Ferentschik */ -public class AttributeTupleRelationalState implements TupleRelationalState { +public class TupleRelationalStateImpl implements TupleRelationalState { List valueStates = new ArrayList(); public void addValueState(SimpleValueRelationalState state) { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/package-info.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/package-info.java index 15b17fa268..b8cb2f04a7 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/package-info.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/package-info.java @@ -24,5 +24,5 @@ package org.hibernate.metamodel.source.annotations; /** - * This package contains the core binding code for binding annotation based configuration to the Hibernate metadata model. + * This package contains the binding code for binding annotation based configuration to the Hibernate metamodel. */ \ No newline at end of file diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/state/domain/AttributeDomainState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/state/domain/AttributeDomainState.java deleted file mode 100644 index 955e07db4f..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/state/domain/AttributeDomainState.java +++ /dev/null @@ -1,109 +0,0 @@ -package org.hibernate.metamodel.source.annotations.state.domain; - -import java.util.Map; - -import org.hibernate.mapping.PropertyGeneration; -import org.hibernate.metamodel.binding.EntityBinding; -import org.hibernate.metamodel.binding.HibernateTypeDescriptor; -import org.hibernate.metamodel.domain.Attribute; -import org.hibernate.metamodel.domain.Entity; -import org.hibernate.metamodel.domain.MetaAttribute; -import org.hibernate.metamodel.state.domain.SimpleAttributeDomainState; -import org.hibernate.metamodel.source.annotations.MappedAttribute; - -/** - * @author Hardy Ferentschik - */ -public class AttributeDomainState implements SimpleAttributeDomainState { - private final PropertyGeneration propertyGeneration = null; - private final HibernateTypeDescriptor typeDescriptor; - private final Attribute attribute; - - public AttributeDomainState(EntityBinding entityBinding, MappedAttribute mappedAttribute) { - typeDescriptor = new HibernateTypeDescriptor(); - typeDescriptor.setTypeName( mappedAttribute.getType().getName() ); - - Entity entity = entityBinding.getEntity(); - attribute = entity.getOrCreateSingularAttribute( mappedAttribute.getName() ); - } - - @Override - public PropertyGeneration getPropertyGeneration() { - -// GeneratedValue generatedValue = property.getAnnotation( GeneratedValue.class ); -// String generatorType = generatedValue != null ? -// generatorType( generatedValue.strategy(), mappings ) : -// "assigned"; -// String generatorName = generatedValue != null ? -// generatedValue.generator() : -// BinderHelper.ANNOTATION_STRING_DEFAULT; - return propertyGeneration; - } - - @Override - public boolean isInsertable() { - return false; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public boolean isUpdateable() { - return false; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public boolean isKeyCasadeDeleteEnabled() { - return false; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public String getUnsavedValue() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public HibernateTypeDescriptor getHibernateTypeDescriptor() { - return typeDescriptor; - } - - @Override - public Attribute getAttribute() { - return attribute; - } - - @Override - public boolean isLazy() { - return false; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public String getPropertyAccessorName() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public boolean isAlternateUniqueKey() { - return false; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public String getCascade() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public boolean isOptimisticLockable() { - return false; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public String getNodeName() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } - - @Override - public Map getMetaAttributes() { - return null; //To change body of implemented methods use File | Settings | File Templates. - } -} - - diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/ConfiguredClassHierarchyBuilder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/ConfiguredClassHierarchyBuilder.java index d227b8bb56..41d07be3cd 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/ConfiguredClassHierarchyBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/ConfiguredClassHierarchyBuilder.java @@ -36,8 +36,9 @@ import org.jboss.jandex.DotName; import org.jboss.jandex.Index; import org.hibernate.AnnotationException; -import org.hibernate.metamodel.source.annotations.ConfiguredClassHierarchy; +import org.hibernate.metamodel.source.annotations.entity.ConfiguredClassHierarchy; import org.hibernate.metamodel.source.annotations.JPADotNames; +import org.hibernate.metamodel.source.annotations.entity.ConfiguredClassHierarchy; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.classloading.spi.ClassLoaderService; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/JandexHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/JandexHelper.java index cce9fcb460..0b1d9e6e54 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/JandexHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/util/JandexHelper.java @@ -126,20 +126,6 @@ public class JandexHelper { } } - /** - * @param annotations List of annotation instances keyed against their dot name. - * @param annotationNames the annotation names to filter - * - * @return a new map of annotation instances only containing annotations of the specified dot names. - */ - public static Map> filterAnnotations(Map> annotations, DotName... annotationNames) { - Map> filteredAnnotations = new HashMap>(); - for ( DotName name : annotationNames ) { - filteredAnnotations.put( name, annotations.get( name ) ); - } - return filteredAnnotations; - } - /** * Creates a jandex index for the specified classes * @@ -202,6 +188,22 @@ public class JandexHelper { return annotations; } + public static Map> getTypeAnnotations(ClassInfo classInfo) { + if ( classInfo == null ) { + throw new IllegalArgumentException( "classInfo cannot be null" ); + } + + Map> annotations = new HashMap>(); + for ( List annotationList : classInfo.annotations().values() ) { + for ( AnnotationInstance instance : annotationList ) { + if ( instance.target() instanceof ClassInfo ) { + addAnnotationToMap( instance, annotations ); + } + } + } + return annotations; + } + private static void addAnnotationToMap(AnnotationInstance instance, Map> annotations) { DotName dotName = instance.name(); List list; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParser.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParser.java index 9243eae1f4..01c9d433dd 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParser.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/xml/OrmXmlParser.java @@ -10,8 +10,6 @@ import org.hibernate.metamodel.source.annotations.xml.mocker.EntityMappingsMocke import org.hibernate.metamodel.source.internal.JaxbRoot; import org.hibernate.metamodel.source.internal.MetadataImpl; -//import org.hibernate.metamodel.source.util.xml.XmlHelper; - /** * @author Hardy Ferentschik * @todo Need some create some XMLContext as well which can be populated w/ information which can not be expressed via annotations @@ -40,9 +38,6 @@ public class OrmXmlParser { list, annotationIndex, meta.getServiceRegistry() ).mockNewIndex(); } - - - } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/AbstractEntityBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/AbstractEntityBinder.java index 349e73ea58..d2ab3058ca 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/AbstractEntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/AbstractEntityBinder.java @@ -34,18 +34,18 @@ import org.hibernate.metamodel.binding.AttributeBinding; import org.hibernate.metamodel.binding.BagBinding; import org.hibernate.metamodel.binding.EntityBinding; import org.hibernate.metamodel.binding.ManyToOneAttributeBinding; -import org.hibernate.metamodel.binding.PluralAttributeBinding; import org.hibernate.metamodel.binding.SimpleAttributeBinding; +import org.hibernate.metamodel.binding.state.ManyToOneAttributeBindingState; import org.hibernate.metamodel.domain.Entity; import org.hibernate.metamodel.domain.Hierarchical; -import org.hibernate.metamodel.domain.PluralAttributeNature; import org.hibernate.metamodel.relational.Schema; import org.hibernate.metamodel.relational.Table; import org.hibernate.metamodel.relational.TableSpecification; import org.hibernate.metamodel.relational.UniqueKey; -import org.hibernate.metamodel.source.hbm.state.domain.HbmManyToOneAttributeDomainState; -import org.hibernate.metamodel.source.hbm.state.domain.HbmPluralAttributeDomainState; -import org.hibernate.metamodel.source.hbm.state.domain.HbmSimpleAttributeDomainState; +import org.hibernate.metamodel.relational.state.ManyToOneRelationalState; +import org.hibernate.metamodel.source.hbm.state.binding.HbmManyToOneAttributeBindingState; +import org.hibernate.metamodel.source.hbm.state.binding.HbmPluralAttributeBindingState; +import org.hibernate.metamodel.source.hbm.state.binding.HbmSimpleAttributeBindingState; import org.hibernate.metamodel.source.hbm.state.relational.HbmManyToOneRelationalStateContainer; import org.hibernate.metamodel.source.hbm.state.relational.HbmSimpleValueRelationalStateContainer; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLAnyElement; @@ -70,7 +70,11 @@ import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSqlQueryElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSubclassElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLTuplizerElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLUnionSubclassElement; -import org.hibernate.metamodel.source.internal.MetadataImpl; +import org.hibernate.metamodel.binding.state.PluralAttributeBindingState; +import org.hibernate.metamodel.binding.state.SimpleAttributeBindingState; +import org.hibernate.metamodel.relational.state.TupleRelationalState; +import org.hibernate.metamodel.relational.state.ValueRelationalState; +import org.hibernate.metamodel.source.spi.MetadataImplementor; /** * TODO : javadoc @@ -102,7 +106,7 @@ abstract class AbstractEntityBinder { return hibernateMappingBinder.getHibernateXmlBinder(); } - protected MetadataImpl getMetadata() { + protected MetadataImplementor getMetadata() { return hibernateMappingBinder.getHibernateXmlBinder().getMetadata(); } @@ -111,7 +115,7 @@ abstract class AbstractEntityBinder { } protected NamingStrategy getNamingStrategy() { - return getMetadata().getNamingStrategy(); + return getMetadata().getOptions().getNamingStrategy(); } protected void basicEntityBinding(XMLHibernateMapping.XMLClass entityClazz, @@ -239,12 +243,16 @@ abstract class AbstractEntityBinder { if ( entityClazz.getTable() == null ) { logicalTableName = StringHelper.unqualify( entityName ); physicalTableName = getHibernateXmlBinder().getMetadata() + .getOptions() .getNamingStrategy() .classToTableName( entityName ); } else { logicalTableName = entityClazz.getTable(); - physicalTableName = getHibernateXmlBinder().getMetadata().getNamingStrategy().tableName( logicalTableName ); + physicalTableName = getHibernateXmlBinder().getMetadata() + .getOptions() + .getNamingStrategy() + .tableName( logicalTableName ); } // todo : find out the purpose of these logical bindings // mappings.addTableBinding( schema, catalog, logicalTableName, physicalTableName, denormalizedSuperTable ); @@ -284,8 +292,7 @@ abstract class AbstractEntityBinder { for ( Object attribute : entityClazz.getPropertyOrManyToOneOrOneToOne() ) { if ( XMLBagElement.class.isInstance( attribute ) ) { XMLBagElement collection = XMLBagElement.class.cast( attribute ); - BagBinding collectionBinding = entityBinding.makeBagAttributeBinding( collection.getName() ); - bindBag( collection, collectionBinding, entityBinding ); + BagBinding collectionBinding = makeBagAttributeBinding( collection, entityBinding ); hibernateMappingBinder.getHibernateXmlBinder().getMetadata().addCollection( collectionBinding ); attributeBinding = collectionBinding; } @@ -321,12 +328,7 @@ abstract class AbstractEntityBinder { } else if ( XMLManyToOneElement.class.isInstance( attribute ) ) { XMLManyToOneElement manyToOne = XMLManyToOneElement.class.cast( attribute ); - ManyToOneAttributeBinding manyToOneBinding = entityBinding.makeManyToOneAttributeBinding( manyToOne.getName() ); - bindManyToOne( manyToOne, manyToOneBinding, entityBinding ); - attributeBinding = manyToOneBinding; -// todo : implement -// value = new ManyToOne( mappings, table ); -// bindManyToOne( subElement, (ManyToOne) value, propertyName, nullable, mappings ); + attributeBinding = makeManyToOneAttributeBinding( manyToOne, entityBinding ); } else if ( XMLAnyElement.class.isInstance( attribute ) ) { // todo : implement @@ -340,9 +342,7 @@ abstract class AbstractEntityBinder { } else if ( XMLPropertyElement.class.isInstance( attribute ) ) { XMLPropertyElement property = XMLPropertyElement.class.cast( attribute ); - SimpleAttributeBinding binding = entityBinding.makeSimpleAttributeBinding( property.getName() ); - bindSimpleAttribute( property, binding, entityBinding ); - attributeBinding = binding; + attributeBinding = bindProperty( property, entityBinding ); } else if ( XMLComponentElement.class.isInstance( attribute ) || XMLDynamicComponentElement.class.isInstance( attribute ) @@ -441,200 +441,86 @@ PrimitiveArray } - protected void bindSimpleAttribute(XMLHibernateMapping.XMLClass.XMLId id, - SimpleAttributeBinding attributeBinding, - EntityBinding entityBinding, - String attributeName) { - if ( attributeBinding.getAttribute() == null ) { - attributeBinding.initialize( - new HbmSimpleAttributeDomainState( - hibernateMappingBinder.getHibernateXmlBinder().getMetadata(), - hibernateMappingBinder, - entityBinding.getEntity().getOrCreateSingularAttribute( attributeName ), - entityBinding.getMetaAttributes(), - id - ) - ); - } + protected SimpleAttributeBinding bindProperty(XMLPropertyElement property, + EntityBinding entityBinding) { + SimpleAttributeBindingState bindingState = new HbmSimpleAttributeBindingState( + entityBinding.getEntity().getPojoEntitySpecifics().getClassName(), + getHibernateMappingBinder(), + entityBinding.getMetaAttributes(), + property + ); - if ( attributeBinding.getValue() == null ) { - // relational model has not been bound yet - // boolean (true here) indicates that by default column names should be guessed - attributeBinding.initializeTupleValue( - new HbmSimpleValueRelationalStateContainer( - getHibernateMappingBinder(), - true, - id - ) - ); - } + // boolean (true here) indicates that by default column names should be guessed + ValueRelationalState relationalState = + convertToSimpleValueRelationalStateIfPossible( + new HbmSimpleValueRelationalStateContainer( + getHibernateMappingBinder(), + true, + property + ) + ); + + entityBinding.getEntity().getOrCreateSingularAttribute( bindingState.getAttributeName() ); + return entityBinding.makeSimpleAttributeBinding( bindingState.getAttributeName() ) + .initialize( bindingState ) + .initialize( relationalState ); } - protected void bindSimpleAttribute(XMLHibernateMapping.XMLClass.XMLDiscriminator discriminator, - SimpleAttributeBinding attributeBinding, - EntityBinding entityBinding, - String attributeName) { - if ( attributeBinding.getAttribute() == null ) { - attributeBinding.initialize( - new HbmSimpleAttributeDomainState( - hibernateMappingBinder.getHibernateXmlBinder().getMetadata(), - hibernateMappingBinder, - entityBinding.getEntity().getOrCreateSingularAttribute( attributeName ), - discriminator - ) - ); - } - - if ( attributeBinding.getValue() == null ) { - // relational model has not been bound yet - // boolean (true here) indicates that by default column names should be guessed - attributeBinding.initializeTupleValue( - new HbmSimpleValueRelationalStateContainer( - getHibernateMappingBinder(), - true, - discriminator - ) - ); + protected static ValueRelationalState convertToSimpleValueRelationalStateIfPossible(ValueRelationalState state) { + // TODO: should a single-valued tuple always be converted??? + if ( !TupleRelationalState.class.isInstance( state ) ) { + return state; } + TupleRelationalState tupleRelationalState = TupleRelationalState.class.cast( state ); + return tupleRelationalState.getRelationalStates().size() == 1 ? + tupleRelationalState.getRelationalStates().get( 0 ) : + state; } - protected void bindSimpleAttribute(XMLHibernateMapping.XMLClass.XMLVersion version, - SimpleAttributeBinding attributeBinding, - EntityBinding entityBinding, - String attributeName) { - if ( attributeBinding.getAttribute() == null ) { - attributeBinding.initialize( - new HbmSimpleAttributeDomainState( - hibernateMappingBinder.getHibernateXmlBinder().getMetadata(), - hibernateMappingBinder, - entityBinding.getEntity().getOrCreateSingularAttribute( attributeName ), - entityBinding.getMetaAttributes(), - version - ) - ); - } - - if ( attributeBinding.getValue() == null ) { - // relational model has not been bound yet - // boolean (true here) indicates that by default column names should be guessed - attributeBinding.initializeTupleValue( - new HbmSimpleValueRelationalStateContainer( - getHibernateMappingBinder(), - true, - version - ) - ); - } - } - - protected void bindSimpleAttribute(XMLHibernateMapping.XMLClass.XMLTimestamp timestamp, - SimpleAttributeBinding attributeBinding, - EntityBinding entityBinding, - String attributeName) { - if ( attributeBinding.getAttribute() == null ) { - attributeBinding.initialize( - new HbmSimpleAttributeDomainState( - hibernateMappingBinder.getHibernateXmlBinder().getMetadata(), - hibernateMappingBinder, - entityBinding.getEntity().getOrCreateSingularAttribute( attributeName ), - entityBinding.getMetaAttributes(), - timestamp - ) - ); - } - - if ( attributeBinding.getValue() == null ) { - // relational model has not been bound yet - // boolean (true here) indicates that by default column names should be guessed - attributeBinding.initializeTupleValue( - new HbmSimpleValueRelationalStateContainer( - getHibernateMappingBinder(), - true, - timestamp - ) - ); - } - } - - protected void bindSimpleAttribute(XMLPropertyElement property, - SimpleAttributeBinding attributeBinding, - EntityBinding entityBinding) { - if ( attributeBinding.getAttribute() == null ) { - attributeBinding.initialize( - new HbmSimpleAttributeDomainState( - hibernateMappingBinder.getHibernateXmlBinder().getMetadata(), - hibernateMappingBinder, - entityBinding.getEntity().getOrCreateSingularAttribute( property.getName() ), - entityBinding.getMetaAttributes(), - property - ) - ); - } - - if ( attributeBinding.getValue() == null ) { - // relational model has not been bound yet - // boolean (true here) indicates that by default column names should be guessed - attributeBinding.initializeTupleValue( - new HbmSimpleValueRelationalStateContainer( - getHibernateMappingBinder(), - true, - property - ) - ); - } - } - - protected void bindBag( + protected BagBinding makeBagAttributeBinding( XMLBagElement collection, - PluralAttributeBinding collectionBinding, EntityBinding entityBinding) { - if ( collectionBinding.getAttribute() == null ) { - // domain model has not been bound yet - collectionBinding.initialize( - new HbmPluralAttributeDomainState( - hibernateMappingBinder.getHibernateXmlBinder().getMetadata(), - hibernateMappingBinder, - collection, - entityBinding.getMetaAttributes(), - entityBinding.getEntity().getOrCreatePluralAttribute( - collection.getName(), - PluralAttributeNature.BAG - ) - ) - ); - } - if ( collectionBinding.getValue() == null ) { + PluralAttributeBindingState bindingState = + new HbmPluralAttributeBindingState( + entityBinding.getEntity().getPojoEntitySpecifics().getClassName(), + hibernateMappingBinder, + collection, + entityBinding.getMetaAttributes() + ); + + BagBinding collectionBinding = entityBinding.makeBagAttributeBinding( bindingState.getAttributeName() ) + .initialize( bindingState ); + // todo : relational model binding - } + return collectionBinding; } - private void bindManyToOne(XMLManyToOneElement manyToOne, - ManyToOneAttributeBinding attributeBinding, + private ManyToOneAttributeBinding makeManyToOneAttributeBinding(XMLManyToOneElement manyToOne, EntityBinding entityBinding) { - if ( attributeBinding.getAttribute() == null ) { - attributeBinding.initialize( - new HbmManyToOneAttributeDomainState( - hibernateMappingBinder.getHibernateXmlBinder().getMetadata(), - hibernateMappingBinder, - entityBinding.getEntity().getOrCreateSingularAttribute( manyToOne.getName() ), - entityBinding.getMetaAttributes(), - manyToOne - ) - ); - } + ManyToOneAttributeBindingState bindingState = + new HbmManyToOneAttributeBindingState( + entityBinding.getEntity().getPojoEntitySpecifics().getClassName(), + hibernateMappingBinder, + entityBinding.getMetaAttributes(), + manyToOne + ); - if ( attributeBinding.getValue() == null ) { - // relational model has not been bound yet - // boolean (true here) indicates that by default column names should be guessed - attributeBinding.initialize( - new HbmManyToOneRelationalStateContainer( - getHibernateMappingBinder(), - true, - manyToOne - ) - ); - } + // boolean (true here) indicates that by default column names should be guessed + ManyToOneRelationalState relationalState = + new HbmManyToOneRelationalStateContainer( + getHibernateMappingBinder(), + true, + manyToOne + ); + + entityBinding.getEntity().getOrCreateSingularAttribute( bindingState.getAttributeName() ); + ManyToOneAttributeBinding manyToOneAttributeBinding = + entityBinding.makeManyToOneAttributeBinding( bindingState.getAttributeName() ) + .initialize( bindingState ) + .initialize( relationalState ); + + return manyToOneAttributeBinding; } // private static Property createProperty( diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HbmHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HbmHelper.java index f7ea3f2552..a6ce83aadd 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HbmHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HbmHelper.java @@ -36,7 +36,7 @@ import org.hibernate.metamodel.binding.CustomSQL; import org.hibernate.metamodel.domain.MetaAttribute; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLMetaElement; -import org.hibernate.metamodel.source.util.MappingHelper; +import org.hibernate.metamodel.source.hbm.util.MappingHelper; /** * TODO : javadoc diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateMappingBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateMappingBinder.java index b89043132d..9a9b43dbac 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateMappingBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateMappingBinder.java @@ -32,7 +32,6 @@ import org.hibernate.MappingException; import org.hibernate.cfg.NamingStrategy; import org.hibernate.internal.util.StringHelper; import org.hibernate.mapping.MetadataSource; -import org.hibernate.metamodel.binding.MappingDefaults; import org.hibernate.metamodel.domain.MetaAttribute; import org.hibernate.metamodel.source.Origin; import org.hibernate.metamodel.source.internal.JaxbRoot; @@ -46,12 +45,16 @@ import org.hibernate.metamodel.source.hbm.xml.mapping.XMLQueryElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSqlQueryElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSubclassElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLUnionSubclassElement; -import org.hibernate.metamodel.source.util.MappingHelper; +import org.hibernate.metamodel.source.hbm.util.MappingHelper; +import org.hibernate.service.ServiceRegistry; /** * Responsible for performing binding of the {@code } DOM element */ public class HibernateMappingBinder implements MappingDefaults { + private static final String DEFAULT_IDENTIFIER_COLUMN_NAME = "id"; + private static final String DEFAULT_DISCRIMINATOR_COLUMN_NAME = "class"; + private final HibernateXmlBinder hibernateXmlBinder; private final JaxbRoot jaxbRoot; private final XMLHibernateMapping hibernateMapping; @@ -103,6 +106,15 @@ public class HibernateMappingBinder implements MappingDefaults { return defaultCatalogName; } + public String getDefaultIdColumnName() { + return DEFAULT_IDENTIFIER_COLUMN_NAME; + + + } + public String getDefaultDiscriminatorColumnName() { + return DEFAULT_DISCRIMINATOR_COLUMN_NAME; + } + public String getDefaultCascade() { return defaultCascade; } @@ -115,8 +127,13 @@ public class HibernateMappingBinder implements MappingDefaults { return defaultLazy; } + @Override + public ServiceRegistry getServiceRegistry() { + return hibernateXmlBinder.getMetadata().getServiceRegistry(); + } + public NamingStrategy getNamingStrategy() { - return hibernateXmlBinder.getMetadata().getNamingStrategy(); + return hibernateXmlBinder.getMetadata().getOptions().getNamingStrategy(); } public String getPackageName() { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateXmlBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateXmlBinder.java index c3a5c4eda0..d0ff9dd8fa 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateXmlBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateXmlBinder.java @@ -40,8 +40,8 @@ import org.hibernate.internal.util.collections.JoinedIterator; import org.hibernate.internal.util.xml.XmlDocument; import org.hibernate.metamodel.domain.MetaAttribute; import org.hibernate.metamodel.source.internal.JaxbRoot; -import org.hibernate.metamodel.source.internal.MetadataImpl; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping; +import org.hibernate.metamodel.source.spi.MetadataImplementor; /** * Binder for {@code hbm.xml} files @@ -51,14 +51,14 @@ import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping; public class HibernateXmlBinder { private static final Logger log = LoggerFactory.getLogger( HibernateXmlBinder.class ); - private final MetadataImpl metadata; + private final MetadataImplementor metadata; private final Map globalMetas; - public HibernateXmlBinder(MetadataImpl metadata) { + public HibernateXmlBinder(MetadataImplementor metadata) { this( metadata, Collections.emptyMap() ); } - public HibernateXmlBinder(MetadataImpl metadata, Map globalMetas) { + public HibernateXmlBinder(MetadataImplementor metadata, Map globalMetas) { this.metadata = metadata; this.globalMetas = globalMetas; } @@ -85,7 +85,7 @@ public class HibernateXmlBinder { mappingBinder.processHibernateMapping(); } - MetadataImpl getMetadata() { + MetadataImplementor getMetadata() { return metadata; } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/MappingDefaults.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/MappingDefaults.java similarity index 87% rename from hibernate-core/src/main/java/org/hibernate/metamodel/binding/MappingDefaults.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/MappingDefaults.java index 8f18a0978d..3b38477c2f 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/binding/MappingDefaults.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/MappingDefaults.java @@ -21,23 +21,37 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.binding; +package org.hibernate.metamodel.source.hbm; import java.util.Map; import org.hibernate.cfg.NamingStrategy; import org.hibernate.metamodel.domain.MetaAttribute; +import org.hibernate.service.ServiceRegistry; /** * @author Gail Badner */ public interface MappingDefaults { Map getMappingMetas(); + String getPackageName(); + String getDefaultSchemaName(); + String getDefaultCatalogName(); + + String getDefaultIdColumnName(); + + String getDefaultDiscriminatorColumnName(); + String getDefaultCascade(); + String getDefaultAccess(); + boolean isDefaultLazy(); + + ServiceRegistry getServiceRegistry(); + NamingStrategy getNamingStrategy(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/RootEntityBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/RootEntityBinder.java index 112fb24fd4..89627e6efb 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/RootEntityBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/RootEntityBinder.java @@ -26,18 +26,22 @@ package org.hibernate.metamodel.source.hbm; import org.hibernate.InvalidMappingException; import org.hibernate.MappingException; import org.hibernate.cache.spi.access.AccessType; -import org.hibernate.mapping.RootClass; import org.hibernate.metamodel.binding.Caching; import org.hibernate.metamodel.binding.EntityBinding; -import org.hibernate.metamodel.binding.SimpleAttributeBinding; -import org.hibernate.metamodel.relational.Column; +import org.hibernate.metamodel.binding.state.SimpleAttributeBindingState; import org.hibernate.metamodel.relational.Identifier; import org.hibernate.metamodel.relational.InLineView; import org.hibernate.metamodel.relational.Schema; +import org.hibernate.metamodel.relational.state.ValueRelationalState; +import org.hibernate.metamodel.source.hbm.state.binding.HbmDiscriminatorBindingState; +import org.hibernate.metamodel.source.hbm.state.binding.HbmSimpleAttributeBindingState; +import org.hibernate.metamodel.source.hbm.state.relational.HbmSimpleValueRelationalStateContainer; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLCacheElement; +import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLCompositeId; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLId; +import org.hibernate.metamodel.binding.state.DiscriminatorBindingState; /** * TODO : javadoc @@ -76,7 +80,7 @@ class RootEntityBinder extends AbstractEntityBinder { bindIdentifier( xmlClazz, entityBinding ); bindDiscriminator( xmlClazz, entityBinding ); - bindVersion( xmlClazz, entityBinding ); + bindVersionOrTimestamp( xmlClazz, entityBinding ); bindCaching( xmlClazz, entityBinding ); // called createClassProperties in HBMBinder... @@ -135,18 +139,25 @@ class RootEntityBinder extends AbstractEntityBinder { } private void bindSimpleId(XMLId id, EntityBinding entityBinding) { - // Handle the domain portion of the binding... - final String explicitName = id.getName(); - final String attributeName = explicitName == null ? RootClass.DEFAULT_IDENTIFIER_COLUMN_NAME : explicitName; - SimpleAttributeBinding idBinding = entityBinding.makeSimplePrimaryKeyAttributeBinding( attributeName ); - bindSimpleAttribute( id, idBinding, entityBinding, attributeName ); - - if ( !Column.class.isInstance( idBinding.getValue() ) ) { - // this should never ever happen.. - throw new MappingException( "Unanticipated situation" ); + SimpleAttributeBindingState bindingState = new HbmSimpleAttributeBindingState( + entityBinding.getEntity().getPojoEntitySpecifics().getClassName(), + getHibernateMappingBinder(), + entityBinding.getMetaAttributes(), + id + ); + // boolean (true here) indicates that by default column names should be guessed + HbmSimpleValueRelationalStateContainer relationalStateContainer = new HbmSimpleValueRelationalStateContainer( + getHibernateMappingBinder(), true, id + ); + if ( relationalStateContainer.getRelationalStates().size() > 1 ) { + throw new MappingException( "ID is expected to be a single column, but has more than 1 value" ); } - entityBinding.getBaseTable().getPrimaryKey().addColumn( Column.class.cast( idBinding.getValue() ) ); + entityBinding.getEntity().getOrCreateSingularAttribute( bindingState.getAttributeName() ); + entityBinding.makeSimpleIdAttributeBinding( bindingState.getAttributeName() ) + .initialize( bindingState ) + .initialize( relationalStateContainer.getRelationalStates().get( 0 ) ); + // if ( propertyName == null || entity.getPojoRepresentation() == null ) { // bindSimpleValue( idNode, id, false, RootClass.DEFAULT_IDENTIFIER_COLUMN_NAME, mappings ); // if ( !id.isTypeSpecified() ) { @@ -233,53 +244,97 @@ class RootEntityBinder extends AbstractEntityBinder { return; } - // Discriminator.getName() is not defined, so the attribute will always be RootClass.DEFAULT_DISCRIMINATOR_COLUMN_NAME - SimpleAttributeBinding discriminatorBinding = entityBinding.makeEntityDiscriminatorBinding( RootClass.DEFAULT_DISCRIMINATOR_COLUMN_NAME ); - - // Handle the relational portion of the binding... - bindSimpleAttribute( - xmlEntityClazz.getDiscriminator(), - discriminatorBinding, - entityBinding, - RootClass.DEFAULT_DISCRIMINATOR_COLUMN_NAME + DiscriminatorBindingState bindingState = new HbmDiscriminatorBindingState( + entityBinding.getEntity().getPojoEntitySpecifics().getClassName(), + getHibernateMappingBinder(), + xmlEntityClazz.getDiscriminator() ); - entityBinding.getEntityDiscriminator().setForced( xmlEntityClazz.getDiscriminator().isForce() ); + // boolean (true here) indicates that by default column names should be guessed + ValueRelationalState relationalState = convertToSimpleValueRelationalStateIfPossible( + new HbmSimpleValueRelationalStateContainer( + getHibernateMappingBinder(), + true, + xmlEntityClazz.getDiscriminator() + ) + ); + + + entityBinding.getEntity().getOrCreateSingularAttribute( bindingState.getAttributeName() ); + entityBinding.makeEntityDiscriminator( bindingState.getAttributeName() ) + .initialize( bindingState ) + .initialize( relationalState ); } - private void bindVersion(XMLClass xmlEntityClazz, - EntityBinding entityBinding) { - if ( xmlEntityClazz.getVersion() == null && xmlEntityClazz.getTimestamp() == null ) { - return; - } - - boolean isVersion = xmlEntityClazz.getVersion() != null; - String explicitName = isVersion ? xmlEntityClazz.getVersion().getName() : xmlEntityClazz.getTimestamp() - .getName(); - if ( explicitName == null ) { - throw new MappingException( - "Missing property name for version/timestamp mapping [" + entityBinding.getEntity().getName() + "]" - ); - } - SimpleAttributeBinding versionBinding = entityBinding.makeVersionBinding( explicitName ); - if ( isVersion ) { - bindSimpleAttribute( + private void bindVersionOrTimestamp(XMLClass xmlEntityClazz, + EntityBinding entityBinding) { + if ( xmlEntityClazz.getVersion() != null ) { + bindVersion( xmlEntityClazz.getVersion(), - versionBinding, - entityBinding, - explicitName + entityBinding ); } - else { - bindSimpleAttribute( + else if ( xmlEntityClazz.getTimestamp() != null ) { + bindTimestamp( xmlEntityClazz.getTimestamp(), - versionBinding, - entityBinding, - explicitName + entityBinding ); } } + protected void bindVersion(XMLHibernateMapping.XMLClass.XMLVersion version, + EntityBinding entityBinding) { + SimpleAttributeBindingState bindingState = + new HbmSimpleAttributeBindingState( + entityBinding.getEntity().getPojoEntitySpecifics().getClassName(), + getHibernateMappingBinder(), + entityBinding.getMetaAttributes(), + version + ); + + // boolean (true here) indicates that by default column names should be guessed + ValueRelationalState relationalState = + convertToSimpleValueRelationalStateIfPossible( + new HbmSimpleValueRelationalStateContainer( + getHibernateMappingBinder(), + true, + version + ) + ); + + entityBinding.getEntity().getOrCreateSingularAttribute( bindingState.getAttributeName() ); + entityBinding.makeVersionBinding( bindingState.getAttributeName() ) + .initialize( bindingState ) + .initialize( relationalState ); + } + + protected void bindTimestamp(XMLHibernateMapping.XMLClass.XMLTimestamp timestamp, + EntityBinding entityBinding) { + + SimpleAttributeBindingState bindingState = + new HbmSimpleAttributeBindingState( + entityBinding.getEntity().getPojoEntitySpecifics().getClassName(), + getHibernateMappingBinder(), + entityBinding.getMetaAttributes(), + timestamp + ); + + // relational model has not been bound yet + // boolean (true here) indicates that by default column names should be guessed + ValueRelationalState relationalState = + convertToSimpleValueRelationalStateIfPossible( + new HbmSimpleValueRelationalStateContainer( + getHibernateMappingBinder(), + true, + timestamp + ) + ); + + entityBinding.makeVersionBinding( bindingState.getAttributeName() ) + .initialize( bindingState ) + .initialize( relationalState ); + } + private void bindCaching(XMLClass xmlClazz, EntityBinding entityBinding) { XMLCacheElement cache = xmlClazz.getCache(); diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/domain/AbstractHbmAttributeDomainState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/AbstractHbmAttributeBindingState.java similarity index 56% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/domain/AbstractHbmAttributeDomainState.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/AbstractHbmAttributeBindingState.java index 868301b69d..bce8b9e8c5 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/domain/AbstractHbmAttributeDomainState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/AbstractHbmAttributeBindingState.java @@ -21,59 +21,71 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.hbm.state.domain; +package org.hibernate.metamodel.source.hbm.state.binding; import java.util.Map; -import org.hibernate.metamodel.Metadata; -import org.hibernate.metamodel.binding.MappingDefaults; -import org.hibernate.metamodel.domain.Attribute; +import org.hibernate.MappingException; +import org.hibernate.internal.util.ReflectHelper; +import org.hibernate.mapping.PropertyGeneration; +import org.hibernate.metamodel.source.hbm.MappingDefaults; +import org.hibernate.metamodel.binding.state.AttributeBindingState; import org.hibernate.metamodel.domain.MetaAttribute; -import org.hibernate.metamodel.source.internal.MetadataImpl; -import org.hibernate.metamodel.source.util.MappingHelper; -import org.hibernate.metamodel.state.domain.AttributeDomainState; +import org.hibernate.metamodel.source.hbm.util.MappingHelper; /** * @author Gail Badner */ -public abstract class AbstractHbmAttributeDomainState implements AttributeDomainState { - private final MetadataImpl metadata; +public abstract class AbstractHbmAttributeBindingState implements AttributeBindingState { + private final String ownerClassName; + private final String attributeName; private final MappingDefaults defaults; - private final Attribute attribute; private final String nodeName; private final String accessorName; private final boolean isOptimisticLockable; private final Map metaAttributes; - public AbstractHbmAttributeDomainState( - MetadataImpl metadata, + public AbstractHbmAttributeBindingState( + String ownerClassName, + String attributeName, MappingDefaults defaults, - Attribute attribute, String nodeName, Map metaAttributes, String accessorName, boolean isOptimisticLockable) { - this.metadata = metadata; + if ( attributeName == null ) { + throw new MappingException( + "Attribute name cannot be null." + ); + } + + this.ownerClassName = ownerClassName; + this.attributeName = attributeName; this.defaults = defaults; - this.attribute = attribute; - this.nodeName = MappingHelper.getStringValue( nodeName, attribute.getName() ); + this.nodeName = nodeName; this.metaAttributes = metaAttributes; this.accessorName = accessorName; this.isOptimisticLockable = isOptimisticLockable; } - public MetadataImpl getMetadata() { - return metadata; + // TODO: really don't like this here... + protected String getOwnerClassName() { + return ownerClassName; + } + + protected final String getTypeNameByReflection() { + Class ownerClass = MappingHelper.classForName( ownerClassName, defaults.getServiceRegistry() ); + return ReflectHelper.reflectedPropertyClass( ownerClass, attributeName ).getName(); + } + + public String getAttributeName() { + return attributeName; } protected final MappingDefaults getDefaults() { return defaults; } - public final Attribute getAttribute() { - return attribute; - } - public final String getPropertyAccessorName() { return accessorName; } @@ -88,10 +100,27 @@ public abstract class AbstractHbmAttributeDomainState implements AttributeDomain } public final String getNodeName() { - return nodeName; + return nodeName == null ? getAttributeName() : nodeName; } public final Map getMetaAttributes() { return metaAttributes; } + + public PropertyGeneration getPropertyGeneration() { + return PropertyGeneration.NEVER; + } + + public boolean isKeyCascadeDeleteEnabled() { + return false; + } + + public String getUnsavedValue() { + //TODO: implement + return null; + } + + public Map getTypeParameters() { + return null; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmDiscriminatorBindingState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmDiscriminatorBindingState.java new file mode 100644 index 0000000000..65956dc093 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmDiscriminatorBindingState.java @@ -0,0 +1,76 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.source.hbm.state.binding; + +import org.hibernate.metamodel.source.hbm.MappingDefaults; +import org.hibernate.metamodel.binding.state.DiscriminatorBindingState; +import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLDiscriminator; + +/** + * @author Gail Badner + */ +public class HbmDiscriminatorBindingState extends AbstractHbmAttributeBindingState + implements DiscriminatorBindingState { + private final XMLDiscriminator discriminator; + + public HbmDiscriminatorBindingState( + String ownerClassName, + MappingDefaults defaults, + XMLDiscriminator discriminator) { + // Discriminator.getName() is not defined, so the attribute will always be + // defaults.getDefaultDescriminatorColumnName() + super( + ownerClassName, defaults.getDefaultDiscriminatorColumnName(), defaults, null, null, null, true + ); + this.discriminator = discriminator; + } + + public String getCascade() { + return null; + } + + protected boolean isEmbedded() { + return false; + } + + public String getTypeName() { + return discriminator.getType() == null ? "string" : discriminator.getType(); + } + + public boolean isLazy() { + return false; + } + + public boolean isInsertable() { + return discriminator.isInsert(); + } + + public boolean isUpdatable() { + return false; + } + + public boolean isForced() { + return discriminator.isForce(); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/domain/HbmManyToOneAttributeDomainState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmManyToOneAttributeBindingState.java similarity index 73% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/domain/HbmManyToOneAttributeDomainState.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmManyToOneAttributeBindingState.java index c254bf02b5..bd088ae261 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/domain/HbmManyToOneAttributeDomainState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmManyToOneAttributeBindingState.java @@ -21,28 +21,26 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.hbm.state.domain; +package org.hibernate.metamodel.source.hbm.state.binding; import java.util.Map; import org.hibernate.FetchMode; -import org.hibernate.metamodel.binding.HibernateTypeDescriptor; -import org.hibernate.metamodel.binding.MappingDefaults; +import org.hibernate.internal.util.ReflectHelper; +import org.hibernate.metamodel.source.hbm.MappingDefaults; +import org.hibernate.metamodel.binding.state.ManyToOneAttributeBindingState; import org.hibernate.metamodel.domain.MetaAttribute; import org.hibernate.metamodel.source.hbm.HbmHelper; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLManyToOneElement; -import org.hibernate.metamodel.source.internal.MetadataImpl; -import org.hibernate.metamodel.source.util.MappingHelper; -import org.hibernate.metamodel.state.domain.ManyToOneAttributeDomainState; +import org.hibernate.metamodel.source.hbm.util.MappingHelper; /** * @author Gail Badner */ -public class HbmManyToOneAttributeDomainState - extends AbstractHbmAttributeDomainState - implements ManyToOneAttributeDomainState { +public class HbmManyToOneAttributeBindingState + extends AbstractHbmAttributeBindingState + implements ManyToOneAttributeBindingState { - private final HibernateTypeDescriptor hibernateTypeDescriptor = new HibernateTypeDescriptor(); private final FetchMode fetchMode; private final boolean isUnwrapProxy; private final boolean isLazy; @@ -54,16 +52,15 @@ public class HbmManyToOneAttributeDomainState private final boolean isInsertable; private final boolean isUpdateable; - public HbmManyToOneAttributeDomainState( - MetadataImpl metadata, + public HbmManyToOneAttributeBindingState( + String ownerClassName, MappingDefaults defaults, - org.hibernate.metamodel.domain.Attribute attribute, Map entityMetaAttributes, XMLManyToOneElement manyToOne) { super( - metadata, + ownerClassName, + manyToOne.getName(), defaults, - attribute, manyToOne.getNode(), HbmHelper.extractMetas( manyToOne.getMeta(), entityMetaAttributes ), HbmHelper.getPropertyAccessorName( @@ -79,13 +76,8 @@ public class HbmManyToOneAttributeDomainState "proxy".equals( manyToOne.getLazy().value() ); cascade = MappingHelper.getStringValue( manyToOne.getCascade(), defaults.getDefaultCascade() ); isEmbedded = manyToOne.isEmbedXml(); - hibernateTypeDescriptor.setTypeName( getReferencedEntityName() ); + referencedEntityName = getReferencedEntityName( ownerClassName, manyToOne, defaults ); referencedPropertyName = manyToOne.getPropertyRef(); - referencedEntityName = ( - manyToOne.getEntityName() == null ? - HbmHelper.getClassName( manyToOne.getClazz(), getDefaults().getPackageName() ) : - manyToOne.getEntityName().intern() - ); ignoreNotFound = "ignore".equals( manyToOne.getNotFound().value() ); isInsertable = manyToOne.isInsert(); isUpdateable = manyToOne.isUpdate(); @@ -96,8 +88,19 @@ public class HbmManyToOneAttributeDomainState return isEmbedded; } - public HibernateTypeDescriptor getHibernateTypeDescriptor() { - return hibernateTypeDescriptor; + private static String getReferencedEntityName(String ownerClassName, XMLManyToOneElement manyToOne, MappingDefaults defaults) { + String referencedEntityName; + if ( manyToOne.getEntityName() != null ) { + referencedEntityName = manyToOne.getEntityName(); + } + else if ( manyToOne.getClazz() != null ) { + referencedEntityName = HbmHelper.getClassName( manyToOne.getClazz(), defaults.getPackageName() ); + } + else { + Class ownerClazz = MappingHelper.classForName( ownerClassName, defaults.getServiceRegistry() ); + referencedEntityName = ReflectHelper.reflectedPropertyClass( ownerClazz, manyToOne.getName() ).getName(); + } + return referencedEntityName; } // same as for plural attributes... @@ -121,6 +124,10 @@ public class HbmManyToOneAttributeDomainState return fetchMode; } + public String getTypeName() { + return referencedEntityName; + } + public FetchMode getFetchMode() { return fetchMode; } @@ -153,11 +160,11 @@ public class HbmManyToOneAttributeDomainState return isInsertable; } - public boolean isUpdateable() { + public boolean isUpdatable() { return isUpdateable; } - public boolean isKeyCasadeDeleteEnabled() { + public boolean isKeyCascadeDeleteEnabled() { //TODO: implement return false; } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/domain/HbmPluralAttributeDomainState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmPluralAttributeBindingState.java similarity index 77% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/domain/HbmPluralAttributeDomainState.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmPluralAttributeBindingState.java index 7716c36094..291934739a 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/domain/HbmPluralAttributeDomainState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmPluralAttributeBindingState.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.hbm.state.domain; +package org.hibernate.metamodel.source.hbm.state.binding; import java.util.Comparator; import java.util.HashMap; @@ -29,11 +29,9 @@ import java.util.HashSet; import java.util.Map; import org.hibernate.FetchMode; -import org.hibernate.MappingException; import org.hibernate.metamodel.binding.CustomSQL; -import org.hibernate.metamodel.binding.HibernateTypeDescriptor; -import org.hibernate.metamodel.binding.MappingDefaults; -import org.hibernate.metamodel.domain.Attribute; +import org.hibernate.metamodel.source.hbm.MappingDefaults; +import org.hibernate.metamodel.binding.state.PluralAttributeBindingState; import org.hibernate.metamodel.domain.MetaAttribute; import org.hibernate.metamodel.source.hbm.HbmHelper; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLBagElement; @@ -42,33 +40,27 @@ import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSqlDeleteElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSqlInsertElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSqlUpdateElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLSynchronizeElement; -import org.hibernate.metamodel.source.internal.MetadataImpl; -import org.hibernate.metamodel.source.util.MappingHelper; -import org.hibernate.metamodel.state.domain.CollectionElementDomainState; -import org.hibernate.metamodel.state.domain.PluralAttributeDomainState; -import org.hibernate.service.classloading.spi.ClassLoaderService; -import org.hibernate.service.classloading.spi.ClassLoadingException; - +import org.hibernate.metamodel.source.hbm.util.MappingHelper; /** * @author Gail Badner */ -public class HbmPluralAttributeDomainState extends AbstractHbmAttributeDomainState - implements PluralAttributeDomainState { +public class HbmPluralAttributeBindingState extends AbstractHbmAttributeBindingState + implements PluralAttributeBindingState { private final XMLBagElement collection; - private final HibernateTypeDescriptor hibernateTypeDescriptor = new HibernateTypeDescriptor(); + private final Class collectionPersisterClass; + private final String typeName; private final String cascade; - public HbmPluralAttributeDomainState( - MetadataImpl metadata, + public HbmPluralAttributeBindingState( + String ownerClassName, MappingDefaults mappingDefaults, XMLBagElement collection, - Map entityMetaAttributes, - Attribute attribute) { + Map entityMetaAttributes) { super( - metadata, + ownerClassName, + collection.getName(), mappingDefaults, - attribute, collection.getNode(), HbmHelper.extractMetas( collection.getMeta(), entityMetaAttributes ), HbmHelper.getPropertyAccessorName( @@ -77,9 +69,11 @@ public class HbmPluralAttributeDomainState extends AbstractHbmAttributeDomainSta collection.isOptimisticLock() ); this.collection = collection; - // TODO: is collection.getCollectionType() correct here? - this.hibernateTypeDescriptor.setTypeName( collection.getCollectionType() ); + this.collectionPersisterClass = MappingHelper.classForName( + collection.getPersister(), getDefaults().getServiceRegistry() + ); this.cascade = MappingHelper.getStringValue( collection.getCascade(), mappingDefaults.getDefaultCascade() ); + //Attribute typeNode = collectionElement.attribute( "collection-type" ); //if ( typeNode != null ) { // TODO: implement when typedef binding is implemented @@ -95,14 +89,13 @@ public class HbmPluralAttributeDomainState extends AbstractHbmAttributeDomainSta } */ //} - //TODO: fix this!!! - this.hibernateTypeDescriptor.setTypeName( collection.getCollectionType() ); + typeName = collection.getCollectionType(); } public FetchMode getFetchMode() { FetchMode fetchMode; if ( collection.getFetch() != null ) { - fetchMode = "join".equals( collection.getFetch() ) ? FetchMode.JOIN : FetchMode.SELECT; + fetchMode = "join".equals( collection.getFetch().value() ) ? FetchMode.JOIN : FetchMode.SELECT; } else { String jfNodeValue = ( collection.getOuterJoin().value() == null ? "auto" : collection.getOuterJoin() @@ -126,11 +119,16 @@ public class HbmPluralAttributeDomainState extends AbstractHbmAttributeDomainSta } public boolean isExtraLazy() { - return ( "extra".equals( collection.getLazy() ) ); + return ( "extra".equals( collection.getLazy().value() ) ); } - public CollectionElementDomainState getCollectionElementDomainState() { - return new HbmCollectionElementDomainState( collection.getElement() ); + public String getElementTypeName() { + return collection.getElement().getTypeAttribute(); + + } + + public String getElementNodeName() { + return collection.getElement().getNode(); } public boolean isInverse() { @@ -142,7 +140,7 @@ public class HbmPluralAttributeDomainState extends AbstractHbmAttributeDomainSta } public boolean isSubselectLoadable() { - return "subselect".equals( collection.getFetch() ); + return "subselect".equals( collection.getFetch().value() ); } public String getCacheConcurrencyStrategy() { @@ -213,22 +211,7 @@ public class HbmPluralAttributeDomainState extends AbstractHbmAttributeDomainSta } public Class getCollectionPersisterClass() { - String className = collection.getPersister(); - ClassLoaderService classLoaderService = getMetadata().getServiceRegistry() - .getService( ClassLoaderService.class ); - try { - return classLoaderService.classForName( className ); - } - catch ( ClassLoadingException e ) { - throw new MappingException( - "Could not find collection persister class: " - + collection.getPersister() - ); - } - } - - public HibernateTypeDescriptor getHibernateTypeDescriptor() { - return hibernateTypeDescriptor; + return collectionPersisterClass; } public java.util.Map getFilters() { @@ -303,7 +286,7 @@ public class HbmPluralAttributeDomainState extends AbstractHbmAttributeDomainSta return cascade; } - public boolean isKeyCasadeDeleteEnabled() { + public boolean isKeyCascadeDeleteEnabled() { //TODO: implement return false; } @@ -312,4 +295,8 @@ public class HbmPluralAttributeDomainState extends AbstractHbmAttributeDomainSta //TODO: implement return null; } + + public String getTypeName() { + return typeName; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/domain/HbmSimpleAttributeDomainState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmSimpleAttributeBindingState.java similarity index 64% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/domain/HbmSimpleAttributeDomainState.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmSimpleAttributeBindingState.java index c15b442b0a..55ed3e3f83 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/domain/HbmSimpleAttributeDomainState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/binding/HbmSimpleAttributeBindingState.java @@ -21,46 +21,46 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.hbm.state.domain; +package org.hibernate.metamodel.source.hbm.state.binding; +import java.util.HashMap; import java.util.Map; import org.hibernate.MappingException; import org.hibernate.mapping.PropertyGeneration; -import org.hibernate.metamodel.binding.HibernateTypeDescriptor; -import org.hibernate.metamodel.binding.MappingDefaults; +import org.hibernate.metamodel.source.hbm.MappingDefaults; +import org.hibernate.metamodel.binding.state.SimpleAttributeBindingState; import org.hibernate.metamodel.domain.MetaAttribute; import org.hibernate.metamodel.source.hbm.HbmHelper; -import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLDiscriminator; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLId; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLTimestamp; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLVersion; +import org.hibernate.metamodel.source.hbm.xml.mapping.XMLParamElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLPropertyElement; -import org.hibernate.metamodel.source.internal.MetadataImpl; -import org.hibernate.metamodel.source.util.MappingHelper; -import org.hibernate.metamodel.state.domain.SimpleAttributeDomainState; +import org.hibernate.metamodel.source.hbm.util.MappingHelper; /** * @author Gail Badner */ -public class HbmSimpleAttributeDomainState extends AbstractHbmAttributeDomainState - implements SimpleAttributeDomainState { - private final HibernateTypeDescriptor hibernateTypeDescriptor = new HibernateTypeDescriptor(); +public class HbmSimpleAttributeBindingState extends AbstractHbmAttributeBindingState + implements SimpleAttributeBindingState { + private final String typeName; + private final Map typeParameters = new HashMap(); + private final boolean isLazy; private final PropertyGeneration propertyGeneration; private final boolean isInsertable; - private final boolean isUpdateable; + private final boolean isUpdatable; - public HbmSimpleAttributeDomainState( - MetadataImpl metadata, + public HbmSimpleAttributeBindingState( + String ownerClassName, MappingDefaults defaults, - org.hibernate.metamodel.domain.Attribute attribute, Map entityMetaAttributes, XMLId id) { super( - metadata, + ownerClassName, + id.getName() != null ? id.getName() : defaults.getDefaultIdColumnName(), defaults, - attribute, id.getNode(), HbmHelper.extractMetas( id.getMeta(), entityMetaAttributes ), HbmHelper.getPropertyAccessorName( id.getAccess(), false, defaults.getDefaultAccess() ), @@ -68,51 +68,49 @@ public class HbmSimpleAttributeDomainState extends AbstractHbmAttributeDomainSta ); this.isLazy = false; - if ( id.getType() != null ) { - this.hibernateTypeDescriptor.setTypeName( id.getType().getName() ); + if ( id.getTypeAttribute() != null ) { + typeName = maybeConvertToTypeDefName( id.getTypeAttribute(), defaults ); + } + else if ( id.getType() != null ) { + typeName = maybeConvertToTypeDefName( id.getType().getName(), defaults ); + } + else { + typeName = getTypeNameByReflection(); } // TODO: how should these be set??? this.propertyGeneration = PropertyGeneration.parse( null ); this.isInsertable = true; - this.isUpdateable = false; + this.isUpdatable = false; } - public HbmSimpleAttributeDomainState( - MetadataImpl metadata, - MappingDefaults defaults, - org.hibernate.metamodel.domain.Attribute attribute, - XMLDiscriminator discriminator) { - super( - metadata, defaults, attribute, null, null, null, true - ); - this.hibernateTypeDescriptor - .setTypeName( discriminator.getType() == null ? "string" : discriminator.getType() ); - this.isLazy = false; - - this.propertyGeneration = PropertyGeneration.NEVER; - this.isInsertable = discriminator.isInsert(); - this.isUpdateable = false; + private static String maybeConvertToTypeDefName(String typeName, MappingDefaults defaults) { + String actualTypeName = typeName; + if ( typeName != null ) { + // TODO: tweak for typedef... + } + else { + } + return actualTypeName; } - public HbmSimpleAttributeDomainState( - MetadataImpl metadata, + public HbmSimpleAttributeBindingState( + String ownerClassName, MappingDefaults defaults, - org.hibernate.metamodel.domain.Attribute attribute, Map entityMetaAttributes, XMLVersion version) { - super( - metadata, + ownerClassName, + version.getName(), defaults, - attribute, version.getNode(), HbmHelper.extractMetas( version.getMeta(), entityMetaAttributes ), HbmHelper.getPropertyAccessorName( version.getAccess(), false, defaults.getDefaultAccess() ), true ); - this.hibernateTypeDescriptor.setTypeName( version.getType() == null ? "integer" : version.getType() ); + this.typeName = version.getType() == null ? "integer" : version.getType(); + this.isLazy = false; // for version properties marked as being generated, make sure they are "always" @@ -123,27 +121,27 @@ public class HbmSimpleAttributeDomainState extends AbstractHbmAttributeDomainSta throw new MappingException( "'generated' attribute cannot be 'insert' for versioning property" ); } this.isInsertable = MappingHelper.getBooleanValue( version.isInsert(), true ); - this.isUpdateable = true; + this.isUpdatable = true; } - public HbmSimpleAttributeDomainState( - MetadataImpl metadata, + public HbmSimpleAttributeBindingState( + String ownerClassName, MappingDefaults defaults, - org.hibernate.metamodel.domain.Attribute attribute, Map entityMetaAttributes, XMLTimestamp timestamp) { super( - metadata, + ownerClassName, + timestamp.getName(), defaults, - attribute, timestamp.getNode(), HbmHelper.extractMetas( timestamp.getMeta(), entityMetaAttributes ), HbmHelper.getPropertyAccessorName( timestamp.getAccess(), false, defaults.getDefaultAccess() ), true ); + // Timestamp.getType() is not defined - this.hibernateTypeDescriptor.setTypeName( "db".equals( timestamp.getSource() ) ? "dbtimestamp" : "timestamp" ); + this.typeName = "db".equals( timestamp.getSource() ) ? "dbtimestamp" : "timestamp"; this.isLazy = false; // for version properties marked as being generated, make sure they are "always" @@ -154,19 +152,18 @@ public class HbmSimpleAttributeDomainState extends AbstractHbmAttributeDomainSta throw new MappingException( "'generated' attribute cannot be 'insert' for versioning property" ); } this.isInsertable = true; //TODO: is this right???? - this.isUpdateable = true; + this.isUpdatable = true; } - public HbmSimpleAttributeDomainState( - MetadataImpl metadata, + public HbmSimpleAttributeBindingState( + String ownerClassName, MappingDefaults defaults, - org.hibernate.metamodel.domain.Attribute attribute, Map entityMetaAttributes, XMLPropertyElement property) { super( - metadata, + ownerClassName, + property.getName(), defaults, - attribute, property.getNode(), HbmHelper.extractMetas( property.getMeta(), entityMetaAttributes ), HbmHelper.getPropertyAccessorName( property.getAccess(), false, defaults.getDefaultAccess() ), @@ -182,7 +179,7 @@ public class HbmSimpleAttributeDomainState extends AbstractHbmAttributeDomainSta throw new MappingException( "cannot specify both insert=\"true\" and generated=\"" + propertyGeneration.getName() + "\" for property: " + - getAttribute().getName() + property.getName() ); } isInsertable = false; @@ -197,22 +194,55 @@ public class HbmSimpleAttributeDomainState extends AbstractHbmAttributeDomainSta throw new MappingException( "cannot specify both update=\"true\" and generated=\"" + propertyGeneration.getName() + "\" for property: " + - getAttribute().getName() + property.getName() ); } - isUpdateable = false; + isUpdatable = false; } else { - isUpdateable = MappingHelper.getBooleanValue( property.isUpdate(), true ); + isUpdatable = MappingHelper.getBooleanValue( property.isUpdate(), true ); } + + if ( property.getTypeAttribute() != null ) { + typeName = maybeConvertToTypeDefName( property.getTypeAttribute(), defaults ); + } + else if ( property.getType() != null ) { + typeName = maybeConvertToTypeDefName( property.getType().getName(), defaults ); + for ( XMLParamElement typeParameter : property.getType().getParam() ) { + //TODO: add parameters from typedef + typeParameters.put( typeParameter.getName(), typeParameter.getValue().trim() ); + } + } + else { + typeName = getTypeNameByReflection(); + } + + + // TODO: check for typedef first + /* + TypeDef typeDef = mappings.getTypeDef( typeName ); + if ( typeDef != null ) { + typeName = typeDef.getTypeClass(); + // parameters on the property mapping should + // override parameters in the typedef + Properties allParameters = new Properties(); + allParameters.putAll( typeDef.getParameters() ); + allParameters.putAll( parameters ); + parameters = allParameters; + } + */ } protected boolean isEmbedded() { return false; } - public HibernateTypeDescriptor getHibernateTypeDescriptor() { - return hibernateTypeDescriptor; + public String getTypeName() { + return typeName; + } + + public Map getTypeParameters() { + return typeParameters; } public boolean isLazy() { @@ -227,15 +257,15 @@ public class HbmSimpleAttributeDomainState extends AbstractHbmAttributeDomainSta return isInsertable; } - public boolean isUpdateable() { - return isUpdateable; + public boolean isUpdatable() { + return isUpdatable; } public String getCascade() { return null; } - public boolean isKeyCasadeDeleteEnabled() { + public boolean isKeyCascadeDeleteEnabled() { //TODO: implement return false; } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmColumnRelationalState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmColumnRelationalState.java index 7cd3526829..53b2b222bf 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmColumnRelationalState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmColumnRelationalState.java @@ -35,8 +35,8 @@ import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLCla import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLVersion; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLManyToOneElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLPropertyElement; -import org.hibernate.metamodel.source.util.MappingHelper; -import org.hibernate.metamodel.state.relational.ColumnRelationalState; +import org.hibernate.metamodel.source.hbm.util.MappingHelper; +import org.hibernate.metamodel.relational.state.ColumnRelationalState; // TODO: remove duplication after Id, Discriminator, Version, Timestamp, and Property extend a common interface. diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmDerivedValueRelationalState.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmDerivedValueRelationalState.java index 8c92cb6189..f160d074ee 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmDerivedValueRelationalState.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmDerivedValueRelationalState.java @@ -23,7 +23,7 @@ */ package org.hibernate.metamodel.source.hbm.state.relational; -import org.hibernate.metamodel.state.relational.DerivedValueRelationalState; +import org.hibernate.metamodel.relational.state.DerivedValueRelationalState; /** * @author Gail Badner diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmManyToOneRelationalStateContainer.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmManyToOneRelationalStateContainer.java index b6f4032dd9..75af93de1c 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmManyToOneRelationalStateContainer.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmManyToOneRelationalStateContainer.java @@ -23,9 +23,9 @@ */ package org.hibernate.metamodel.source.hbm.state.relational; -import org.hibernate.metamodel.binding.MappingDefaults; +import org.hibernate.metamodel.source.hbm.MappingDefaults; +import org.hibernate.metamodel.relational.state.ManyToOneRelationalState; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLManyToOneElement; -import org.hibernate.metamodel.state.relational.ManyToOneRelationalState; /** * @author Gail Badner diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmSimpleValueRelationalStateContainer.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmSimpleValueRelationalStateContainer.java index c20b811b64..74fc36f239 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmSimpleValueRelationalStateContainer.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/state/relational/HbmSimpleValueRelationalStateContainer.java @@ -31,7 +31,9 @@ import java.util.Set; import org.hibernate.MappingException; import org.hibernate.cfg.NamingStrategy; import org.hibernate.metamodel.binding.HibernateTypeDescriptor; -import org.hibernate.metamodel.binding.MappingDefaults; +import org.hibernate.metamodel.source.hbm.MappingDefaults; +import org.hibernate.metamodel.relational.state.SimpleValueRelationalState; +import org.hibernate.metamodel.relational.state.TupleRelationalState; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLColumnElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLDiscriminator; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLId; @@ -39,8 +41,6 @@ import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLCla import org.hibernate.metamodel.source.hbm.xml.mapping.XMLHibernateMapping.XMLClass.XMLVersion; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLManyToOneElement; import org.hibernate.metamodel.source.hbm.xml.mapping.XMLPropertyElement; -import org.hibernate.metamodel.state.relational.SimpleValueRelationalState; -import org.hibernate.metamodel.state.relational.TupleRelationalState; /** * @author Gail Badner diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/MappingHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/util/MappingHelper.java similarity index 76% rename from hibernate-core/src/main/java/org/hibernate/metamodel/source/util/MappingHelper.java rename to hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/util/MappingHelper.java index 9ad0172a85..cc4864b7e6 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/MappingHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/util/MappingHelper.java @@ -21,13 +21,18 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.util; +package org.hibernate.metamodel.source.hbm.util; import java.util.Collections; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; +import org.hibernate.MappingException; +import org.hibernate.service.ServiceRegistry; +import org.hibernate.service.classloading.spi.ClassLoaderService; +import org.hibernate.service.classloading.spi.ClassLoadingException; + /** * Helper class. * @@ -66,4 +71,17 @@ public class MappingHelper { return tokens; } } + + public static Class classForName(String className, ServiceRegistry serviceRegistry) { + ClassLoaderService classLoaderService = serviceRegistry.getService( ClassLoaderService.class ); + try { + return classLoaderService.classForName( className ); + } + catch ( ClassLoadingException e ) { + throw new MappingException( + "Could not find class: " + + className + ); + } + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/JavaClassNameResolver.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/JavaClassNameResolver.java new file mode 100644 index 0000000000..69428f8766 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/JavaClassNameResolver.java @@ -0,0 +1,70 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.source.internal; + +import org.hibernate.MappingException; +import org.hibernate.metamodel.binding.EntityBinding; +import org.hibernate.metamodel.domain.Attribute; +import org.hibernate.metamodel.domain.Entity; +import org.hibernate.metamodel.domain.SingularAttribute; +import org.hibernate.metamodel.source.spi.MetadataImplementor; + +/** + * @author Gail Badner + */ +public class JavaClassNameResolver { + private final MetadataImplementor metadata; + + JavaClassNameResolver(MetadataImplementor metadata) { + this.metadata = metadata; + } + + public void resolve(EntityBinding entityBinding) { + // TODO: this only deals w/ POJO... + Entity entity = entityBinding.getEntity(); + if ( entity == null ) { + throw new MappingException( + "Cannot resolve Java type names because the domain model has not been bound to EntityBinding." ); + } + String entityClassName = + entity.getPojoEntitySpecifics().getClassName() != null ? + entity.getPojoEntitySpecifics().getClassName() : + entity.getPojoEntitySpecifics().getProxyInterfaceName(); + if ( entityClassName == null ) { + throw new MappingException( "No Java class or interface defined for: " + entityBinding.getEntity().getName() ); + } + + for ( Attribute attribute : entity.getAttributes() ) { + if ( attribute.isSingular() ) { + SingularAttribute singularAttribute = SingularAttribute.class.cast( attribute ); + if ( singularAttribute.getSingularAttributeType().getName() == null ) { + + } + } + } + + } + + +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataBuilderImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataBuilderImpl.java index ff15136673..ffedddea1b 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataBuilderImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataBuilderImpl.java @@ -25,63 +25,121 @@ package org.hibernate.metamodel.source.internal; import javax.persistence.SharedCacheMode; +import org.hibernate.cache.spi.access.AccessType; +import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.EJB3NamingStrategy; import org.hibernate.cfg.NamingStrategy; import org.hibernate.metamodel.Metadata; import org.hibernate.metamodel.MetadataBuilder; import org.hibernate.metamodel.MetadataSources; import org.hibernate.metamodel.SourceProcessingOrder; +import org.hibernate.service.BasicServiceRegistry; +import org.hibernate.service.config.spi.ConfigurationService; /** * @author Steve Ebersole */ public class MetadataBuilderImpl implements MetadataBuilder { private final MetadataSources sources; - - private NamingStrategy namingStrategy = EJB3NamingStrategy.INSTANCE; - private SourceProcessingOrder sourceProcessingOrder = SourceProcessingOrder.HBM_FIRST; - private SharedCacheMode sharedCacheMode = SharedCacheMode.ENABLE_SELECTIVE; + private final OptionsImpl options; public MetadataBuilderImpl(MetadataSources sources) { this.sources = sources; - } - - public MetadataSources getSources() { - return sources; - } - - public NamingStrategy getNamingStrategy() { - return namingStrategy; - } - - public SharedCacheMode getSharedCacheMode() { - return sharedCacheMode; - } - - public SourceProcessingOrder getSourceProcessingOrder() { - return sourceProcessingOrder; + this.options = new OptionsImpl( sources.getServiceRegistry() ); } @Override public MetadataBuilder with(NamingStrategy namingStrategy) { - this.namingStrategy = namingStrategy; + this.options.namingStrategy = namingStrategy; return this; } @Override public MetadataBuilder with(SourceProcessingOrder sourceProcessingOrder) { - this.sourceProcessingOrder = sourceProcessingOrder; + this.options.sourceProcessingOrder = sourceProcessingOrder; return this; } @Override public MetadataBuilder with(SharedCacheMode sharedCacheMode) { - this.sharedCacheMode = sharedCacheMode; + this.options.sharedCacheMode = sharedCacheMode; + return this; + } + + @Override + public MetadataBuilder with(AccessType accessType) { + this.options.defaultCacheAccessType = accessType; + return this; + } + + @Override + public MetadataBuilder withNewIdentifierGeneratorsEnabled(boolean enabled) { + this.options.useNewIdentifierGenerators = enabled; return this; } @Override public Metadata buildMetadata() { - return new MetadataImpl( this ); + return new MetadataImpl( sources, options ); } + + private static class OptionsImpl implements Metadata.Options { + private SourceProcessingOrder sourceProcessingOrder = SourceProcessingOrder.HBM_FIRST; + private NamingStrategy namingStrategy = EJB3NamingStrategy.INSTANCE; + private SharedCacheMode sharedCacheMode = SharedCacheMode.ENABLE_SELECTIVE; + private AccessType defaultCacheAccessType; + private boolean useNewIdentifierGenerators; + + public OptionsImpl(BasicServiceRegistry serviceRegistry) { + ConfigurationService configService = serviceRegistry.getService( ConfigurationService.class ); + + // cache access type + defaultCacheAccessType = configService.getSetting( + AvailableSettings.DEFAULT_CACHE_CONCURRENCY_STRATEGY, + new ConfigurationService.Converter() { + @Override + public AccessType convert(Object value) { + return AccessType.fromExternalName( value.toString() ); + } + } + ); + + useNewIdentifierGenerators = configService.getSetting( + AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS, + new ConfigurationService.Converter() { + @Override + public Boolean convert(Object value) { + return Boolean.parseBoolean( value.toString() ); + } + }, + false + ); + } + + + @Override + public SourceProcessingOrder getSourceProcessingOrder() { + return sourceProcessingOrder; + } + + @Override + public NamingStrategy getNamingStrategy() { + return namingStrategy; + } + + @Override + public AccessType getDefaultAccessType() { + return defaultCacheAccessType; + } + + @Override + public SharedCacheMode getSharedCacheMode() { + return sharedCacheMode; + } + + @Override + public boolean useNewIdentifierGenerators() { + return useNewIdentifierGenerators; + } + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataImpl.java index 05e9e50e8a..09005ba8c4 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/internal/MetadataImpl.java @@ -30,7 +30,6 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import javax.persistence.SharedCacheMode; import org.jboss.jandex.Index; import org.jboss.jandex.Indexer; @@ -38,7 +37,7 @@ import org.jboss.logging.Logger; import org.hibernate.DuplicateMappingException; import org.hibernate.HibernateException; -import org.hibernate.cfg.NamingStrategy; +import org.hibernate.SessionFactory; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.mapping.MetadataSource; import org.hibernate.metamodel.Metadata; @@ -47,6 +46,7 @@ import org.hibernate.metamodel.SourceProcessingOrder; import org.hibernate.metamodel.binding.EntityBinding; import org.hibernate.metamodel.binding.FetchProfile; import org.hibernate.metamodel.binding.PluralAttributeBinding; +import org.hibernate.metamodel.binding.TypeDef; import org.hibernate.metamodel.relational.Database; import org.hibernate.metamodel.source.annotation.xml.XMLEntityMappings; import org.hibernate.metamodel.source.annotations.AnnotationBinder; @@ -58,7 +58,7 @@ import org.hibernate.service.BasicServiceRegistry; import org.hibernate.service.classloading.spi.ClassLoaderService; /** - * Container for configuration data while building and binding the metamodel + * Container for configuration data collected during binding the metamodel. * * @author Steve Ebersole * @author Hardy Ferentschik @@ -69,24 +69,25 @@ public class MetadataImpl implements Metadata, MetadataImplementor, Serializable ); private final BasicServiceRegistry serviceRegistry; - private final NamingStrategy namingStrategy; - private final SharedCacheMode sharedCacheMode; + private final Options options; + private final Database database = new Database(); + /** + * Maps the fully qualified class name of an entity to its entity binding + */ private Map entityBindingMap = new HashMap(); private Map collectionBindingMap = new HashMap(); private Map fetchProfiles = new HashMap(); + private Map typeDefs = new HashMap(); private Map imports; - public MetadataImpl(MetadataBuilderImpl builder) { - final MetadataSources metadataSources = builder.getSources(); - + public MetadataImpl(MetadataSources metadataSources, Options options) { this.serviceRegistry = metadataSources.getServiceRegistry(); - this.namingStrategy = builder.getNamingStrategy(); - this.sharedCacheMode = builder.getSharedCacheMode(); + this.options = options; final ArrayList processedEntityNames = new ArrayList(); - if ( builder.getSourceProcessingOrder() == SourceProcessingOrder.HBM_FIRST ) { + if ( options.getSourceProcessingOrder() == SourceProcessingOrder.HBM_FIRST ) { applyHibernateMappings( metadataSources, processedEntityNames ); applyAnnotationMappings( metadataSources, processedEntityNames ); } @@ -132,8 +133,8 @@ public class MetadataImpl implements Metadata, MetadataImplementor, Serializable index = ormParser.parseAndUpdateIndex( mappings, index ); // create the annotation binder and pass it the final annotation index - final AnnotationBinder annotationBinder = new AnnotationBinder( this ); - annotationBinder.bind( index ); + final AnnotationBinder annotationBinder = new AnnotationBinder( this, index ); + annotationBinder.bind(); } /** @@ -153,22 +154,27 @@ public class MetadataImpl implements Metadata, MetadataImplementor, Serializable } } + @Override + public Options getOptions() { + return options; + } + + @Override + public SessionFactory buildSessionFactory() { + // todo : implement!!!! + return null; + } + + @Override public BasicServiceRegistry getServiceRegistry() { return serviceRegistry; } + @Override public Database getDatabase() { return database; } - public NamingStrategy getNamingStrategy() { - return namingStrategy; - } - - public SharedCacheMode getSharedCacheMode() { - return sharedCacheMode; - } - public EntityBinding getEntityBinding(String entityName) { return entityBindingMap.get( entityName ); } @@ -218,6 +224,15 @@ public class MetadataImpl implements Metadata, MetadataImplementor, Serializable return fetchProfiles.values(); } + public void addTypeDef(String name, TypeDef typeDef) { + // TODO - should we check whether the typedef already exists? Log it? Exception? (HF) + typeDefs.put( name, typeDef ); + } + + public TypeDef getTypeDef(String name) { + return typeDefs.get( name ); + } + public FetchProfile findOrCreateFetchProfile(String profileName, MetadataSource source) { FetchProfile profile = fetchProfiles.get( profileName ); if ( profile == null ) { diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/spi/MetadataImplementor.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/spi/MetadataImplementor.java index cc4b3194f2..291f4bf888 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/spi/MetadataImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/spi/MetadataImplementor.java @@ -23,17 +23,29 @@ */ package org.hibernate.metamodel.source.spi; +import org.hibernate.mapping.MetadataSource; +import org.hibernate.metamodel.Metadata; import org.hibernate.metamodel.binding.EntityBinding; +import org.hibernate.metamodel.binding.FetchProfile; +import org.hibernate.metamodel.binding.PluralAttributeBinding; import org.hibernate.metamodel.relational.Database; import org.hibernate.service.BasicServiceRegistry; /** * @author Steve Ebersole */ -public interface MetadataImplementor { +public interface MetadataImplementor extends Metadata { public BasicServiceRegistry getServiceRegistry(); public Database getDatabase(); public Iterable getEntityBindings(); public EntityBinding getEntityBinding(String entityName); + + public void addImport(String entityName, String entityName1); + + public void addEntity(EntityBinding entityBinding); + + public void addCollection(PluralAttributeBinding collectionBinding); + + public FetchProfile findOrCreateFetchProfile(String profileName, MetadataSource hbm); } diff --git a/hibernate-core/src/main/java/org/hibernate/service/BasicServiceRegistry.java b/hibernate-core/src/main/java/org/hibernate/service/BasicServiceRegistry.java index 56d66cda9d..dc6bf9aae0 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/BasicServiceRegistry.java +++ b/hibernate-core/src/main/java/org/hibernate/service/BasicServiceRegistry.java @@ -23,11 +23,8 @@ */ package org.hibernate.service; -import org.hibernate.service.spi.BasicServiceInitiator; - /** * @author Steve Ebersole */ public interface BasicServiceRegistry extends ServiceRegistry { - public void registerServiceInitiator(BasicServiceInitiator initiator); } diff --git a/hibernate-core/src/main/java/org/hibernate/service/StandardServiceInitiators.java b/hibernate-core/src/main/java/org/hibernate/service/StandardServiceInitiators.java index 3aad4c47ad..38e05815c7 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/StandardServiceInitiators.java +++ b/hibernate-core/src/main/java/org/hibernate/service/StandardServiceInitiators.java @@ -34,6 +34,7 @@ import org.hibernate.integrator.internal.IntegratorServiceInitiator; import org.hibernate.persister.internal.PersisterClassResolverInitiator; import org.hibernate.persister.internal.PersisterFactoryInitiator; import org.hibernate.service.classloading.internal.ClassLoaderServiceInitiator; +import org.hibernate.service.config.internal.ConfigurationServiceInitiator; import org.hibernate.service.internal.SessionFactoryServiceRegistryFactoryInitiator; import org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator; import org.hibernate.service.jdbc.connections.internal.MultiTenantConnectionProviderInitiator; @@ -53,6 +54,8 @@ public class StandardServiceInitiators { private static List buildStandardServiceInitiatorList() { final List serviceInitiators = new ArrayList(); + serviceInitiators.add( ConfigurationServiceInitiator.INSTANCE ); + serviceInitiators.add( ClassLoaderServiceInitiator.INSTANCE ); serviceInitiators.add( JndiServiceInitiator.INSTANCE ); serviceInitiators.add( JmxServiceInitiator.INSTANCE ); diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyTargetSource.java b/hibernate-core/src/main/java/org/hibernate/service/config/internal/ConfigurationServiceImpl.java similarity index 56% rename from hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyTargetSource.java rename to hibernate-core/src/main/java/org/hibernate/service/config/internal/ConfigurationServiceImpl.java index 45419fa2f6..0bbe388748 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyTargetSource.java +++ b/hibernate-core/src/main/java/org/hibernate/service/config/internal/ConfigurationServiceImpl.java @@ -21,26 +21,41 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.service.spi.proxy; +package org.hibernate.service.config.internal; -import org.hibernate.service.Service; -import org.hibernate.service.ServiceRegistry; +import java.util.Collections; +import java.util.Map; + +import org.hibernate.service.config.spi.ConfigurationService; /** - * Additional contract for service proxies. This allows the proxies access to their actual service instances. - * * @author Steve Ebersole */ -public interface ServiceProxyTargetSource extends ServiceRegistry { - /** - * Retrieve a service by role. Unlike {@link ServiceRegistry#getService}, this version will never return a proxy. - * - * @param serviceRole The service role - * @param The service role type - * - * @return The requested service. - * - * @throws org.hibernate.service.UnknownServiceException Indicates the service was not known. - */ - public R getServiceInternal(Class serviceRole); +public class ConfigurationServiceImpl implements ConfigurationService { + private final Map settings; + + @SuppressWarnings( "unchecked" ) + public ConfigurationServiceImpl(Map settings) { + this.settings = Collections.unmodifiableMap( settings ); + } + + @Override + public Map getSettings() { + return settings; + } + + @Override + public T getSetting(String name, Converter converter) { + return getSetting( name, converter, null ); + } + + @Override + public T getSetting(String name, Converter converter, T defaultValue) { + final Object value = settings.get( name ); + if ( value == null ) { + return defaultValue; + } + + return converter.convert( value ); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/service/internal/proxy/javassist/ServiceProxyFactoryFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/service/config/internal/ConfigurationServiceInitiator.java similarity index 60% rename from hibernate-core/src/main/java/org/hibernate/service/internal/proxy/javassist/ServiceProxyFactoryFactoryImpl.java rename to hibernate-core/src/main/java/org/hibernate/service/config/internal/ConfigurationServiceInitiator.java index 4d7f46a583..610734ca8f 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/internal/proxy/javassist/ServiceProxyFactoryFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/service/config/internal/ConfigurationServiceInitiator.java @@ -21,20 +21,26 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.service.internal.proxy.javassist; +package org.hibernate.service.config.internal; -import org.hibernate.service.spi.proxy.ServiceProxyFactory; -import org.hibernate.service.spi.proxy.ServiceProxyFactoryFactory; -import org.hibernate.service.spi.proxy.ServiceProxyTargetSource; +import java.util.Map; + +import org.hibernate.service.config.spi.ConfigurationService; +import org.hibernate.service.spi.BasicServiceInitiator; +import org.hibernate.service.spi.ServiceRegistryImplementor; /** - * Javassist-based implementation of a {@link ServiceProxyFactoryFactory} - * * @author Steve Ebersole */ -public class ServiceProxyFactoryFactoryImpl implements ServiceProxyFactoryFactory { +public class ConfigurationServiceInitiator implements BasicServiceInitiator { + public static final ConfigurationServiceInitiator INSTANCE = new ConfigurationServiceInitiator(); + + public ConfigurationService initiateService(Map configurationValues, ServiceRegistryImplementor registry) { + return new ConfigurationServiceImpl( configurationValues ); + } + @Override - public ServiceProxyFactory makeServiceProxyFactory(ServiceProxyTargetSource registry) { - return new ServiceProxyFactoryImpl( registry ); + public Class getServiceInitiated() { + return ConfigurationService.class; } } diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyFactory.java b/hibernate-core/src/main/java/org/hibernate/service/config/spi/ConfigurationService.java similarity index 71% rename from hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyFactory.java rename to hibernate-core/src/main/java/org/hibernate/service/config/spi/ConfigurationService.java index 9d81709a1e..815a44bdd8 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/service/config/spi/ConfigurationService.java @@ -21,23 +21,24 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.service.spi.proxy; +package org.hibernate.service.config.spi; + +import java.util.Map; import org.hibernate.service.Service; /** - * Contract for creating proxy instances for {@link Service} instances. + * Provides access to the initial user-provided configuration values * * @author Steve Ebersole */ -public interface ServiceProxyFactory { - /** - * Create a proxy for the given service role. - * - * @param serviceRole The service role for which to create a proxy. - * @param The type of the service - * - * @return The service proxy - */ - public T makeProxy(Class serviceRole); +public interface ConfigurationService extends Service { + public Map getSettings(); + + public T getSetting(String name, Converter converter); + public T getSetting(String name, Converter converter, T defaultValue); + + public static interface Converter { + public T convert(Object value); + } } diff --git a/hibernate-core/src/main/java/org/hibernate/service/internal/AbstractServiceRegistryImpl.java b/hibernate-core/src/main/java/org/hibernate/service/internal/AbstractServiceRegistryImpl.java index 3bce73e8fd..55f7382f2e 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/internal/AbstractServiceRegistryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/service/internal/AbstractServiceRegistryImpl.java @@ -31,32 +31,29 @@ import java.util.concurrent.ConcurrentHashMap; import org.jboss.logging.Logger; -import org.hibernate.HibernateException; import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.service.Service; import org.hibernate.service.ServiceRegistry; -import org.hibernate.service.internal.proxy.javassist.ServiceProxyFactoryFactoryImpl; +import org.hibernate.service.UnknownServiceException; import org.hibernate.service.jmx.spi.JmxService; import org.hibernate.service.spi.InjectService; import org.hibernate.service.spi.Manageable; +import org.hibernate.service.spi.ServiceBinding; +import org.hibernate.service.spi.ServiceException; +import org.hibernate.service.spi.ServiceInitiator; import org.hibernate.service.spi.ServiceRegistryImplementor; import org.hibernate.service.spi.Startable; import org.hibernate.service.spi.Stoppable; -import org.hibernate.service.UnknownServiceException; -import org.hibernate.service.spi.proxy.ServiceProxyFactory; /** * @author Steve Ebersole */ -public abstract class AbstractServiceRegistryImpl implements ServiceRegistryImplementor { +public abstract class AbstractServiceRegistryImpl implements ServiceRegistryImplementor, ServiceBinding.OwningRegistry { private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, AbstractServiceRegistryImpl.class.getName() ); private final ServiceRegistryImplementor parent; - // for now just hard-code the javassist factory - private ServiceProxyFactory serviceProxyFactory = new ServiceProxyFactoryFactoryImpl().makeServiceProxyFactory( this ); - private ConcurrentHashMap serviceBindingMap; // IMPL NOTE : the list used for ordered destruction. Cannot used map above because we need to // iterate it in reverse order which is only available through ListIterator @@ -73,6 +70,20 @@ public abstract class AbstractServiceRegistryImpl implements ServiceRegistryImpl this.serviceList = CollectionHelper.arrayList( 20 ); } + @SuppressWarnings({ "unchecked" }) + protected void createServiceBinding(ServiceInitiator initiator) { + serviceBindingMap.put( initiator.getServiceInitiated(), new ServiceBinding( this, initiator ) ); + } + + protected void createServiceBinding(ProvidedService providedService) { + ServiceBinding binding = locateServiceBinding( providedService.getServiceRole(), false ); + if ( binding == null ) { + binding = new ServiceBinding( this, providedService.getServiceRole(), providedService.getService() ); + serviceBindingMap.put( providedService.getServiceRole(), binding ); + } + registerService( binding, providedService.getService() ); + } + @Override @SuppressWarnings( {"unchecked"}) public ServiceRegistry getParentServiceRegistry() { @@ -97,40 +108,33 @@ public abstract class AbstractServiceRegistryImpl implements ServiceRegistryImpl @Override public R getService(Class serviceRole) { - return locateOrCreateServiceBinding( serviceRole, true ).getProxy(); - } - - @SuppressWarnings({ "unchecked" }) - protected ServiceBinding locateOrCreateServiceBinding(Class serviceRole, boolean checkParent) { - ServiceBinding serviceBinding = locateServiceBinding( serviceRole, checkParent ); + final ServiceBinding serviceBinding = locateServiceBinding( serviceRole ); if ( serviceBinding == null ) { - createServiceBinding( serviceRole ); + throw new UnknownServiceException( serviceRole ); } - return serviceBinding; + + R service = serviceBinding.getService(); + if ( service == null ) { + service = initializeService( serviceBinding ); + } + + return service; } - protected ServiceBinding createServiceBinding(Class serviceRole) { - R proxy = serviceProxyFactory.makeProxy( serviceRole ); - ServiceBinding serviceBinding = new ServiceBinding( proxy ); - serviceBindingMap.put( serviceRole, serviceBinding ); - return serviceBinding; - } - - protected void registerService(Class serviceRole, R service) { - ServiceBinding serviceBinding = locateOrCreateServiceBinding( serviceRole, false ); - R priorServiceInstance = serviceBinding.getTarget(); - serviceBinding.setTarget( service ); + protected void registerService(ServiceBinding serviceBinding, R service) { + R priorServiceInstance = serviceBinding.getService(); + serviceBinding.setService( service ); if ( priorServiceInstance != null ) { serviceList.remove( priorServiceInstance ); } serviceList.add( service ); } - private R initializeService(Class serviceRole) { - LOG.trace("Initializing service [role=" + serviceRole.getName() + "]"); + private R initializeService(ServiceBinding serviceBinding) { + LOG.trace( "Initializing service [role=" + serviceBinding.getServiceRole().getName() + "]" ); // PHASE 1 : create service - R service = createService( serviceRole ); + R service = createService( serviceBinding ); if ( service == null ) { return null; } @@ -139,12 +143,34 @@ public abstract class AbstractServiceRegistryImpl implements ServiceRegistryImpl configureService( service ); // PHASE 3 : Start service - startService( service, serviceRole ); + startService( serviceBinding ); return service; } - protected abstract T createService(Class serviceRole); + @SuppressWarnings( {"unchecked"}) + protected R createService(ServiceBinding serviceBinding) { + final ServiceInitiator serviceInitiator = serviceBinding.getServiceInitiator(); + if ( serviceInitiator == null ) { + // this condition should never ever occur + throw new UnknownServiceException( serviceBinding.getServiceRole() ); + } + + try { + R service = serviceBinding.getServiceRegistry().initiateService( serviceInitiator ); + // IMPL NOTE : the register call here is important to avoid potential stack overflow issues + // from recursive calls through #configureService + registerService( serviceBinding, service ); + return service; + } + catch ( ServiceException e ) { + throw e; + } + catch ( Exception e ) { + throw new ServiceException( "Unable to create requested service [" + serviceBinding.getServiceRole().getName() + "]", e ); + } + } + protected abstract void configureService(T service); protected void applyInjections(T service) { @@ -197,36 +223,19 @@ public abstract class AbstractServiceRegistryImpl implements ServiceRegistryImpl } @SuppressWarnings({ "unchecked" }) - protected void startService(T service, Class serviceRole) { - if ( Startable.class.isInstance( service ) ) { - ( (Startable) service ).start(); + protected void startService(ServiceBinding serviceBinding) { + if ( Startable.class.isInstance( serviceBinding.getService() ) ) { + ( (Startable) serviceBinding.getService() ).start(); } - if ( Manageable.class.isInstance( service ) ) { - getService( JmxService.class ).registerService( (Manageable) service, serviceRole ); + if ( Manageable.class.isInstance( serviceBinding.getService() ) ) { + getService( JmxService.class ).registerService( + (Manageable) serviceBinding.getService(), + serviceBinding.getServiceRole() + ); } } - @Override - @SuppressWarnings( {"unchecked"}) - public R getServiceInternal(Class serviceRole) { - // this call comes from the binding proxy, we most definitely do not want to look up into the parent - // in this case! - ServiceBinding serviceBinding = locateServiceBinding( serviceRole, false ); - if ( serviceBinding == null ) { - throw new HibernateException( "Only proxies should invoke #getServiceInternal" ); - } - R service = serviceBinding.getTarget(); - if ( service == null ) { - service = initializeService( serviceRole ); - serviceBinding.setTarget( service ); - } - if ( service == null ) { - throw new UnknownServiceException( serviceRole ); - } - return service; - } - public void destroy() { ListIterator serviceIterator = serviceList.listIterator( serviceList.size() ); while ( serviceIterator.hasPrevious() ) { diff --git a/hibernate-core/src/main/java/org/hibernate/service/internal/BasicServiceRegistryImpl.java b/hibernate-core/src/main/java/org/hibernate/service/internal/BasicServiceRegistryImpl.java index 58c22ba0da..e34eb5aada 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/internal/BasicServiceRegistryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/service/internal/BasicServiceRegistryImpl.java @@ -23,19 +23,14 @@ */ package org.hibernate.service.internal; -import java.util.HashMap; import java.util.List; import java.util.Map; -import org.jboss.logging.Logger; - -import org.hibernate.internal.CoreMessageLogger; import org.hibernate.service.BasicServiceRegistry; import org.hibernate.service.Service; -import org.hibernate.service.UnknownServiceException; import org.hibernate.service.spi.BasicServiceInitiator; import org.hibernate.service.spi.Configurable; -import org.hibernate.service.spi.ServiceException; +import org.hibernate.service.spi.ServiceInitiator; import org.hibernate.service.spi.ServiceRegistryAwareService; /** @@ -44,86 +39,33 @@ import org.hibernate.service.spi.ServiceRegistryAwareService; * @author Steve Ebersole */ public class BasicServiceRegistryImpl extends AbstractServiceRegistryImpl implements BasicServiceRegistry { - private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, BasicServiceRegistryImpl.class.getName()); - private final Map serviceInitiatorMap; private final Map configurationValues; @SuppressWarnings( {"unchecked"}) public BasicServiceRegistryImpl( - List serviceInitiators, - List providedServices, - Map configurationValues) { + final List serviceInitiators, + final List providedServices, + final Map configurationValues) { super(); this.configurationValues = configurationValues; - this.serviceInitiatorMap = toMap( serviceInitiators ); - for ( BasicServiceInitiator initiator : serviceInitiatorMap.values() ) { - // create the bindings up front to help identify to which registry services belong - createServiceBinding( initiator.getServiceInitiated() ); + // process initiators + for ( ServiceInitiator initiator : serviceInitiators ) { + createServiceBinding( initiator ); } + // then, explicitly provided service instances for ( ProvidedService providedService : providedServices ) { - ServiceBinding binding = locateOrCreateServiceBinding( providedService.getServiceRole(), false ); - binding.setTarget( providedService.getService() ); - } - } - - /** - * We convert the incoming list of initiators to a map for 2 reasons:
    - *
  • to make it easier to look up the initiator we need for a given service role
  • - *
  • to make sure there is only one initiator for a given service role (last wins)
  • - *
- * - * @param serviceInitiators The list of individual initiators - * - * @return The map of initiators keyed by the service rle they initiate. - */ - private static Map toMap(List serviceInitiators) { - final Map result = new HashMap(); - for ( BasicServiceInitiator initiator : serviceInitiators ) { - result.put( initiator.getServiceInitiated(), initiator ); - } - return result; - } - - @Override - @SuppressWarnings( {"unchecked"}) - public void registerServiceInitiator(BasicServiceInitiator initiator) { - ServiceBinding serviceBinding = locateServiceBinding( initiator.getServiceInitiated(), false ); - if ( serviceBinding != null ) { - serviceBinding.setTarget( null ); - } - else { - createServiceBinding( initiator.getServiceInitiated() ); - } - final Object previous = serviceInitiatorMap.put( initiator.getServiceInitiated(), initiator ); - if ( previous != null ) { - LOG.debugf( "Over-wrote existing service initiator [role=%s]", initiator.getServiceInitiated().getName() ); + createServiceBinding( providedService ); } } @Override - @SuppressWarnings({ "unchecked" }) - protected T createService(Class serviceRole) { - BasicServiceInitiator initiator = serviceInitiatorMap.get( serviceRole ); - if ( initiator == null ) { - throw new UnknownServiceException( serviceRole ); - } - try { - T service = initiator.initiateService( configurationValues, this ); - // IMPL NOTE : the register call here is important to avoid potential stack overflow issues - // from recursive calls through #configureService - registerService( serviceRole, service ); - return service; - } - catch ( ServiceException e ) { - throw e; - } - catch ( Exception e ) { - throw new ServiceException( "Unable to create requested service [" + serviceRole.getName() + "]", e ); - } + public R initiateService(ServiceInitiator serviceInitiator) { + // todo : add check/error for unexpected initiator types? + return ( (BasicServiceInitiator) serviceInitiator ).initiateService( configurationValues, this ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/service/internal/ProvidedService.java b/hibernate-core/src/main/java/org/hibernate/service/internal/ProvidedService.java index 74a2fd92dc..d0cf4d1348 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/internal/ProvidedService.java +++ b/hibernate-core/src/main/java/org/hibernate/service/internal/ProvidedService.java @@ -28,20 +28,20 @@ package org.hibernate.service.internal; * * @author Steve Ebersole */ -public class ProvidedService { - private final Class serviceRole; - private final T service; +public class ProvidedService { + private final Class serviceRole; + private final R service; - public ProvidedService(Class serviceRole, T service) { + public ProvidedService(Class serviceRole, R service) { this.serviceRole = serviceRole; this.service = service; } - public Class getServiceRole() { + public Class getServiceRole() { return serviceRole; } - public T getService() { + public R getService() { return service; } } diff --git a/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryImpl.java b/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryImpl.java index 311f6b911b..45199c861d 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/service/internal/SessionFactoryServiceRegistryImpl.java @@ -23,19 +23,11 @@ */ package org.hibernate.service.internal; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.jboss.logging.Logger; - -import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.internal.CoreMessageLogger; import org.hibernate.cfg.Configuration; +import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.service.Service; import org.hibernate.service.StandardSessionFactoryServiceInitiators; -import org.hibernate.service.UnknownServiceException; -import org.hibernate.service.spi.ServiceException; +import org.hibernate.service.spi.ServiceInitiator; import org.hibernate.service.spi.ServiceRegistryAwareService; import org.hibernate.service.spi.ServiceRegistryImplementor; import org.hibernate.service.spi.SessionFactoryServiceInitiator; @@ -44,13 +36,7 @@ import org.hibernate.service.spi.SessionFactoryServiceRegistry; /** * @author Steve Ebersole */ -public class SessionFactoryServiceRegistryImpl - extends AbstractServiceRegistryImpl - implements SessionFactoryServiceRegistry { - - private static final CoreMessageLogger LOG = Logger.getMessageLogger( CoreMessageLogger.class, SessionFactoryServiceRegistryImpl.class.getName() ); - - private final Map serviceInitiatorMap; +public class SessionFactoryServiceRegistryImpl extends AbstractServiceRegistryImpl implements SessionFactoryServiceRegistry { // for now we need to hold on to the Configuration... :( private Configuration configuration; @@ -62,62 +48,21 @@ public class SessionFactoryServiceRegistryImpl SessionFactoryImplementor sessionFactory, Configuration configuration) { super( parent ); - // for now, just use the standard initiator list - this.serviceInitiatorMap = toMap( StandardSessionFactoryServiceInitiators.LIST ); this.sessionFactory = sessionFactory; this.configuration = configuration; - for ( SessionFactoryServiceInitiator initiator : serviceInitiatorMap.values() ) { + // for now, just use the standard initiator list + for ( SessionFactoryServiceInitiator initiator : StandardSessionFactoryServiceInitiators.LIST ) { // create the bindings up front to help identify to which registry services belong - createServiceBinding( initiator.getServiceInitiated() ); - } - } - - private static Map toMap(List serviceInitiators) { - final Map result = new HashMap(); - for ( SessionFactoryServiceInitiator initiator : serviceInitiators ) { - result.put( initiator.getServiceInitiated(), initiator ); - } - return result; - } - - @Override - @SuppressWarnings( {"unchecked"}) - public void registerServiceInitiator(SessionFactoryServiceInitiator initiator) { - ServiceBinding serviceBinding = locateServiceBinding( initiator.getServiceInitiated(), false ); - if ( serviceBinding != null ) { - serviceBinding.setTarget( null ); - } - else { - createServiceBinding( initiator.getServiceInitiated() ); - } - final Object previous = serviceInitiatorMap.put( initiator.getServiceInitiated(), initiator ); - if ( previous != null ) { - LOG.debugf( "Over-wrote existing service initiator [role=%s]", initiator.getServiceInitiated().getName() ); + createServiceBinding( initiator ); } } @Override - @SuppressWarnings({ "unchecked" }) - protected T createService(Class serviceRole) { - SessionFactoryServiceInitiator initiator = serviceInitiatorMap.get( serviceRole ); - if ( initiator == null ) { - throw new UnknownServiceException( serviceRole ); - } - try { - T service = initiator.initiateService( sessionFactory, configuration, this ); - // IMPL NOTE : the register call here is important to avoid potential stack overflow issues - // from recursive calls through #configureService - registerService( serviceRole, service ); - return service; - } - catch ( ServiceException e ) { - throw e; - } - catch ( Exception e ) { - throw new ServiceException( "Unable to create requested service [" + serviceRole.getName() + "]", e ); - } + public R initiateService(ServiceInitiator serviceInitiator) { + // todo : add check/error for unexpected initiator types? + return ( (SessionFactoryServiceInitiator) serviceInitiator ).initiateService( sessionFactory, configuration, this ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/service/internal/proxy/javassist/ServiceProxyFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/service/internal/proxy/javassist/ServiceProxyFactoryImpl.java deleted file mode 100644 index 9be3b2daa5..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/service/internal/proxy/javassist/ServiceProxyFactoryImpl.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2011, Red Hat Inc. or third-party contributors as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. All third-party contributions are - * distributed under license by Red Hat Inc. - * - * This copyrighted material is made available to anyone wishing to use, modify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ -package org.hibernate.service.internal.proxy.javassist; - -import javassist.util.proxy.MethodFilter; -import javassist.util.proxy.MethodHandler; -import javassist.util.proxy.ProxyFactory; -import javassist.util.proxy.ProxyObject; - -import org.hibernate.service.internal.ServiceProxy; -import org.hibernate.service.internal.ServiceProxyGenerationException; -import org.hibernate.service.Service; -import org.hibernate.service.spi.proxy.ServiceProxyFactory; -import org.hibernate.service.spi.proxy.ServiceProxyTargetSource; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -/** - * Javassist-based implementation of a {@link ServiceProxyFactory} - * - * @author Steve Ebersole - */ -public class ServiceProxyFactoryImpl implements ServiceProxyFactory { - private final ServiceProxyTargetSource serviceRegistry; - - public ServiceProxyFactoryImpl(ServiceProxyTargetSource serviceRegistry) { - this.serviceRegistry = serviceRegistry; - } - - private static final MethodFilter FINALIZE_FILTER = new MethodFilter() { - public boolean isHandled(Method m) { - // skip finalize methods - return !( m.getParameterTypes().length == 0 && m.getName().equals( "finalize" ) ); - } - }; - - @Override - @SuppressWarnings( {"unchecked"}) - public T makeProxy(Class serviceRole) { - try { - ProxyFactory factory = new ProxyFactory(); - factory.setFilter( FINALIZE_FILTER ); - - Class[] interfaces = new Class[2]; - interfaces[0] = serviceRole; - interfaces[1] = ServiceProxy.class; - factory.setInterfaces( interfaces ); - - Class proxyClass = factory.createClass(); - ProxyObject proxyObject = (ProxyObject) proxyClass.newInstance(); - proxyObject.setHandler( new ServiceProxyMethodInterceptor( (T)proxyObject, serviceRole, serviceRegistry ) ); - return (T) proxyObject; - } - catch (Exception e) { - throw new ServiceProxyGenerationException( "Unable to make service proxy", e ); - } - } - - private static class ServiceProxyMethodInterceptor implements MethodHandler { - private final T proxy; - private final Class serviceRole; - private final ServiceProxyTargetSource serviceRegistry; - - private ServiceProxyMethodInterceptor(T proxy, Class serviceRole, ServiceProxyTargetSource serviceRegistry) { - this.proxy = proxy; - this.serviceRole = serviceRole; - this.serviceRegistry = serviceRegistry; - } - - @Override - @SuppressWarnings( {"UnnecessaryBoxing"} ) - public Object invoke( - Object object, - Method method, - Method method1, - Object[] args) throws Exception { - String name = method.getName(); - if ( "toString".equals( name ) ) { - return serviceRole.getName() + "_$$_Proxy@" + System.identityHashCode( object ); - } - else if ( "equals".equals( name ) ) { - return proxy == object ? Boolean.TRUE : Boolean.FALSE; - } - else if ( "hashCode".equals( name ) ) { - return Integer.valueOf( System.identityHashCode( object ) ); - } - else if ( "getTargetInstance".equals( name ) && ServiceProxy.class.equals( method.getDeclaringClass() ) ) { - return serviceRegistry.getServiceInternal( serviceRole ); - } - else { - try { - T target = serviceRegistry.getServiceInternal( serviceRole ); - return method.invoke( target, args ); - } - catch (InvocationTargetException e) { - throw (Exception) e.getTargetException(); - } - } - } - } - -} diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/BasicServiceInitiator.java b/hibernate-core/src/main/java/org/hibernate/service/spi/BasicServiceInitiator.java index 1b9ed5fe15..2115ddee70 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/BasicServiceInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/service/spi/BasicServiceInitiator.java @@ -32,14 +32,7 @@ import org.hibernate.service.Service; * * @author Steve Ebersole */ -public interface BasicServiceInitiator { - /** - * Obtains the service role initiated by this initiator. Should be unique within a registry - * - * @return The service role. - */ - public Class getServiceInitiated(); - +public interface BasicServiceInitiator extends ServiceInitiator { /** * Initiates the managed service. * diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceBinding.java b/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceBinding.java new file mode 100644 index 0000000000..8d754a2684 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceBinding.java @@ -0,0 +1,81 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.service.spi; + +import org.jboss.logging.Logger; + +import org.hibernate.service.Service; + +/** + * Models a binding for a particular service + * @author Steve Ebersole + */ +public final class ServiceBinding { + private static final Logger log = Logger.getLogger( ServiceBinding.class ); + + public static interface OwningRegistry { + public R initiateService(ServiceInitiator serviceInitiator); + } + + private final OwningRegistry serviceRegistry; + private final Class serviceRole; + private final ServiceInitiator serviceInitiator; + private R service; + + public ServiceBinding(OwningRegistry serviceRegistry, Class serviceRole, R service) { + this.serviceRegistry = serviceRegistry; + this.serviceRole = serviceRole; + this.serviceInitiator = null; + this.service = service; + } + + public ServiceBinding(OwningRegistry serviceRegistry, ServiceInitiator serviceInitiator) { + this.serviceRegistry = serviceRegistry; + this.serviceRole = serviceInitiator.getServiceInitiated(); + this.serviceInitiator = serviceInitiator; + } + + public OwningRegistry getServiceRegistry() { + return serviceRegistry; + } + + public Class getServiceRole() { + return serviceRole; + } + + public ServiceInitiator getServiceInitiator() { + return serviceInitiator; + } + + public R getService() { + return service; + } + + public void setService(R service) { + if ( this.service != null ) { + log.debug( "Overriding existing service binding [" + serviceRole.getName() + "]" ); + } + this.service = service; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/state/domain/SimpleAttributeDomainState.java b/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceInitiator.java similarity index 76% rename from hibernate-core/src/main/java/org/hibernate/metamodel/state/domain/SimpleAttributeDomainState.java rename to hibernate-core/src/main/java/org/hibernate/service/spi/ServiceInitiator.java index 1a3b455fcc..40107caf06 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/state/domain/SimpleAttributeDomainState.java +++ b/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceInitiator.java @@ -21,14 +21,18 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.state.domain; +package org.hibernate.service.spi; -import org.hibernate.mapping.PropertyGeneration; -import org.hibernate.metamodel.binding.SingularAttributeBinding; +import org.hibernate.service.Service; /** - * @author Gail Badner + * @author Steve Ebersole */ -public interface SimpleAttributeDomainState extends SingularAttributeDomainState { - public PropertyGeneration getPropertyGeneration(); +public interface ServiceInitiator { + /** + * Obtains the service role initiated by this initiator. Should be unique within a registry + * + * @return The service role. + */ + public Class getServiceInitiated(); } diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceRegistryImplementor.java b/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceRegistryImplementor.java index 70179376d5..e15d6153bb 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceRegistryImplementor.java +++ b/hibernate-core/src/main/java/org/hibernate/service/spi/ServiceRegistryImplementor.java @@ -25,34 +25,11 @@ package org.hibernate.service.spi; import org.hibernate.service.Service; import org.hibernate.service.ServiceRegistry; -import org.hibernate.service.spi.proxy.ServiceProxyTargetSource; /** * @author Steve Ebersole */ -public interface ServiceRegistryImplementor extends ServiceRegistry, ServiceProxyTargetSource { +public interface ServiceRegistryImplementor extends ServiceRegistry { public ServiceBinding locateServiceBinding(Class serviceRole); - public void destroy(); - - public final class ServiceBinding { - private final R proxy; - private R target; - - public ServiceBinding(R proxy) { - this.proxy = proxy; - } - - public R getProxy() { - return proxy; - } - - public R getTarget() { - return target; - } - - public void setTarget(R target) { - this.target = target; - } - } } diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceInitiator.java b/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceInitiator.java index 5c119f38d3..8df4a41182 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceInitiator.java @@ -30,14 +30,7 @@ import org.hibernate.service.Service; /** * @author Steve Ebersole */ -public interface SessionFactoryServiceInitiator { - /** - * Obtains the service role initiated by this initiator. Should be unique within a registry - * - * @return The service role. - */ - public Class getServiceInitiated(); - +public interface SessionFactoryServiceInitiator extends ServiceInitiator{ /** * Initiates the managed service. *

diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceRegistry.java b/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceRegistry.java index 32ebfe2dd1..e2775874b6 100644 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceRegistry.java +++ b/hibernate-core/src/main/java/org/hibernate/service/spi/SessionFactoryServiceRegistry.java @@ -30,5 +30,4 @@ package org.hibernate.service.spi; * @author Steve Ebersole */ public interface SessionFactoryServiceRegistry extends ServiceRegistryImplementor { - public void registerServiceInitiator(SessionFactoryServiceInitiator initiator); } diff --git a/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyFactoryFactory.java b/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyFactoryFactory.java deleted file mode 100644 index ff638e4c01..0000000000 --- a/hibernate-core/src/main/java/org/hibernate/service/spi/proxy/ServiceProxyFactoryFactory.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * Copyright (c) 2011, Red Hat Inc. or third-party contributors as - * indicated by the @author tags or express copyright attribution - * statements applied by the authors. All third-party contributions are - * distributed under license by Red Hat Inc. - * - * This copyrighted material is made available to anyone wishing to use, modify, - * copy, or redistribute it subject to the terms and conditions of the GNU - * Lesser General Public License, as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this distribution; if not, write to: - * Free Software Foundation, Inc. - * 51 Franklin Street, Fifth Floor - * Boston, MA 02110-1301 USA - */ -package org.hibernate.service.spi.proxy; - -/** - * Contract for making a {@link ServiceProxyFactory}. - * - * @author Steve Ebersole - */ -public interface ServiceProxyFactoryFactory { - /** - * Make an instance of the service proxy factory. - * - * @param registry The registry of actual service instances - * - * @return The created service proxy factory - */ - public ServiceProxyFactory makeServiceProxyFactory(ServiceProxyTargetSource registry); -} diff --git a/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsInitiator.java b/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsInitiator.java index f01273d311..f6a81c9073 100644 --- a/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsInitiator.java +++ b/hibernate-core/src/main/java/org/hibernate/stat/internal/StatisticsInitiator.java @@ -26,9 +26,9 @@ package org.hibernate.stat.internal; import org.jboss.logging.Logger; import org.hibernate.HibernateException; +import org.hibernate.cfg.Configuration; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.internal.CoreMessageLogger; -import org.hibernate.cfg.Configuration; import org.hibernate.service.classloading.spi.ClassLoaderService; import org.hibernate.service.spi.ServiceRegistryImplementor; import org.hibernate.service.spi.SessionFactoryServiceInitiator; @@ -90,7 +90,6 @@ public class StatisticsInitiator implements SessionFactoryServiceInitiator referenceBindings = simpleEntityBinding.getAttributeBinding( "id" ) + .getEntityReferencingAttributeBindings(); + assertEquals( "There should be only one reference binding", 1, referenceBindings.size() ); + + EntityReferencingAttributeBinding referenceBinding = referenceBindings.iterator().next(); + EntityBinding referencedEntityBinding = referenceBinding.getReferencedEntityBinding(); + // TODO - Is this assertion correct (HF)? + assertEquals( "Should be the same entity binding", referencedEntityBinding, simpleEntityBinding ); + + EntityBinding entityWithManyToOneBinding = metadata.getEntityBinding( ManyToOneEntity.class.getName() ); Iterator it = entityWithManyToOneBinding.getEntityReferencingAttributeBindings() .iterator(); assertTrue( it.hasNext() ); assertSame( entityWithManyToOneBinding.getAttributeBinding( "simpleEntity" ), it.next() ); assertFalse( it.hasNext() ); } - /* - @Test - public void testEntityWithElementCollection() { - EntityBinding entityBinding = buildEntityWithElementCollectionBinding(); + public abstract MetadataImpl addSourcesForSimpleVersionedEntityBinding(MetadataSources sources); + + public abstract MetadataImpl addSourcesForSimpleEntityBinding(MetadataSources sources); + + public abstract MetadataImpl addSourcesForManyToOne(MetadataSources sources); + + protected void assertIdAndSimpleProperty(EntityBinding entityBinding) { assertNotNull( entityBinding ); assertNotNull( entityBinding.getEntityIdentifier() ); assertNotNull( entityBinding.getEntityIdentifier().getValueBinding() ); - assertNull( entityBinding.getVersioningValueBinding() ); AttributeBinding idAttributeBinding = entityBinding.getAttributeBinding( "id" ); assertNotNull( idAttributeBinding ); @@ -149,13 +136,4 @@ public abstract class AbstractBasicBindingTests extends BaseUnitTestCase { assertNotNull( nameBinding.getAttribute() ); assertNotNull( nameBinding.getValue() ); } - */ - - public abstract EntityBinding buildSimpleVersionedEntityBinding(); - - public abstract EntityBinding buildSimpleEntityBinding(); - - public abstract MetadataImplementor buildMetadataWithManyToOne(); - - //public abstract EntityBinding buildEntityWithElementCollectionBinding(); } diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicAnnotationBindingTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicAnnotationBindingTests.java index 5a0d913541..fec39ee5b7 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicAnnotationBindingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicAnnotationBindingTests.java @@ -27,8 +27,6 @@ import org.junit.Test; import org.hibernate.metamodel.MetadataSources; import org.hibernate.metamodel.source.internal.MetadataImpl; -import org.hibernate.metamodel.source.spi.MetadataImplementor; -import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.testing.FailureExpected; /** @@ -43,26 +41,20 @@ public class BasicAnnotationBindingTests extends AbstractBasicBindingTests { super.testEntityWithManyToOneMapping(); } - public EntityBinding buildSimpleEntityBinding() { - MetadataSources sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() ); + public MetadataImpl addSourcesForSimpleEntityBinding(MetadataSources sources) { sources.addAnnotatedClass( SimpleEntity.class ); - MetadataImpl metadata = (MetadataImpl) sources.buildMetadata(); + return (MetadataImpl) sources.buildMetadata(); - return metadata.getEntityBinding( SimpleEntity.class.getSimpleName() ); } - public EntityBinding buildSimpleVersionedEntityBinding() { - MetadataSources sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() ); + public MetadataImpl addSourcesForSimpleVersionedEntityBinding(MetadataSources sources) { sources.addAnnotatedClass( SimpleVersionedEntity.class ); - MetadataImpl metadata = (MetadataImpl) sources.buildMetadata(); - - return metadata.getEntityBinding( SimpleVersionedEntity.class.getSimpleName() ); + return (MetadataImpl) sources.buildMetadata(); } - public MetadataImplementor buildMetadataWithManyToOne() { - MetadataSources sources = new MetadataSources( new ServiceRegistryBuilder().buildServiceRegistry() ); - sources.addAnnotatedClass( EntityWithManyToOne.class ); - sources.addAnnotatedClass( SimpleVersionedEntity.class ); - return (MetadataImplementor) sources.buildMetadata(); + public MetadataImpl addSourcesForManyToOne(MetadataSources sources) { + sources.addAnnotatedClass( ManyToOneEntity.class ); + sources.addAnnotatedClass( SimpleEntity.class ); + return (MetadataImpl) sources.buildMetadata(); } } diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicHbmBindingTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicHbmBindingTests.java index 847757b73d..927354b15e 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicHbmBindingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicHbmBindingTests.java @@ -25,9 +25,6 @@ package org.hibernate.metamodel.binding; import org.hibernate.metamodel.MetadataSources; import org.hibernate.metamodel.source.internal.MetadataImpl; -import org.hibernate.metamodel.source.spi.MetadataImplementor; - -import static org.junit.Assert.assertEquals; /** * Basic tests of {@code hbm.xml} binding code @@ -35,33 +32,19 @@ import static org.junit.Assert.assertEquals; * @author Steve Ebersole */ public class BasicHbmBindingTests extends AbstractBasicBindingTests { - public EntityBinding buildSimpleEntityBinding() { - return getEntityBinding( - "org/hibernate/metamodel/binding/SimpleEntity.hbm.xml", - SimpleEntity.class.getName() - ); + public MetadataImpl addSourcesForSimpleEntityBinding(MetadataSources sources) { + sources.addResource( "org/hibernate/metamodel/binding/SimpleEntity.hbm.xml" ); + return (MetadataImpl) sources.buildMetadata(); } - public EntityBinding buildSimpleVersionedEntityBinding() { - return getEntityBinding( - "org/hibernate/metamodel/binding/SimpleVersionedEntity.hbm.xml", - SimpleVersionedEntity.class.getName() - ); + public MetadataImpl addSourcesForSimpleVersionedEntityBinding(MetadataSources sources) { + sources.addResource( "org/hibernate/metamodel/binding/SimpleVersionedEntity.hbm.xml" ); + return (MetadataImpl) sources.buildMetadata(); } - public MetadataImplementor buildMetadataWithManyToOne() { - MetadataSources metadataSources = new MetadataSources( basicServiceRegistry() ); - metadataSources.addResource( "org/hibernate/metamodel/binding/EntityWithManyToOne.hbm.xml" ); - metadataSources.addResource( "org/hibernate/metamodel/binding/SimpleEntity.hbm.xml" ); - assertEquals( 2, metadataSources.getJaxbRootList().size() ); - return ( MetadataImplementor ) metadataSources.buildMetadata(); - } - - private EntityBinding getEntityBinding(String resourceName, String entityName ) { - MetadataSources metadataSources = new MetadataSources( basicServiceRegistry() ); - metadataSources.addResource( resourceName ); - assertEquals( 1, metadataSources.getJaxbRootList().size() ); - MetadataImpl metadata = ( MetadataImpl ) metadataSources.buildMetadata(); - return metadata.getEntityBinding( entityName ); + public MetadataImpl addSourcesForManyToOne(MetadataSources sources) { + sources.addResource( "org/hibernate/metamodel/binding/ManyToOneEntity.hbm.xml" ); + sources.addResource( "org/hibernate/metamodel/binding/SimpleEntity.hbm.xml" ); + return (MetadataImpl) sources.buildMetadata(); } } diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/EntityWithManyToOne.hbm.xml b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/EntityWithManyToOne.hbm.xml deleted file mode 100644 index e1d9cb8d15..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/EntityWithManyToOne.hbm.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - - - diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/ManyToOneEntity.hbm.xml b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/ManyToOneEntity.hbm.xml new file mode 100644 index 0000000000..e0d1af18d7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/ManyToOneEntity.hbm.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/EntityWithManyToOne.java b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/ManyToOneEntity.java similarity index 78% rename from hibernate-core/src/test/java/org/hibernate/metamodel/binding/EntityWithManyToOne.java rename to hibernate-core/src/test/java/org/hibernate/metamodel/binding/ManyToOneEntity.java index 18485baad2..7069704cca 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/EntityWithManyToOne.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/ManyToOneEntity.java @@ -23,9 +23,6 @@ */ package org.hibernate.metamodel.binding; -import java.util.HashSet; -import java.util.Set; - import javax.persistence.Id; import javax.persistence.ManyToOne; @@ -35,17 +32,18 @@ import org.hibernate.annotations.Entity; * @author Gail Badner */ @Entity -public class EntityWithManyToOne { +public class ManyToOneEntity { @Id private Long id; private String theName; + @ManyToOne SimpleEntity simpleEntity; - public EntityWithManyToOne() { + public ManyToOneEntity() { } - public EntityWithManyToOne(String name) { - this.theName = theName; + public ManyToOneEntity(String name) { + this.theName = name; } public Long getId() { @@ -61,10 +59,9 @@ public class EntityWithManyToOne { } public void setName(String name) { - this.theName = theName; + this.theName = name; } - @ManyToOne public SimpleEntity getSimpleEntity() { return simpleEntity; } @@ -72,4 +69,15 @@ public class EntityWithManyToOne { public void setSimpleEntity(SimpleEntity simpleEntity) { this.simpleEntity = simpleEntity; } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder(); + sb.append( "EntityWithManyToOne" ); + sb.append( "{id=" ).append( id ); + sb.append( ", theName='" ).append( theName ).append( '\'' ); + sb.append( ", simpleEntity=" ).append( simpleEntity ); + sb.append( '}' ); + return sb.toString(); + } } diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/MiscAnnotationBindingTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/MiscAnnotationBindingTest.java index daac49fd64..71ba4f96f6 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/MiscAnnotationBindingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/MiscAnnotationBindingTest.java @@ -25,7 +25,7 @@ public class MiscAnnotationBindingTest extends BaseUnitTestCase { sources.addAnnotatedClass( Foo.class ); MetadataImpl metadata = (MetadataImpl) sources.buildMetadata(); - EntityBinding binding = metadata.getEntityBinding( MiscAnnotationBindingTest.class.getSimpleName() + "$" + Foo.class.getSimpleName() ); + EntityBinding binding = metadata.getEntityBinding( MiscAnnotationBindingTest.class.getName() + "$" + Foo.class.getSimpleName() ); assertEquals( "Wrong where filter", "1=1", binding.getWhereFilter() ); } diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/CacheBindingTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/CacheBindingTests.java similarity index 92% rename from hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/CacheBindingTests.java rename to hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/CacheBindingTests.java index b35f23a53e..4c4aeb9405 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/CacheBindingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/CacheBindingTests.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.annotations; +package org.hibernate.metamodel.source.annotations.entity; import javax.persistence.Cacheable; import javax.persistence.Entity; @@ -65,7 +65,11 @@ public class CacheBindingTests extends BaseUnitTestCase { EntityBinding binding = getEntityBinding( JpaCacheEntity.class, SharedCacheMode.ALL ); assertNotNull( "There should be a cache binding", binding.getCaching() ); Caching caching = binding.getCaching(); - assertEquals( "Wrong region", "CacheBindingTests$JpaCacheEntity", caching.getRegion() ); + assertEquals( + "Wrong region", + this.getClass().getName() + "$" + JpaCacheEntity.class.getSimpleName(), + caching.getRegion() + ); assertEquals( "Wrong lazy properties configuration", true, caching.isCacheLazyProperties() ); } @@ -81,7 +85,7 @@ public class CacheBindingTests extends BaseUnitTestCase { sources.getMetadataBuilder().with( cacheMode ); MetadataImpl metadata = (MetadataImpl) sources.buildMetadata(); - return metadata.getEntityBinding( this.getClass().getSimpleName() + "$" + clazz.getSimpleName() ); + return metadata.getEntityBinding( this.getClass().getName() + "$" + clazz.getSimpleName() ); } @Entity diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/InheritanceTypeTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/InheritanceTypeTest.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/InheritanceTypeTest.java rename to hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/InheritanceTypeTest.java index 90ec00efd4..8f5738feb0 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/InheritanceTypeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/InheritanceTypeTest.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.annotations; +package org.hibernate.metamodel.source.annotations.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @@ -52,7 +52,7 @@ public class InheritanceTypeTest extends BaseUnitTestCase { @Test public void testDiscriminatorValue() { MetadataImpl meta = buildMetadata( RootOfSingleTableInheritance.class, SubclassOfSingleTableInheritance.class ); - EntityBinding entityBinding = meta.getEntityBinding( SubclassOfSingleTableInheritance.class.getSimpleName() ); + EntityBinding entityBinding = meta.getEntityBinding( SubclassOfSingleTableInheritance.class.getName() ); assertEquals( "Wrong discriminator value", "foo", entityBinding.getDiscriminatorValue() ); } @@ -65,7 +65,7 @@ public class InheritanceTypeTest extends BaseUnitTestCase { } private EntityBinding getEntityBindingForInnerClass(MetadataImpl meta, Class clazz) { - return meta.getEntityBinding( this.getClass().getSimpleName() + "$" + clazz.getSimpleName() ); + return meta.getEntityBinding( this.getClass().getName() + "$" + clazz.getSimpleName() ); } @Entity diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/RootOfSingleTableInheritance.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/RootOfSingleTableInheritance.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/RootOfSingleTableInheritance.java rename to hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/RootOfSingleTableInheritance.java index ef78455cd6..a16b0beb2b 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/RootOfSingleTableInheritance.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/RootOfSingleTableInheritance.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.annotations; +package org.hibernate.metamodel.source.annotations.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/SubclassOfSingleTableInheritance.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/SubclassOfSingleTableInheritance.java similarity index 95% rename from hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/SubclassOfSingleTableInheritance.java rename to hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/SubclassOfSingleTableInheritance.java index e7a76529ef..6cc033e95e 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/SubclassOfSingleTableInheritance.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/SubclassOfSingleTableInheritance.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.annotations; +package org.hibernate.metamodel.source.annotations.entity; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/TableNameTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/TableNameTest.java similarity index 97% rename from hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/TableNameTest.java rename to hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/TableNameTest.java index 9544412ac2..feafbc5f4c 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/TableNameTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/entity/TableNameTest.java @@ -21,7 +21,7 @@ * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ -package org.hibernate.metamodel.source.annotations; +package org.hibernate.metamodel.source.annotations.entity; import java.util.Iterator; import java.util.Set; @@ -40,6 +40,8 @@ import org.junit.Before; import org.junit.Test; import org.hibernate.metamodel.binding.InheritanceType; +import org.hibernate.metamodel.source.annotations.entity.ConfiguredClass; +import org.hibernate.metamodel.source.annotations.entity.ConfiguredClassHierarchy; import org.hibernate.metamodel.source.annotations.util.ConfiguredClassHierarchyBuilder; import org.hibernate.metamodel.source.annotations.util.JandexHelper; import org.hibernate.service.ServiceRegistryBuilder; diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/ConfiguredClassHierarchyBuilderTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/ConfiguredClassHierarchyBuilderTest.java index 15977204d4..8363514f42 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/ConfiguredClassHierarchyBuilderTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/ConfiguredClassHierarchyBuilderTest.java @@ -42,8 +42,10 @@ import org.junit.Test; import org.hibernate.AnnotationException; import org.hibernate.metamodel.binding.InheritanceType; -import org.hibernate.metamodel.source.annotations.ConfiguredClass; -import org.hibernate.metamodel.source.annotations.ConfiguredClassHierarchy; +import org.hibernate.metamodel.source.annotations.entity.ConfiguredClass; +import org.hibernate.metamodel.source.annotations.entity.ConfiguredClassHierarchy; +import org.hibernate.metamodel.source.annotations.entity.ConfiguredClass; +import org.hibernate.metamodel.source.annotations.entity.ConfiguredClassHierarchy; import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.service.classloading.spi.ClassLoaderService; import org.hibernate.service.internal.BasicServiceRegistryImpl; diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/GenericTypeDiscoveryTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/GenericTypeDiscoveryTest.java index 2d7a25a1dc..886c90c763 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/GenericTypeDiscoveryTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/GenericTypeDiscoveryTest.java @@ -38,9 +38,9 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.hibernate.metamodel.source.annotations.ConfiguredClass; -import org.hibernate.metamodel.source.annotations.ConfiguredClassHierarchy; -import org.hibernate.metamodel.source.annotations.MappedAttribute; +import org.hibernate.metamodel.source.annotations.entity.ConfiguredClass; +import org.hibernate.metamodel.source.annotations.entity.ConfiguredClassHierarchy; +import org.hibernate.metamodel.source.annotations.entity.MappedAttribute; import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.service.classloading.spi.ClassLoaderService; import org.hibernate.service.internal.BasicServiceRegistryImpl; @@ -81,7 +81,7 @@ public class GenericTypeDiscoveryTest extends BaseUnitTestCase { ClassInfo info = configuredClass.getClassInfo(); assertEquals( "wrong class", DotName.createSimple( Stuff.class.getName() ), info.name() ); MappedAttribute property = configuredClass.getMappedProperty( "value" ); - assertEquals( Price.class, property.getType() ); + assertEquals( Price.class.getName(), property.getType() ); assertTrue( iter.hasNext() ); configuredClass = iter.next(); @@ -97,9 +97,9 @@ public class GenericTypeDiscoveryTest extends BaseUnitTestCase { assertEquals( "wrong class", DotName.createSimple( Item.class.getName() ), info.name() ); // properties are alphabetically ordered! property = configuredClass.getMappedProperty( "owner" ); - assertEquals( SomeGuy.class, property.getType() ); + assertEquals( SomeGuy.class.getName(), property.getType() ); property = configuredClass.getMappedProperty( "type" ); - assertEquals( PaperType.class, property.getType() ); + assertEquals( PaperType.class.getName(), property.getType() ); assertTrue( iter.hasNext() ); configuredClass = iter.next(); diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/TypeDiscoveryTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/TypeDiscoveryTest.java new file mode 100644 index 0000000000..223e7fdc28 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/util/TypeDiscoveryTest.java @@ -0,0 +1,98 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.metamodel.source.annotations.util; + +import java.util.Iterator; +import java.util.Map; +import java.util.Set; +import javax.persistence.Id; + +import org.jboss.jandex.Index; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import org.hibernate.annotations.Parameter; +import org.hibernate.annotations.Type; +import org.hibernate.metamodel.source.annotations.entity.ConfiguredClass; +import org.hibernate.metamodel.source.annotations.entity.ConfiguredClassHierarchy; +import org.hibernate.metamodel.source.annotations.entity.MappedAttribute; +import org.hibernate.service.ServiceRegistryBuilder; +import org.hibernate.service.classloading.spi.ClassLoaderService; +import org.hibernate.service.internal.BasicServiceRegistryImpl; +import org.hibernate.testing.junit4.BaseUnitTestCase; + +import static junit.framework.Assert.assertEquals; + +/** + * @author Hardy Ferentschik + */ +public class TypeDiscoveryTest extends BaseUnitTestCase { + private BasicServiceRegistryImpl serviceRegistry; + private ClassLoaderService service; + + @Before + public void setUp() { + serviceRegistry = (BasicServiceRegistryImpl) new ServiceRegistryBuilder().buildServiceRegistry(); + service = serviceRegistry.getService( ClassLoaderService.class ); + } + + @After + public void tearDown() { + serviceRegistry.destroy(); + } + + @Test + public void testImplicitAndExplicitType() { + Index index = JandexHelper.indexForClass( service, Entity.class ); + Set hierarchies = ConfiguredClassHierarchyBuilder.createEntityHierarchies( + index, serviceRegistry + ); + assertEquals( "There should be only one hierarchy", 1, hierarchies.size() ); + + Iterator iter = hierarchies.iterator().next().iterator(); + ConfiguredClass configuredClass = iter.next(); + + MappedAttribute property = configuredClass.getMappedProperty( "id" ); + assertEquals( "Unexpected property type", "int", property.getType() ); + + property = configuredClass.getMappedProperty( "string" ); + assertEquals( "Unexpected property type", String.class.getName(), property.getType() ); + + property = configuredClass.getMappedProperty( "customString" ); + assertEquals( "Unexpected property type", "my.custom.Type", property.getType() ); + + Map typeParameters = property.getTypeParameters(); + assertEquals( "There should be a type parameter", "bar", typeParameters.get( "foo" ) ); + } + + @javax.persistence.Entity + class Entity { + @Id + private int id; + private String string; + @Type(type = "my.custom.Type", parameters = { @Parameter(name = "foo", value = "bar") }) + private String customString; + } +} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/transaction/jta/ManagedDrivingTest.java b/hibernate-core/src/test/java/org/hibernate/test/transaction/jta/ManagedDrivingTest.java index 4756608460..fc70db21c7 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/transaction/jta/ManagedDrivingTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/transaction/jta/ManagedDrivingTest.java @@ -112,7 +112,7 @@ public class ManagedDrivingTest extends BaseUnitTestCase { fail( "incorrect exception type : SQLException" ); } - JtaPlatform instance = ( (ServiceProxy) serviceRegistry.getService( JtaPlatform.class ) ).getTargetInstance(); + JtaPlatform instance = serviceRegistry.getService( JtaPlatform.class ); TransactionManager transactionManager = instance.retrieveTransactionManager(); // start the cmt diff --git a/hibernate-infinispan/hibernate-infinispan.gradle b/hibernate-infinispan/hibernate-infinispan.gradle index 95e428fd88..119466347d 100644 --- a/hibernate-infinispan/hibernate-infinispan.gradle +++ b/hibernate-infinispan/hibernate-infinispan.gradle @@ -5,7 +5,7 @@ configurations { } dependencies { - infinispanVersion = '4.2.1.CR1' + infinispanVersion = '5.0.0.CR2' jnpVersion = '5.0.3.GA' compile(project(':hibernate-core')) diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseRegion.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseRegion.java index 3f9ccdd41d..96082a9d35 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseRegion.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/impl/BaseRegion.java @@ -268,7 +268,7 @@ public abstract class BaseRegion implements Region { protected boolean handleEvictAllModification(CacheEntryModifiedEvent event) { if (!event.isPre() && (replication || event.isOriginLocal()) && CacheHelper.isEvictAllNotification(event.getKey(), event.getValue())) { - if (log.isTraceEnabled()) log.trace("Set invalid state because marker cache entry was put: {0}", event); + if (log.isTraceEnabled()) log.tracef("Set invalid state because marker cache entry was put: {0}", event); invalidateState.set(InvalidateState.INVALID); return true; } @@ -277,13 +277,13 @@ public abstract class BaseRegion implements Region { @CacheEntryInvalidated public void entryInvalidated(CacheEntryInvalidatedEvent event) { - if (log.isTraceEnabled()) log.trace("Cache entry invalidated: {0}", event); + if (log.isTraceEnabled()) log.tracef("Cache entry invalidated: {0}", event); handleEvictAllInvalidation(event); } protected boolean handleEvictAllInvalidation(CacheEntryInvalidatedEvent event) { if (!event.isPre() && CacheHelper.isEvictAllNotification(event.getKey())) { - if (log.isTraceEnabled()) log.trace("Set invalid state because marker cache entry was invalidated: {0}", event); + if (log.isTraceEnabled()) log.tracef("Set invalid state because marker cache entry was invalidated: {0}", event); invalidateState.set(InvalidateState.INVALID); return true; } diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/util/AddressAdapterImpl.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/util/AddressAdapterImpl.java index 0b2c511ee7..e46e25c0a3 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/util/AddressAdapterImpl.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/util/AddressAdapterImpl.java @@ -39,7 +39,11 @@ public class AddressAdapterImpl implements AddressAdapter, Externalizable { private Address address; - private AddressAdapterImpl(Address address) { + // Required by Java Externalizable + public AddressAdapterImpl() { + } + + public AddressAdapterImpl(Address address) { this.address = address; } diff --git a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/util/CacheHelper.java b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/util/CacheHelper.java index e4ce4caa2a..df93ee93f5 100644 --- a/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/util/CacheHelper.java +++ b/hibernate-infinispan/src/main/java/org/hibernate/cache/infinispan/util/CacheHelper.java @@ -69,10 +69,14 @@ public class CacheHelper { return key instanceof EvictAll && value == Internal.EVICT; } - private static class EvictAll implements Externalizable { + public static class EvictAll implements Externalizable { AddressAdapter member; - EvictAll(AddressAdapter member) { + // Required by Java Externalizable + public EvictAll() { + } + + public EvictAll(AddressAdapter member) { this.member = member; } diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/ConcurrentWriteTest.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/ConcurrentWriteTest.java index 07d1fc0c00..b19f3860a7 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/ConcurrentWriteTest.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/ConcurrentWriteTest.java @@ -175,7 +175,7 @@ public class ConcurrentWriteTest extends SingleNodeTestCase { assertEquals( customerSlcs.getElementCountInMemory(), 1 ); assertEquals( customerSlcs.getEntries().size(), 1 ); - log.info( "Add contact to customer {0}", customerId ); + log.infof( "Add contact to customer {0}", customerId ); SecondLevelCacheStatistics contactsCollectionSlcs = sessionFactory() .getStatistics() .getSecondLevelCacheStatistics( Customer.class.getName() + ".contacts" ); @@ -478,7 +478,7 @@ public class ConcurrentWriteTest extends SingleNodeTestCase { thinkRandomTime(); ++completedIterations; if ( log.isTraceEnabled() ) { - log.trace( "Iteration completed {0}", completedIterations ); + log.tracef( "Iteration completed {0}", completedIterations ); } } } diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/cluster/DualNodeJtaTransactionImpl.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/cluster/DualNodeJtaTransactionImpl.java index fbe8d93032..8476107948 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/cluster/DualNodeJtaTransactionImpl.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/functional/cluster/DualNodeJtaTransactionImpl.java @@ -263,7 +263,7 @@ public class DualNodeJtaTransactionImpl implements Transaction { if (prepareResult != XAResource.XA_RDONLY) xaResource.commit(xid, b); else - log.trace("Not committing {0} due to readonly.", xid); + log.tracef("Not committing {0} due to readonly.", xid); } @Override diff --git a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/tm/XaTransactionImpl.java b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/tm/XaTransactionImpl.java index ce9bccf95c..b098f6e0c1 100644 --- a/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/tm/XaTransactionImpl.java +++ b/hibernate-infinispan/src/test/java/org/hibernate/test/cache/infinispan/tm/XaTransactionImpl.java @@ -265,7 +265,7 @@ public class XaTransactionImpl implements Transaction { if (prepareResult != XAResource.XA_RDONLY) xaResource.commit(xid, b); else - log.trace("Not committing {0} due to readonly.", xid); + log.tracef("Not committing {0} due to readonly.", xid); } @Override