HHH-6168 : Misc fixes to many-to-one AttributeBinding

This commit is contained in:
Gail Badner 2011-05-01 19:42:18 -07:00
parent b667767f85
commit 99cec1404f
8 changed files with 91 additions and 87 deletions

View File

@ -31,13 +31,11 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.metamodel.domain.Attribute; import org.hibernate.metamodel.domain.Attribute;
import org.hibernate.metamodel.domain.MetaAttribute; import org.hibernate.metamodel.domain.MetaAttribute;
import org.hibernate.metamodel.relational.Column; import org.hibernate.metamodel.relational.Column;
import org.hibernate.metamodel.relational.DerivedValue; import org.hibernate.metamodel.relational.DerivedValue;
import org.hibernate.metamodel.relational.SimpleValue; import org.hibernate.metamodel.relational.SimpleValue;
import org.hibernate.metamodel.relational.Size;
import org.hibernate.metamodel.relational.TableSpecification; import org.hibernate.metamodel.relational.TableSpecification;
import org.hibernate.metamodel.relational.Tuple; import org.hibernate.metamodel.relational.Tuple;
import org.hibernate.metamodel.relational.Value; import org.hibernate.metamodel.relational.Value;
@ -83,7 +81,7 @@ public abstract class AbstractAttributeBinding implements AttributeBinding {
this.entityBinding = entityBinding; this.entityBinding = entityBinding;
} }
public void initialize(DomainState state) { protected void initialize(DomainState state) {
hibernateTypeDescriptor.initialize( state.getHibernateTypeDescriptor() ); hibernateTypeDescriptor.initialize( state.getHibernateTypeDescriptor() );
attribute = state.getAttribute(); attribute = state.getAttribute();
isLazy = state.isLazy(); isLazy = state.isLazy();
@ -118,8 +116,7 @@ public abstract class AbstractAttributeBinding implements AttributeBinding {
} }
protected void initializeColumnValue(ColumnRelationalState state) { protected void initializeColumnValue(ColumnRelationalState state) {
Column columnValue = createColumn( state ); value = createColumn( state );
setValue( columnValue );
} }
private Column createColumn(ColumnRelationalState state) { private Column createColumn(ColumnRelationalState state) {
@ -151,17 +148,20 @@ public abstract class AbstractAttributeBinding implements AttributeBinding {
return columnValue; return columnValue;
} }
public final void initialize(RelationalState state) { // TODO: move this logic out...
protected void initialize(RelationalState state) {
if ( SingleValueRelationalState.class.isInstance( state ) ) { if ( SingleValueRelationalState.class.isInstance( state ) ) {
initializeSingleValue( SingleValueRelationalState.class.cast( state ) ); initializeSingleValue( SingleValueRelationalState.class.cast( state ) );
} }
else if ( SimpleTupleRelationalState.class.isInstance( 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() );
} }
} }
protected <T extends DerivedRelationalState> void initializeDerivedValue(T state) {
public final <T extends DerivedRelationalState> void initializeDerivedValue(T state) {
value = createDerivedValue( state ); value = createDerivedValue( state );
} }
@ -169,11 +169,11 @@ public abstract class AbstractAttributeBinding implements AttributeBinding {
return getEntityBinding().getBaseTable().createDerivedValue( state.getFormula() ); return getEntityBinding().getBaseTable().createDerivedValue( state.getFormula() );
} }
public final void initializeSingleValue(SingleValueRelationalState state) { private void initializeSingleValue(SingleValueRelationalState state) {
value = createSingleValue( state ); value = createSingleValue( state );
} }
protected SimpleValue createSingleValue(SingleValueRelationalState state) { private SimpleValue createSingleValue(SingleValueRelationalState state) {
if ( state instanceof ColumnRelationalState ) { if ( state instanceof ColumnRelationalState ) {
return createColumn( ColumnRelationalState.class.cast( state ) ); return createColumn( ColumnRelationalState.class.cast( state ) );
} }
@ -185,31 +185,31 @@ public abstract class AbstractAttributeBinding implements AttributeBinding {
} }
} }
protected final void initializeTupleValue(Set<SingleValueRelationalState> singleValueStates) { protected void initializeSimpleTupleValue(SimpleTupleRelationalState state) {
if ( singleValueStates.size() == 0 ) { if ( state.getRelationalStates().size() == 0 ) {
throw new MappingException( "Tuple state does not contain any values." ); throw new MappingException( "Tuple state does not contain any values." );
} }
if ( singleValueStates.size() == 1 ) { if ( state.getRelationalStates().size() == 1 ) {
initializeSingleValue( singleValueStates.iterator().next() ); initializeSingleValue( state.getRelationalStates().iterator().next() );
} }
else { else {
Tuple tuple = getEntityBinding().getBaseTable().createTuple( "[" + getAttribute().getName() + "]" ); value = createSimpleTupleValue( state );
for ( SingleValueRelationalState singleValueState : singleValueStates ) {
tuple.addValue( createSingleValue( singleValueState ) );
}
value = tuple;
} }
} }
private Tuple createSimpleTupleValue(SimpleTupleRelationalState state) {
Tuple tuple = getEntityBinding().getBaseTable().createTuple( "[" + getAttribute().getName() + "]" );
for ( SingleValueRelationalState singleValueState : state.getRelationalStates() ) {
tuple.addValue( createSingleValue( singleValueState ) );
}
return tuple;
}
@Override @Override
public Value getValue() { public Value getValue() {
return value; return value;
} }
protected void setValue(Value value) {
this.value = value;
}
@Override @Override
public HibernateTypeDescriptor getHibernateTypeDescriptor() { public HibernateTypeDescriptor getHibernateTypeDescriptor() {
return hibernateTypeDescriptor; 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<String> getUniqueKeys();
Set<String> getIndexes();
}
public static interface DerivedRelationalState extends SingleValueRelationalState {
String getFormula();
}
public static interface SimpleTupleRelationalState extends TupleRelationalState<SingleValueRelationalState> {
}
public static interface TupleRelationalState<T extends RelationalState> extends RelationalState{
Set<T> getRelationalStates();
}
} }

View File

@ -23,12 +23,15 @@
*/ */
package org.hibernate.metamodel.binding; package org.hibernate.metamodel.binding;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.metamodel.domain.Attribute; import org.hibernate.metamodel.domain.Attribute;
import org.hibernate.metamodel.domain.MetaAttribute; import org.hibernate.metamodel.domain.MetaAttribute;
import org.hibernate.metamodel.relational.SimpleValue; import org.hibernate.metamodel.relational.SimpleValue;
import org.hibernate.metamodel.relational.Size;
import org.hibernate.metamodel.relational.TableSpecification; import org.hibernate.metamodel.relational.TableSpecification;
import org.hibernate.metamodel.relational.Value; import org.hibernate.metamodel.relational.Value;
@ -108,4 +111,37 @@ public interface AttributeBinding {
public Set<EntityReferencingAttributeBinding> getEntityReferencingAttributeBindings(); public Set<EntityReferencingAttributeBinding> getEntityReferencingAttributeBindings();
public void validate(); 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<String> getUniqueKeys();
Set<String> getIndexes();
}
interface DerivedRelationalState extends SingleValueRelationalState {
String getFormula();
}
interface SimpleTupleRelationalState extends AbstractAttributeBinding.TupleRelationalState<SingleValueRelationalState> {
}
interface TupleRelationalState<T extends RelationalState> extends RelationalState{
List<T> getRelationalStates();
}
} }

View File

@ -52,7 +52,7 @@ public class ManyToOneAttributeBinding extends SingularAttributeBinding implemen
boolean ignoreNotFound(); boolean ignoreNotFound();
} }
public static interface ManyToOneRelationalState extends RelationalState { public static interface RelationalState extends AttributeBinding.RelationalState {
boolean isLogicalOneToOne(); boolean isLogicalOneToOne();
String getForeignKeyName(); 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 ); super.initialize( state );
isLogicalOneToOne = state.isLogicalOneToOne(); isLogicalOneToOne = state.isLogicalOneToOne();
foreignKeyName = state.getForeignKeyName(); foreignKeyName = state.getForeignKeyName();

View File

@ -23,7 +23,6 @@
*/ */
package org.hibernate.metamodel.binding; package org.hibernate.metamodel.binding;
import org.hibernate.MappingException;
import org.hibernate.mapping.PropertyGeneration; import org.hibernate.mapping.PropertyGeneration;
/** /**
@ -47,16 +46,16 @@ public class SimpleAttributeBinding extends SingularAttributeBinding {
generation = state.getPropertyGeneration(); generation = state.getPropertyGeneration();
} }
public final void initializeTupleValue(SimpleTupleRelationalState state) { public void initializeColumnValue(ColumnRelationalState state) {
if ( state.getRelationalStates().size() == 0 ) { super.initializeColumnValue( state );
throw new MappingException( "Tuple state does not contain any values." ); }
}
if ( state.getRelationalStates().size() == 1 ) { public void initializeDerivedValue(DerivedRelationalState state) {
initializeSingleValue( state.getRelationalStates().iterator().next() ); super.initializeDerivedValue( state );
} }
else {
initializeTupleValue( state.getRelationalStates() ); public void initializeSimpleTupleValue(SimpleTupleRelationalState state) {
} super.initializeSimpleTupleValue( state );
} }
private boolean isUnique(ColumnRelationalState state) { private boolean isUnique(ColumnRelationalState state) {

View File

@ -23,7 +23,7 @@
*/ */
package org.hibernate.metamodel.source.annotations; package org.hibernate.metamodel.source.annotations;
import java.util.LinkedHashSet; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -35,6 +35,8 @@ import org.hibernate.MappingException;
import org.hibernate.cfg.NamingStrategy; import org.hibernate.cfg.NamingStrategy;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.mapping.PropertyGeneration; 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.EntityBinding;
import org.hibernate.metamodel.binding.HibernateTypeDescriptor; import org.hibernate.metamodel.binding.HibernateTypeDescriptor;
import org.hibernate.metamodel.binding.SimpleAttributeBinding; import org.hibernate.metamodel.binding.SimpleAttributeBinding;
@ -144,7 +146,7 @@ public class EntityBinder {
AnnotationSimpleAttributeRelationalState relationalState = new AnnotationSimpleAttributeRelationalState(); AnnotationSimpleAttributeRelationalState relationalState = new AnnotationSimpleAttributeRelationalState();
relationalState.valueStates.add( columnRelationsState ); relationalState.valueStates.add( columnRelationsState );
idBinding.initializeTupleValue( relationalState ); idBinding.initializeSimpleTupleValue( relationalState );
} }
private void bindHibernateEntityAnnotation(EntityBinding entityBinding) { private void bindHibernateEntityAnnotation(EntityBinding entityBinding) {
@ -316,10 +318,10 @@ public class EntityBinder {
public static class AnnotationSimpleAttributeRelationalState public static class AnnotationSimpleAttributeRelationalState
implements SimpleAttributeBinding.SimpleTupleRelationalState { implements SimpleAttributeBinding.SimpleTupleRelationalState {
LinkedHashSet<SimpleAttributeBinding.SingleValueRelationalState> valueStates = new LinkedHashSet<SimpleAttributeBinding.SingleValueRelationalState>(); List<AttributeBinding.SingleValueRelationalState> valueStates = new ArrayList<AttributeBinding.SingleValueRelationalState>();
@Override @Override
public LinkedHashSet<SimpleAttributeBinding.SingleValueRelationalState> getRelationalStates() { public List<AttributeBinding.SingleValueRelationalState> getRelationalStates() {
return valueStates; return valueStates;
} }
} }

View File

@ -23,16 +23,12 @@
*/ */
package org.hibernate.metamodel.source.hbm; package org.hibernate.metamodel.source.hbm;
import java.sql.ResultSet;
import java.util.Iterator;
import org.dom4j.Attribute; import org.dom4j.Attribute;
import org.dom4j.Element;
import org.hibernate.EntityMode; import org.hibernate.EntityMode;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.cfg.NamingStrategy; import org.hibernate.cfg.NamingStrategy;
import org.hibernate.engine.Versioning; import org.hibernate.engine.Versioning;
import org.hibernate.engine.jdbc.batch.spi.Batch;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.binding.AttributeBinding; import org.hibernate.metamodel.binding.AttributeBinding;
import org.hibernate.metamodel.binding.BagBinding; 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.binding.SimpleAttributeBinding;
import org.hibernate.metamodel.domain.Entity; import org.hibernate.metamodel.domain.Entity;
import org.hibernate.metamodel.domain.Hierarchical; import org.hibernate.metamodel.domain.Hierarchical;
import org.hibernate.metamodel.domain.PluralAttribute;
import org.hibernate.metamodel.domain.PluralAttributeNature; import org.hibernate.metamodel.domain.PluralAttributeNature;
import org.hibernate.metamodel.relational.Schema; import org.hibernate.metamodel.relational.Schema;
import org.hibernate.metamodel.relational.Table; import org.hibernate.metamodel.relational.Table;
@ -440,7 +435,7 @@ PrimitiveArray
if ( attributeBinding.getValue() == null ) { if ( attributeBinding.getValue() == null ) {
// relational model has not been bound yet // relational model has not been bound yet
// boolean (true here) indicates that by default column names should be guessed // boolean (true here) indicates that by default column names should be guessed
attributeBinding.initializeTupleValue( attributeBinding.initializeSimpleTupleValue(
new HbmSimpleValueRelationalStateContainer( new HbmSimpleValueRelationalStateContainer(
getHibernateMappingBinder(), getHibernateMappingBinder(),
true, true,
@ -468,7 +463,7 @@ PrimitiveArray
if ( attributeBinding.getValue() == null ) { if ( attributeBinding.getValue() == null ) {
// relational model has not been bound yet // relational model has not been bound yet
// boolean (true here) indicates that by default column names should be guessed // boolean (true here) indicates that by default column names should be guessed
attributeBinding.initializeTupleValue( attributeBinding.initializeSimpleTupleValue(
new HbmSimpleValueRelationalStateContainer( new HbmSimpleValueRelationalStateContainer(
getHibernateMappingBinder(), getHibernateMappingBinder(),
true, true,
@ -496,7 +491,7 @@ PrimitiveArray
if ( attributeBinding.getValue() == null ) { if ( attributeBinding.getValue() == null ) {
// relational model has not been bound yet // relational model has not been bound yet
// boolean (true here) indicates that by default column names should be guessed // boolean (true here) indicates that by default column names should be guessed
attributeBinding.initializeTupleValue( attributeBinding.initializeSimpleTupleValue(
new HbmSimpleValueRelationalStateContainer( new HbmSimpleValueRelationalStateContainer(
getHibernateMappingBinder(), getHibernateMappingBinder(),
true, true,
@ -524,7 +519,7 @@ PrimitiveArray
if ( attributeBinding.getValue() == null ) { if ( attributeBinding.getValue() == null ) {
// relational model has not been bound yet // relational model has not been bound yet
// boolean (true here) indicates that by default column names should be guessed // boolean (true here) indicates that by default column names should be guessed
attributeBinding.initializeTupleValue( attributeBinding.initializeSimpleTupleValue(
new HbmSimpleValueRelationalStateContainer( new HbmSimpleValueRelationalStateContainer(
getHibernateMappingBinder(), getHibernateMappingBinder(),
true, true,
@ -551,7 +546,7 @@ PrimitiveArray
if ( attributeBinding.getValue() == null ) { if ( attributeBinding.getValue() == null ) {
// relational model has not been bound yet // relational model has not been bound yet
// boolean (true here) indicates that by default column names should be guessed // boolean (true here) indicates that by default column names should be guessed
attributeBinding.initializeTupleValue( attributeBinding.initializeSimpleTupleValue(
new HbmSimpleValueRelationalStateContainer( new HbmSimpleValueRelationalStateContainer(
getHibernateMappingBinder(), getHibernateMappingBinder(),
true, true,

View File

@ -23,6 +23,7 @@
*/ */
package org.hibernate.metamodel.source.hbm.state.relational; package org.hibernate.metamodel.source.hbm.state.relational;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
@ -31,6 +32,7 @@ import java.util.Set;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.cfg.NamingStrategy; import org.hibernate.cfg.NamingStrategy;
import org.hibernate.metamodel.binding.AbstractAttributeBinding; import org.hibernate.metamodel.binding.AbstractAttributeBinding;
import org.hibernate.metamodel.binding.AttributeBinding;
import org.hibernate.metamodel.binding.HibernateTypeDescriptor; import org.hibernate.metamodel.binding.HibernateTypeDescriptor;
import org.hibernate.metamodel.binding.MappingDefaults; import org.hibernate.metamodel.binding.MappingDefaults;
import org.hibernate.metamodel.binding.SimpleAttributeBinding; import org.hibernate.metamodel.binding.SimpleAttributeBinding;
@ -49,7 +51,7 @@ public class HbmSimpleValueRelationalStateContainer implements AbstractAttribute
private final MappingDefaults defaults; private final MappingDefaults defaults;
private final Set<String> propertyUniqueKeys; private final Set<String> propertyUniqueKeys;
private final Set<String> propertyIndexes; private final Set<String> propertyIndexes;
private final Set<SimpleAttributeBinding.SingleValueRelationalState> singleValueStates; private final List<AttributeBinding.SingleValueRelationalState> singleValueStates;
private final HibernateTypeDescriptor hibernateTypeDescriptor = new HibernateTypeDescriptor(); private final HibernateTypeDescriptor hibernateTypeDescriptor = new HibernateTypeDescriptor();
public NamingStrategy getNamingStrategy() { public NamingStrategy getNamingStrategy() {
@ -169,7 +171,7 @@ public class HbmSimpleValueRelationalStateContainer implements AbstractAttribute
this.defaults = defaults; this.defaults = defaults;
this.propertyUniqueKeys = Collections.emptySet(); this.propertyUniqueKeys = Collections.emptySet();
this.propertyIndexes = Collections.emptySet(); this.propertyIndexes = Collections.emptySet();
singleValueStates = new LinkedHashSet<SimpleAttributeBinding.SingleValueRelationalState>( singleValueStates = new ArrayList<AttributeBinding.SingleValueRelationalState>(
mappedColumnsOrFormulas == null || mappedColumnsOrFormulas.isEmpty() ? mappedColumnsOrFormulas == null || mappedColumnsOrFormulas.isEmpty() ?
1 : 1 :
mappedColumnsOrFormulas.size() mappedColumnsOrFormulas.size()
@ -181,7 +183,7 @@ public class HbmSimpleValueRelationalStateContainer implements AbstractAttribute
} }
} }
private static SimpleAttributeBinding.SingleValueRelationalState createColumnOrFormulaRelationalState( private static AttributeBinding.SingleValueRelationalState createColumnOrFormulaRelationalState(
HbmSimpleValueRelationalStateContainer container, HbmSimpleValueRelationalStateContainer container,
Object columnOrFormula) { Object columnOrFormula) {
if ( XMLColumnElement.class.isInstance( 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() ); throw new MappingException( "unknown type of column or formula: " + columnOrFormula.getClass().getName() );
} }
public Set<SimpleAttributeBinding.SingleValueRelationalState> getRelationalStates() { public List<AttributeBinding.SingleValueRelationalState> getRelationalStates() {
return singleValueStates; return singleValueStates;
} }

View File

@ -68,6 +68,6 @@ public class SimpleValueBindingTests extends BaseUnitTestCase {
idColumn.setSize( Size.precision( 18, 0 ) ); idColumn.setSize( Size.precision( 18, 0 ) );
table.getPrimaryKey().addColumn( idColumn ); table.getPrimaryKey().addColumn( idColumn );
table.getPrimaryKey().setName( "my_table_pk" ); table.getPrimaryKey().setName( "my_table_pk" );
attributeBinding.setValue( idColumn ); //attributeBinding.setValue( idColumn );
} }
} }