HHH-7049 - Add tests of org.hibernate.metamodel.internal.source stuff

This commit is contained in:
Steve Ebersole 2012-02-10 17:54:12 -06:00
parent da2eac71ed
commit 01eec1fbd2
17 changed files with 179 additions and 50 deletions

View File

@ -70,6 +70,11 @@ public boolean isLazy() {
return attribute.isLazy();
}
@Override
public NaturalIdMutability getNaturalIdMutability() {
return null; // todo : implement proper method body
}
@Override
public boolean isIncludedInOptimisticLocking() {
return attribute.isOptimisticLockable();

View File

@ -51,6 +51,11 @@ public SingularAttributeNature getNature() {
return SingularAttributeNature.MANY_TO_ONE;
}
@Override
public NaturalIdMutability getNaturalIdMutability() {
return null; // todo : implement proper method body
}
@Override
public String getReferencedEntityName() {
return associationAttribute.getReferencedEntityType();

View File

@ -44,6 +44,7 @@
import org.hibernate.metamodel.spi.source.MetaAttributeSource;
import org.hibernate.metamodel.spi.source.RelationalValueSource;
import org.hibernate.metamodel.spi.source.SingularAttributeNature;
import org.hibernate.metamodel.spi.source.SingularAttributeSource;
/**
* Annotation backed implementation of {@code ComponentAttributeSource}.
@ -115,30 +116,39 @@ public LocalBindingContext getLocalBindingContext() {
return embeddableClass.getLocalBindingContext();
}
@Override
public Iterable<AttributeSource> attributeSources() {
List<AttributeSource> attributeList = new ArrayList<AttributeSource>();
for ( BasicAttribute attribute : embeddableClass.getSimpleAttributes() ) {
AttributeOverride attributeOverride = null;
String tmp = getPath() + PATH_SEPARATOR + attribute.getName();
if ( attributeOverrides.containsKey( tmp ) ) {
attributeOverride = attributeOverrides.get( tmp );
private final Value<List<AttributeSource>> attributeSourcesValue = new Value<List<AttributeSource>>(
new Value.DeferredInitializer<List<AttributeSource>>() {
@Override
public List<AttributeSource> initialize() {
List<AttributeSource> attributeList = new ArrayList<AttributeSource>();
for ( BasicAttribute attribute : embeddableClass.getSimpleAttributes() ) {
AttributeOverride attributeOverride = null;
String tmp = getPath() + PATH_SEPARATOR + attribute.getName();
if ( attributeOverrides.containsKey( tmp ) ) {
attributeOverride = attributeOverrides.get( tmp );
}
attributeList.add( new SingularAttributeSourceImpl( attribute, attributeOverride ) );
}
for ( EmbeddableClass embeddable : embeddableClass.getEmbeddedClasses().values() ) {
attributeList.add(
new ComponentAttributeSourceImpl(
embeddable,
getPath(),
createAggregatedOverrideMap()
)
);
}
for ( AssociationAttribute associationAttribute : embeddableClass.getAssociationAttributes() ) {
attributeList.add( new ToOneAttributeSourceImpl( associationAttribute ) );
}
return Collections.unmodifiableList( attributeList );
}
}
attributeList.add( new SingularAttributeSourceImpl( attribute, attributeOverride ) );
}
for ( EmbeddableClass embeddable : embeddableClass.getEmbeddedClasses().values() ) {
attributeList.add(
new ComponentAttributeSourceImpl(
embeddable,
getPath(),
createAggregatedOverrideMap()
)
);
}
for ( AssociationAttribute associationAttribute : embeddableClass.getAssociationAttributes() ) {
attributeList.add( new ToOneAttributeSourceImpl( associationAttribute ) );
}
return attributeList;
);
@Override
public List<AttributeSource> attributeSources() {
return attributeSourcesValue.getValue();
}
@Override
@ -179,6 +189,11 @@ public boolean isLazy() {
return false;
}
@Override
public NaturalIdMutability getNaturalIdMutability() {
return null; // todo : implement proper method body
}
@Override
public boolean isIncludedInOptimisticLocking() {
return true;

View File

@ -171,7 +171,7 @@ public String getPath() {
}
@Override
public Iterable<AttributeSource> attributeSources() {
public List<AttributeSource> attributeSources() {
List<AttributeSource> attributeList = new ArrayList<AttributeSource>();
for ( BasicAttribute attribute : entityClass.getSimpleAttributes() ) {
attributeList.add( new SingularAttributeSourceImpl( attribute ) );

View File

@ -45,12 +45,13 @@
import org.hibernate.internal.jaxb.mapping.hbm.JaxbTuplizerElement;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.spi.binding.CustomSQL;
import org.hibernate.metamodel.spi.source.LocalBindingContext;
import org.hibernate.metamodel.spi.source.AttributeSource;
import org.hibernate.metamodel.spi.source.ConstraintSource;
import org.hibernate.metamodel.spi.source.EntitySource;
import org.hibernate.metamodel.spi.source.JpaCallbackSource;
import org.hibernate.metamodel.spi.source.LocalBindingContext;
import org.hibernate.metamodel.spi.source.MetaAttributeSource;
import org.hibernate.metamodel.spi.source.SingularAttributeSource;
import org.hibernate.metamodel.spi.source.SubclassEntitySource;
import org.hibernate.metamodel.spi.source.TableSource;
@ -202,31 +203,51 @@ public String getPath() {
}
@Override
public Iterable<AttributeSource> attributeSources() {
public List<AttributeSource> attributeSources() {
List<AttributeSource> attributeSources = new ArrayList<AttributeSource>();
for ( Object attributeElement : entityElement.getPropertyOrManyToOneOrOneToOne() ) {
processAttributes( attributeSources );
return attributeSources;
}
protected List<AttributeSource> processAttributes(List<AttributeSource> attributeSources) {
processAttributes(
attributeSources,
entityElement.getPropertyOrManyToOneOrOneToOne(),
SingularAttributeSource.NaturalIdMutability.NOT_NATURAL_ID
);
return attributeSources;
}
protected void processAttributes(
List<AttributeSource> results,
List attributeElements,
SingularAttributeSource.NaturalIdMutability naturalIdMutability) {
for ( Object attributeElement : attributeElements ) {
if ( JaxbPropertyElement.class.isInstance( attributeElement ) ) {
attributeSources.add(
results.add(
new PropertyAttributeSourceImpl(
JaxbPropertyElement.class.cast( attributeElement ),
sourceMappingDocument().getMappingLocalBindingContext()
sourceMappingDocument().getMappingLocalBindingContext(),
naturalIdMutability
)
);
}
else if ( JaxbComponentElement.class.isInstance( attributeElement ) ) {
attributeSources.add(
results.add(
new ComponentAttributeSourceImpl(
(JaxbComponentElement) attributeElement,
this,
sourceMappingDocument.getMappingLocalBindingContext()
sourceMappingDocument.getMappingLocalBindingContext(),
naturalIdMutability
)
);
}
else if ( JaxbManyToOneElement.class.isInstance( attributeElement ) ) {
attributeSources.add(
results.add(
new ManyToOneAttributeSourceImpl(
JaxbManyToOneElement.class.cast( attributeElement ),
sourceMappingDocument().getMappingLocalBindingContext()
sourceMappingDocument().getMappingLocalBindingContext(),
naturalIdMutability
)
);
}
@ -237,7 +258,7 @@ else if ( JaxbAnyElement.class.isInstance( attributeElement ) ) {
// todo : implement
}
else if ( JaxbBagElement.class.isInstance( attributeElement ) ) {
attributeSources.add(
results.add(
new BagAttributeSourceImpl(
JaxbBagElement.class.cast( attributeElement ),
this
@ -248,7 +269,7 @@ else if ( JaxbIdbagElement.class.isInstance( attributeElement ) ) {
// todo : implement
}
else if ( JaxbSetElement.class.isInstance( attributeElement ) ) {
attributeSources.add(
results.add(
new SetAttributeSourceImpl(
JaxbSetElement.class.cast( attributeElement ),
this
@ -265,7 +286,6 @@ else if ( JaxbMapElement.class.isInstance( attributeElement ) ) {
throw new AssertionFailure( "Unexpected attribute element type encountered : " + attributeElement.getClass() );
}
}
return attributeSources;
}
private EntityHierarchyImpl entityHierarchy;

View File

@ -46,6 +46,7 @@
import org.hibernate.metamodel.spi.source.MetaAttributeSource;
import org.hibernate.metamodel.spi.source.RelationalValueSource;
import org.hibernate.metamodel.spi.source.SingularAttributeNature;
import org.hibernate.metamodel.spi.source.SingularAttributeSource;
/**
* @author Steve Ebersole
@ -53,17 +54,18 @@
public class ComponentAttributeSourceImpl implements ComponentAttributeSource {
private final JaxbComponentElement componentElement;
private final AttributeSourceContainer parentContainer;
private final NaturalIdMutability naturalIdMutability;
private final Value<Class<?>> componentClassReference;
private final String path;
public ComponentAttributeSourceImpl(
JaxbComponentElement componentElement,
AttributeSourceContainer parentContainer,
LocalBindingContext bindingContext) {
LocalBindingContext bindingContext,
NaturalIdMutability naturalIdMutability) {
this.componentElement = componentElement;
this.parentContainer = parentContainer;
this.naturalIdMutability = naturalIdMutability;
this.componentClassReference = bindingContext.makeClassReference(
bindingContext.qualifyClassName( componentElement.getClazz() )
);
@ -110,14 +112,15 @@ public String getExplicitTuplizerClassName() {
}
@Override
public Iterable<AttributeSource> attributeSources() {
public List<AttributeSource> attributeSources() {
List<AttributeSource> attributeSources = new ArrayList<AttributeSource>();
for ( Object attributeElement : componentElement.getPropertyOrManyToOneOrOneToOne() ) {
if ( JaxbPropertyElement.class.isInstance( attributeElement ) ) {
attributeSources.add(
new PropertyAttributeSourceImpl(
JaxbPropertyElement.class.cast( attributeElement ),
getLocalBindingContext()
getLocalBindingContext(),
naturalIdMutability
)
);
}
@ -126,7 +129,8 @@ else if ( JaxbComponentElement.class.isInstance( attributeElement ) ) {
new ComponentAttributeSourceImpl(
(JaxbComponentElement) attributeElement,
this,
getLocalBindingContext()
getLocalBindingContext(),
naturalIdMutability
)
);
}
@ -134,7 +138,8 @@ else if ( JaxbManyToOneElement.class.isInstance( attributeElement ) ) {
attributeSources.add(
new ManyToOneAttributeSourceImpl(
JaxbManyToOneElement.class.cast( attributeElement ),
getLocalBindingContext()
getLocalBindingContext(),
naturalIdMutability
)
);
}
@ -196,6 +201,11 @@ public boolean isLazy() {
return componentElement.isLazy();
}
@Override
public NaturalIdMutability getNaturalIdMutability() {
return naturalIdMutability;
}
@Override
public boolean isIncludedInOptimisticLocking() {
return componentElement.isOptimisticLock();

View File

@ -93,7 +93,7 @@ public String getPath() {
}
@Override
public Iterable<AttributeSource> attributeSources() {
public List<AttributeSource> attributeSources() {
List<AttributeSource> attributeSources = new ArrayList<AttributeSource>();
for ( Object attribute : compositeElement.getPropertyOrManyToOneOrAny() ) {

View File

@ -31,9 +31,9 @@
import org.hibernate.engine.spi.CascadeStyle;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbManyToOneElement;
import org.hibernate.mapping.PropertyGeneration;
import org.hibernate.metamodel.spi.source.ExplicitHibernateTypeSource;
import org.hibernate.metamodel.spi.source.LocalBindingContext;
import org.hibernate.metamodel.spi.source.MappingException;
import org.hibernate.metamodel.spi.source.ExplicitHibernateTypeSource;
import org.hibernate.metamodel.spi.source.MetaAttributeSource;
import org.hibernate.metamodel.spi.source.RelationalValueSource;
import org.hibernate.metamodel.spi.source.SingularAttributeNature;
@ -47,11 +47,16 @@
class ManyToOneAttributeSourceImpl implements ToOneAttributeSource {
private final JaxbManyToOneElement manyToOneElement;
private final LocalBindingContext bindingContext;
private final NaturalIdMutability naturalIdMutability;
private final List<RelationalValueSource> valueSources;
ManyToOneAttributeSourceImpl(final JaxbManyToOneElement manyToOneElement, LocalBindingContext bindingContext) {
ManyToOneAttributeSourceImpl(
final JaxbManyToOneElement manyToOneElement,
LocalBindingContext bindingContext,
NaturalIdMutability naturalIdMutability) {
this.manyToOneElement = manyToOneElement;
this.bindingContext = bindingContext;
this.naturalIdMutability = naturalIdMutability;
this.valueSources = Helper.buildValueSources(
new Helper.ValueSourcesAdapter() {
@Override
@ -114,6 +119,11 @@ public boolean isLazy() {
return false;
}
@Override
public NaturalIdMutability getNaturalIdMutability() {
return naturalIdMutability;
}
@Override
public boolean isIncludedInOptimisticLocking() {
return manyToOneElement.isOptimisticLock();

View File

@ -28,8 +28,8 @@
import org.hibernate.internal.jaxb.mapping.hbm.JaxbPropertyElement;
import org.hibernate.mapping.PropertyGeneration;
import org.hibernate.metamodel.spi.source.LocalBindingContext;
import org.hibernate.metamodel.spi.source.ExplicitHibernateTypeSource;
import org.hibernate.metamodel.spi.source.LocalBindingContext;
import org.hibernate.metamodel.spi.source.MetaAttributeSource;
import org.hibernate.metamodel.spi.source.RelationalValueSource;
import org.hibernate.metamodel.spi.source.SingularAttributeNature;
@ -44,8 +44,12 @@ class PropertyAttributeSourceImpl implements SingularAttributeSource {
private final JaxbPropertyElement propertyElement;
private final ExplicitHibernateTypeSource typeSource;
private final List<RelationalValueSource> valueSources;
private final NaturalIdMutability naturalIdMutability;
PropertyAttributeSourceImpl(final JaxbPropertyElement propertyElement, LocalBindingContext bindingContext) {
PropertyAttributeSourceImpl(
final JaxbPropertyElement propertyElement,
LocalBindingContext bindingContext,
NaturalIdMutability naturalIdMutability) {
this.propertyElement = propertyElement;
this.typeSource = new ExplicitHibernateTypeSource() {
private final String name = propertyElement.getTypeAttribute() != null
@ -102,6 +106,7 @@ public boolean isIncludedInUpdateByDefault() {
},
bindingContext
);
this.naturalIdMutability = naturalIdMutability;
}
@Override
@ -129,6 +134,11 @@ public boolean isLazy() {
return Helper.getBooleanValue( propertyElement.isLazy(), false );
}
@Override
public NaturalIdMutability getNaturalIdMutability() {
return naturalIdMutability;
}
@Override
public boolean isIncludedInOptimisticLocking() {
return Helper.getBooleanValue( propertyElement.isOptimisticLock(), true );

View File

@ -23,6 +23,9 @@
*/
package org.hibernate.metamodel.internal.source.hbm;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.EntityMode;
import org.hibernate.TruthValue;
import org.hibernate.cache.spi.access.AccessType;
@ -33,6 +36,7 @@
import org.hibernate.internal.util.Value;
import org.hibernate.metamodel.spi.binding.Caching;
import org.hibernate.metamodel.spi.binding.IdGenerator;
import org.hibernate.metamodel.spi.source.AttributeSource;
import org.hibernate.metamodel.spi.source.MappingException;
import org.hibernate.metamodel.spi.source.DiscriminatorSource;
import org.hibernate.metamodel.spi.source.IdentifierSource;
@ -112,6 +116,21 @@ else if ( entityElement().getTimestamp() != null ) {
return null;
}
@Override
public List<AttributeSource> attributeSources() {
List<AttributeSource> attributeSources = new ArrayList<AttributeSource>();
final JaxbHibernateMapping.JaxbClass.JaxbNaturalId naturalId = entityElement().getNaturalId();
processAttributes(
attributeSources,
naturalId.getPropertyOrManyToOneOrComponent(),
naturalId.isMutable()
? SingularAttributeSource.NaturalIdMutability.MUTABLE
: SingularAttributeSource.NaturalIdMutability.IMMUTABLE
);
processAttributes( attributeSources );
return attributeSources;
}
@Override
public EntityMode getEntityMode() {
return determineEntityMode();

View File

@ -138,6 +138,11 @@ public boolean isLazy() {
return false;
}
@Override
public NaturalIdMutability getNaturalIdMutability() {
return NaturalIdMutability.NOT_NATURAL_ID;
}
@Override
public boolean isIncludedInOptimisticLocking() {
return false;

View File

@ -144,6 +144,11 @@ public boolean isLazy() {
return false;
}
@Override
public NaturalIdMutability getNaturalIdMutability() {
return NaturalIdMutability.NOT_NATURAL_ID;
}
@Override
public boolean isIncludedInOptimisticLocking() {
return false;

View File

@ -144,6 +144,11 @@ public boolean isLazy() {
return false;
}
@Override
public NaturalIdMutability getNaturalIdMutability() {
return NaturalIdMutability.NOT_NATURAL_ID;
}
@Override
public boolean isIncludedInOptimisticLocking() {
return false;

View File

@ -23,6 +23,8 @@
*/
package org.hibernate.metamodel.spi.source;
import java.util.List;
/**
* Contract for a container of {@link AttributeSource} references. Both entities and components contain
* attributes.
@ -42,7 +44,7 @@ public interface AttributeSourceContainer {
*
* @return The attribute sources.
*/
public Iterable<AttributeSource> attributeSources();
public List<AttributeSource> attributeSources();
/**
* Obtain the local binding context associated with this container.

View File

@ -23,6 +23,7 @@
*/
package org.hibernate.metamodel.spi.source;
import org.hibernate.TruthValue;
import org.hibernate.mapping.PropertyGeneration;
/**
@ -59,4 +60,17 @@ public interface SingularAttributeSource extends AttributeSource, RelationalValu
* @return {@code true} to indicate the attribute should be lazily loaded.
*/
public boolean isLazy();
/**
* Retrieve the natural id mutability
*
* @return The mutability, see enum for meanings
*/
public NaturalIdMutability getNaturalIdMutability();
public static enum NaturalIdMutability {
MUTABLE,
IMMUTABLE,
NOT_NATURAL_ID
}
}

View File

@ -159,6 +159,8 @@ private void testUserEntitySources(MetadataSourceProcessor processor) {
// assertFalse( columnSource.isIncludedInUpdate() );
// assertFalse( columnSource.isNullable() );
assertEquals( 2, entitySource.attributeSources().size() );
}
}

View File

@ -6,7 +6,9 @@
<id name="id">
<generator class="increment"/>
</id>
<property name="userName" column="UNAME"/>
<natural-id>
<property name="userName" column="UNAME"/>
</natural-id>
<component name="name" class="User$Name">
<property name="firstName" column="FNAME"/>
<property name="middleName" column="MNAME"/>