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 f28a22ffe8..5d7b785e92 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 @@ -31,13 +31,11 @@ import java.util.Map; import java.util.Set; import org.hibernate.MappingException; -import org.hibernate.cfg.NamingStrategy; import org.hibernate.metamodel.domain.Attribute; import org.hibernate.metamodel.domain.MetaAttribute; import org.hibernate.metamodel.relational.Column; import org.hibernate.metamodel.relational.DerivedValue; import org.hibernate.metamodel.relational.SimpleValue; -import org.hibernate.metamodel.relational.Size; import org.hibernate.metamodel.relational.TableSpecification; import org.hibernate.metamodel.relational.Tuple; import org.hibernate.metamodel.relational.Value; @@ -83,7 +81,7 @@ public abstract class AbstractAttributeBinding implements AttributeBinding { this.entityBinding = entityBinding; } - public void initialize(DomainState state) { + protected void initialize(DomainState state) { hibernateTypeDescriptor.initialize( state.getHibernateTypeDescriptor() ); attribute = state.getAttribute(); isLazy = state.isLazy(); @@ -118,8 +116,7 @@ public abstract class AbstractAttributeBinding implements AttributeBinding { } protected void initializeColumnValue(ColumnRelationalState state) { - Column columnValue = createColumn( state ); - setValue( columnValue ); + value = createColumn( state ); } private Column createColumn(ColumnRelationalState state) { @@ -151,17 +148,20 @@ public abstract class AbstractAttributeBinding implements AttributeBinding { return columnValue; } - public final void initialize(RelationalState state) { + // TODO: move this logic out... + protected void initialize(RelationalState state) { if ( SingleValueRelationalState.class.isInstance( state ) ) { initializeSingleValue( SingleValueRelationalState.class.cast( state ) ); } else if ( SimpleTupleRelationalState.class.isInstance( state ) ) { - initializeTupleValue( SimpleTupleRelationalState.class.cast( state ).getRelationalStates() ); + initializeSimpleTupleValue( SimpleTupleRelationalState.class.cast( state ) ); + } + else { + throw new MappingException( "Unexpected type of RelationalState" + state.getClass().getName() ); } } - - public final void initializeDerivedValue(T state) { + protected void initializeDerivedValue(T state) { value = createDerivedValue( state ); } @@ -169,11 +169,11 @@ public abstract class AbstractAttributeBinding implements AttributeBinding { return getEntityBinding().getBaseTable().createDerivedValue( state.getFormula() ); } - public final void initializeSingleValue(SingleValueRelationalState state) { + private void initializeSingleValue(SingleValueRelationalState state) { value = createSingleValue( state ); } - protected SimpleValue createSingleValue(SingleValueRelationalState state) { + private SimpleValue createSingleValue(SingleValueRelationalState state) { if ( state instanceof ColumnRelationalState ) { return createColumn( ColumnRelationalState.class.cast( state ) ); } @@ -185,31 +185,31 @@ public abstract class AbstractAttributeBinding implements AttributeBinding { } } - protected final void initializeTupleValue(Set singleValueStates) { - if ( singleValueStates.size() == 0 ) { + protected void initializeSimpleTupleValue(SimpleTupleRelationalState state) { + if ( state.getRelationalStates().size() == 0 ) { throw new MappingException( "Tuple state does not contain any values." ); } - if ( singleValueStates.size() == 1 ) { - initializeSingleValue( singleValueStates.iterator().next() ); + if ( state.getRelationalStates().size() == 1 ) { + initializeSingleValue( state.getRelationalStates().iterator().next() ); } else { - Tuple tuple = getEntityBinding().getBaseTable().createTuple( "[" + getAttribute().getName() + "]" ); - for ( SingleValueRelationalState singleValueState : singleValueStates ) { - tuple.addValue( createSingleValue( singleValueState ) ); - } - value = tuple; + value = createSimpleTupleValue( state ); } } + private Tuple createSimpleTupleValue(SimpleTupleRelationalState state) { + Tuple tuple = getEntityBinding().getBaseTable().createTuple( "[" + getAttribute().getName() + "]" ); + for ( SingleValueRelationalState singleValueState : state.getRelationalStates() ) { + tuple.addValue( createSingleValue( singleValueState ) ); + } + return tuple; + } + @Override public Value getValue() { return value; } - protected void setValue(Value value) { - this.value = value; - } - @Override public HibernateTypeDescriptor getHibernateTypeDescriptor() { return hibernateTypeDescriptor; @@ -328,34 +328,4 @@ public abstract class AbstractAttributeBinding implements AttributeBinding { } } - public static interface RelationalState {} - - public static interface SingleValueRelationalState extends RelationalState {} - - public static interface ColumnRelationalState extends SingleValueRelationalState { - 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(); - } - - public static interface DerivedRelationalState extends SingleValueRelationalState { - String getFormula(); - } - - public static interface SimpleTupleRelationalState extends TupleRelationalState { - } - - public static interface TupleRelationalState extends RelationalState{ - Set getRelationalStates(); - } } 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 4aa4cad3ce..cac968a6d3 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 @@ -23,12 +23,15 @@ */ package org.hibernate.metamodel.binding; +import java.util.List; import java.util.Map; import java.util.Set; +import org.hibernate.cfg.NamingStrategy; import org.hibernate.metamodel.domain.Attribute; import org.hibernate.metamodel.domain.MetaAttribute; import org.hibernate.metamodel.relational.SimpleValue; +import org.hibernate.metamodel.relational.Size; import org.hibernate.metamodel.relational.TableSpecification; import org.hibernate.metamodel.relational.Value; @@ -108,4 +111,37 @@ public interface AttributeBinding { public Set getEntityReferencingAttributeBindings(); public void validate(); + + // TODO: where should this RelationalState stuff go??? + + interface RelationalState {} + + interface SingleValueRelationalState extends RelationalState {} + + interface ColumnRelationalState extends SingleValueRelationalState { + 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(); + } + + interface DerivedRelationalState extends SingleValueRelationalState { + String getFormula(); + } + + interface SimpleTupleRelationalState extends AbstractAttributeBinding.TupleRelationalState { + } + + interface TupleRelationalState extends RelationalState{ + List getRelationalStates(); + } } 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 1b050409ff..fb58189a2e 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 @@ -52,7 +52,7 @@ public class ManyToOneAttributeBinding extends SingularAttributeBinding implemen boolean ignoreNotFound(); } - public static interface ManyToOneRelationalState extends RelationalState { + public static interface RelationalState extends AttributeBinding.RelationalState { boolean isLogicalOneToOne(); String getForeignKeyName(); } @@ -75,7 +75,7 @@ public class ManyToOneAttributeBinding extends SingularAttributeBinding implemen } } - public final void initialize(ManyToOneRelationalState state) { + public final void initialize(RelationalState state) { super.initialize( state ); isLogicalOneToOne = state.isLogicalOneToOne(); foreignKeyName = state.getForeignKeyName(); 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 fd90fd6c35..39f27f270f 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 @@ -23,7 +23,6 @@ */ package org.hibernate.metamodel.binding; -import org.hibernate.MappingException; import org.hibernate.mapping.PropertyGeneration; /** @@ -47,16 +46,16 @@ public class SimpleAttributeBinding extends SingularAttributeBinding { generation = state.getPropertyGeneration(); } - public final void initializeTupleValue(SimpleTupleRelationalState state) { - if ( state.getRelationalStates().size() == 0 ) { - throw new MappingException( "Tuple state does not contain any values." ); - } - if ( state.getRelationalStates().size() == 1 ) { - initializeSingleValue( state.getRelationalStates().iterator().next() ); - } - else { - initializeTupleValue( state.getRelationalStates() ); - } + public void initializeColumnValue(ColumnRelationalState state) { + super.initializeColumnValue( state ); + } + + public void initializeDerivedValue(DerivedRelationalState state) { + super.initializeDerivedValue( state ); + } + + public void initializeSimpleTupleValue(SimpleTupleRelationalState state) { + super.initializeSimpleTupleValue( state ); } private boolean isUnique(ColumnRelationalState state) { 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/EntityBinder.java index e05ed7c88b..219781b4cb 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/EntityBinder.java @@ -23,7 +23,7 @@ */ package org.hibernate.metamodel.source.annotations; -import java.util.LinkedHashSet; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; @@ -35,6 +35,8 @@ import org.hibernate.MappingException; import org.hibernate.cfg.NamingStrategy; import org.hibernate.internal.util.StringHelper; import org.hibernate.mapping.PropertyGeneration; +import org.hibernate.metamodel.binding.AbstractAttributeBinding; +import org.hibernate.metamodel.binding.AttributeBinding; import org.hibernate.metamodel.binding.EntityBinding; import org.hibernate.metamodel.binding.HibernateTypeDescriptor; import org.hibernate.metamodel.binding.SimpleAttributeBinding; @@ -144,7 +146,7 @@ public class EntityBinder { AnnotationSimpleAttributeRelationalState relationalState = new AnnotationSimpleAttributeRelationalState(); relationalState.valueStates.add( columnRelationsState ); - idBinding.initializeTupleValue( relationalState ); + idBinding.initializeSimpleTupleValue( relationalState ); } private void bindHibernateEntityAnnotation(EntityBinding entityBinding) { @@ -316,10 +318,10 @@ public class EntityBinder { public static class AnnotationSimpleAttributeRelationalState implements SimpleAttributeBinding.SimpleTupleRelationalState { - LinkedHashSet valueStates = new LinkedHashSet(); + List valueStates = new ArrayList(); @Override - public LinkedHashSet getRelationalStates() { + public List getRelationalStates() { return valueStates; } } 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 64de6d27d6..759713c64a 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 @@ -23,16 +23,12 @@ */ package org.hibernate.metamodel.source.hbm; -import java.sql.ResultSet; -import java.util.Iterator; - import org.dom4j.Attribute; -import org.dom4j.Element; + import org.hibernate.EntityMode; import org.hibernate.MappingException; import org.hibernate.cfg.NamingStrategy; import org.hibernate.engine.Versioning; -import org.hibernate.engine.jdbc.batch.spi.Batch; import org.hibernate.internal.util.StringHelper; import org.hibernate.metamodel.binding.AttributeBinding; import org.hibernate.metamodel.binding.BagBinding; @@ -42,7 +38,6 @@ import org.hibernate.metamodel.binding.PluralAttributeBinding; import org.hibernate.metamodel.binding.SimpleAttributeBinding; import org.hibernate.metamodel.domain.Entity; import org.hibernate.metamodel.domain.Hierarchical; -import org.hibernate.metamodel.domain.PluralAttribute; import org.hibernate.metamodel.domain.PluralAttributeNature; import org.hibernate.metamodel.relational.Schema; import org.hibernate.metamodel.relational.Table; @@ -440,7 +435,7 @@ PrimitiveArray 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( + attributeBinding.initializeSimpleTupleValue( new HbmSimpleValueRelationalStateContainer( getHibernateMappingBinder(), true, @@ -468,7 +463,7 @@ PrimitiveArray 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( + attributeBinding.initializeSimpleTupleValue( new HbmSimpleValueRelationalStateContainer( getHibernateMappingBinder(), true, @@ -496,7 +491,7 @@ PrimitiveArray 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( + attributeBinding.initializeSimpleTupleValue( new HbmSimpleValueRelationalStateContainer( getHibernateMappingBinder(), true, @@ -524,7 +519,7 @@ PrimitiveArray 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( + attributeBinding.initializeSimpleTupleValue( new HbmSimpleValueRelationalStateContainer( getHibernateMappingBinder(), true, @@ -551,7 +546,7 @@ PrimitiveArray 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( + attributeBinding.initializeSimpleTupleValue( new HbmSimpleValueRelationalStateContainer( getHibernateMappingBinder(), true, 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 58e69abb6c..b71ca3330b 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 @@ -23,6 +23,7 @@ */ package org.hibernate.metamodel.source.hbm.state.relational; +import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; @@ -31,6 +32,7 @@ import java.util.Set; import org.hibernate.MappingException; import org.hibernate.cfg.NamingStrategy; import org.hibernate.metamodel.binding.AbstractAttributeBinding; +import org.hibernate.metamodel.binding.AttributeBinding; import org.hibernate.metamodel.binding.HibernateTypeDescriptor; import org.hibernate.metamodel.binding.MappingDefaults; import org.hibernate.metamodel.binding.SimpleAttributeBinding; @@ -49,7 +51,7 @@ public class HbmSimpleValueRelationalStateContainer implements AbstractAttribute private final MappingDefaults defaults; private final Set propertyUniqueKeys; private final Set propertyIndexes; - private final Set singleValueStates; + private final List singleValueStates; private final HibernateTypeDescriptor hibernateTypeDescriptor = new HibernateTypeDescriptor(); public NamingStrategy getNamingStrategy() { @@ -169,7 +171,7 @@ public class HbmSimpleValueRelationalStateContainer implements AbstractAttribute this.defaults = defaults; this.propertyUniqueKeys = Collections.emptySet(); this.propertyIndexes = Collections.emptySet(); - singleValueStates = new LinkedHashSet( + singleValueStates = new ArrayList( mappedColumnsOrFormulas == null || mappedColumnsOrFormulas.isEmpty() ? 1 : mappedColumnsOrFormulas.size() @@ -181,7 +183,7 @@ public class HbmSimpleValueRelationalStateContainer implements AbstractAttribute } } - private static SimpleAttributeBinding.SingleValueRelationalState createColumnOrFormulaRelationalState( + private static AttributeBinding.SingleValueRelationalState createColumnOrFormulaRelationalState( HbmSimpleValueRelationalStateContainer container, Object columnOrFormula) { if ( XMLColumnElement.class.isInstance( columnOrFormula ) ) { @@ -196,7 +198,7 @@ public class HbmSimpleValueRelationalStateContainer implements AbstractAttribute throw new MappingException( "unknown type of column or formula: " + columnOrFormula.getClass().getName() ); } - public Set getRelationalStates() { + public List getRelationalStates() { return singleValueStates; } diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/SimpleValueBindingTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/SimpleValueBindingTests.java index 9958d500f8..ffe2103119 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/SimpleValueBindingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/SimpleValueBindingTests.java @@ -68,6 +68,6 @@ public class SimpleValueBindingTests extends BaseUnitTestCase { idColumn.setSize( Size.precision( 18, 0 ) ); table.getPrimaryKey().addColumn( idColumn ); table.getPrimaryKey().setName( "my_table_pk" ); - attributeBinding.setValue( idColumn ); + //attributeBinding.setValue( idColumn ); } }