From 85c63dbaf0bf8b4a84f7bccbf540e127ec61a174 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Thu, 28 Jul 2011 11:37:51 -0500 Subject: [PATCH] Simple code cleanup --- .../relational/AbstractConstraint.java | 13 +++- .../AbstractTableSpecification.java | 12 ++-- .../metamodel/relational/ForeignKey.java | 68 +++++++++---------- 3 files changed, 51 insertions(+), 42 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractConstraint.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractConstraint.java index 317829fc16..9410c8e138 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractConstraint.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractConstraint.java @@ -26,6 +26,7 @@ package org.hibernate.metamodel.relational; import java.util.List; import java.util.ArrayList; +import org.hibernate.AssertionFailure; import org.hibernate.dialect.Dialect; /** @@ -67,8 +68,18 @@ public abstract class AbstractConstraint implements Constraint { } public void addColumn(Column column) { + internalAddColumn( column ); + } + + protected void internalAddColumn(Column column) { if ( column.getTable() != getTable() ) { - throw new IllegalArgumentException( "Unable to add column to constraint; tables did not match" ); + throw new AssertionFailure( + String.format( + "Unable to add column to constraint; tables [%s, %s] did not match", + column.getTable().toLoggableString(), + getTable().toLoggableString() + ) + ); } columns.add( column ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractTableSpecification.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractTableSpecification.java index d768d9df1e..3165c6b107 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractTableSpecification.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/AbstractTableSpecification.java @@ -30,16 +30,18 @@ import java.util.concurrent.atomic.AtomicInteger; /** * Convenience base class for implementing the {@link ValueContainer} contract centralizing commonality - * between modelling tables views and inline views. + * between modeling tables, views and inline views. * * @author Steve Ebersole */ public abstract class AbstractTableSpecification implements TableSpecification { private final static AtomicInteger tableCounter = new AtomicInteger( 0 ); private final int tableNumber; + private final LinkedHashMap values = new LinkedHashMap(); - private PrimaryKey primaryKey = new PrimaryKey( this ); - private List foreignKeys = new ArrayList(); + + private final PrimaryKey primaryKey = new PrimaryKey( this ); + private final List foreignKeys = new ArrayList(); public AbstractTableSpecification() { this.tableNumber = tableCounter.getAndIncrement(); @@ -92,9 +94,7 @@ public abstract class AbstractTableSpecification implements TableSpecification { return fk; } - /** - * {@inheritDoc} - */ + @Override public PrimaryKey getPrimaryKey() { return primaryKey; } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/ForeignKey.java b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/ForeignKey.java index 9a03f9d3b2..bcb8c84510 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/relational/ForeignKey.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/relational/ForeignKey.java @@ -27,9 +27,9 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import org.hibernate.AssertionFailure; import org.hibernate.MappingException; import org.hibernate.dialect.Dialect; -import org.hibernate.internal.CoreMessageLogger; import org.jboss.logging.Logger; @@ -43,15 +43,16 @@ import org.jboss.logging.Logger; * @author Steve Ebersole */ public class ForeignKey extends AbstractConstraint implements Constraint, Exportable { + private static final Logger LOG = Logger.getLogger( ForeignKey.class ); - private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, AbstractConstraint.class.getName()); private static final String ON_DELETE = " on delete "; private static final String ON_UPDATE = " on update "; private final TableSpecification targetTable; private List targetColumns; + private ReferentialAction deleteRule = ReferentialAction.NO_ACTION; - public ReferentialAction updateRule = ReferentialAction.NO_ACTION; + private ReferentialAction updateRule = ReferentialAction.NO_ACTION; protected ForeignKey(TableSpecification sourceTable, TableSpecification targetTable, String name) { super( sourceTable, name ); @@ -96,6 +97,7 @@ public class ForeignKey extends AbstractConstraint implements Constraint, Export } } else { + checkTargetTable( targetColumn ); if ( targetColumns == null ) { if (!internalColumnAccess().isEmpty()) { LOG.debugf( @@ -109,7 +111,19 @@ public class ForeignKey extends AbstractConstraint implements Constraint, Export } targetColumns.add( targetColumn ); } - internalColumnAccess().add( sourceColumn ); + internalAddColumn( sourceColumn ); + } + + private void checkTargetTable(Column targetColumn) { + if ( targetColumn.getTable() != getTargetTable() ) { + throw new AssertionFailure( + String.format( + "Unable to add column to constraint; tables [%s, %s] did not match", + targetColumn.getTable().toLoggableString(), + getTargetTable().toLoggableString() + ) + ); + } } @Override @@ -183,36 +197,20 @@ public class ForeignKey extends AbstractConstraint implements Constraint, Export } public static enum ReferentialAction { - NO_ACTION { - private static final String ACTION_STRING = "no action"; - public String getActionString() { - return ACTION_STRING; - } - }, - CASCADE { - private static final String ACTION_STRING = "cascade"; - public String getActionString() { - return ACTION_STRING; - } - }, - SET_NULL { - private static final String ACTION_STRING = "set null"; - public String getActionString() { - return ACTION_STRING; - } - }, - SET_DEFAULT { - private static final String ACTION_STRING = "set default"; - public String getActionString() { - return ACTION_STRING; - } - }, - RESTRICT { - private static final String ACTION_STRING = "restrict"; - public String getActionString() { - return ACTION_STRING; - } - }; - public abstract String getActionString(); + NO_ACTION( "no action" ), + CASCADE( "cascade" ), + SET_NULL( "set null" ), + SET_DEFAULT( "set default" ), + RESTRICT( "restrict" ); + + private final String actionString; + + private ReferentialAction(String actionString) { + this.actionString = actionString; + } + + public String getActionString() { + return actionString; + } } }