HHH-7088 - Implement secondary table support in new metamodel code

This commit is contained in:
Steve Ebersole 2012-02-20 18:01:50 -06:00
parent 9c0d625235
commit 3eccabb79a
35 changed files with 1014 additions and 435 deletions

View File

@ -30,7 +30,7 @@ import java.util.List;
* *
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public interface PluralAttributeElement extends MetaAttributeContainer { public interface PluralAttributeElement extends TableInformationSource, MetaAttributeContainer {
public String getName(); public String getName();
public String getAccess(); public String getAccess();
@ -42,13 +42,8 @@ public interface PluralAttributeElement extends MetaAttributeContainer {
public JaxbManyToManyElement getManyToMany(); public JaxbManyToManyElement getManyToMany();
public JaxbManyToAnyElement getManyToAny(); public JaxbManyToAnyElement getManyToAny();
public String getSchema();
public String getCatalog();
public String getTable();
public String getComment(); public String getComment();
public String getCheck(); public String getCheck();
public String getSubselect();
public String getSubselectAttribute();
public String getWhere(); public String getWhere();
public JaxbLoaderElement getLoader(); public JaxbLoaderElement getLoader();

View File

@ -0,0 +1,35 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, 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.internal.jaxb.mapping.hbm;
/**
* @author Steve Ebersole
*/
public interface TableInformationSource {
public String getSchema();
public String getCatalog();
public String getTable();
public String getSubselect();
public String getSubselectAttribute();
}

View File

@ -69,6 +69,7 @@ import org.hibernate.metamodel.spi.relational.Column;
import org.hibernate.metamodel.spi.relational.DerivedValue; import org.hibernate.metamodel.spi.relational.DerivedValue;
import org.hibernate.metamodel.spi.relational.ForeignKey; import org.hibernate.metamodel.spi.relational.ForeignKey;
import org.hibernate.metamodel.spi.relational.Identifier; import org.hibernate.metamodel.spi.relational.Identifier;
import org.hibernate.metamodel.spi.relational.InLineView;
import org.hibernate.metamodel.spi.relational.Schema; import org.hibernate.metamodel.spi.relational.Schema;
import org.hibernate.metamodel.spi.relational.Table; import org.hibernate.metamodel.spi.relational.Table;
import org.hibernate.metamodel.spi.relational.TableSpecification; import org.hibernate.metamodel.spi.relational.TableSpecification;
@ -85,6 +86,7 @@ import org.hibernate.metamodel.spi.source.DerivedValueSource;
import org.hibernate.metamodel.spi.source.DiscriminatorSource; import org.hibernate.metamodel.spi.source.DiscriminatorSource;
import org.hibernate.metamodel.spi.source.EntityHierarchy; import org.hibernate.metamodel.spi.source.EntityHierarchy;
import org.hibernate.metamodel.spi.source.EntitySource; import org.hibernate.metamodel.spi.source.EntitySource;
import org.hibernate.metamodel.spi.source.InLineViewSource;
import org.hibernate.metamodel.spi.source.LocalBindingContext; import org.hibernate.metamodel.spi.source.LocalBindingContext;
import org.hibernate.metamodel.spi.source.MappingException; import org.hibernate.metamodel.spi.source.MappingException;
import org.hibernate.metamodel.spi.source.MetaAttributeContext; import org.hibernate.metamodel.spi.source.MetaAttributeContext;
@ -97,6 +99,7 @@ import org.hibernate.metamodel.spi.source.PluralAttributeSource;
import org.hibernate.metamodel.spi.source.RelationalValueSource; import org.hibernate.metamodel.spi.source.RelationalValueSource;
import org.hibernate.metamodel.spi.source.RelationalValueSourceContainer; import org.hibernate.metamodel.spi.source.RelationalValueSourceContainer;
import org.hibernate.metamodel.spi.source.RootEntitySource; import org.hibernate.metamodel.spi.source.RootEntitySource;
import org.hibernate.metamodel.spi.source.SecondaryTableSource;
import org.hibernate.metamodel.spi.source.SimpleIdentifierSource; import org.hibernate.metamodel.spi.source.SimpleIdentifierSource;
import org.hibernate.metamodel.spi.source.SingularAttributeNature; import org.hibernate.metamodel.spi.source.SingularAttributeNature;
import org.hibernate.metamodel.spi.source.SingularAttributeSource; import org.hibernate.metamodel.spi.source.SingularAttributeSource;
@ -104,6 +107,7 @@ import org.hibernate.metamodel.spi.source.Sortable;
import org.hibernate.metamodel.spi.source.SubclassEntityContainer; import org.hibernate.metamodel.spi.source.SubclassEntityContainer;
import org.hibernate.metamodel.spi.source.SubclassEntitySource; import org.hibernate.metamodel.spi.source.SubclassEntitySource;
import org.hibernate.metamodel.spi.source.TableSource; import org.hibernate.metamodel.spi.source.TableSource;
import org.hibernate.metamodel.spi.source.TableSpecificationSource;
import org.hibernate.metamodel.spi.source.ToOneAttributeSource; import org.hibernate.metamodel.spi.source.ToOneAttributeSource;
import org.hibernate.metamodel.spi.source.UniqueConstraintSource; import org.hibernate.metamodel.spi.source.UniqueConstraintSource;
import org.hibernate.metamodel.spi.source.VersionAttributeSource; import org.hibernate.metamodel.spi.source.VersionAttributeSource;
@ -974,51 +978,37 @@ public class Binder {
// } // }
private void bindCollectionTable( private void bindCollectionTable(
PluralAttributeSource attributeSource, final PluralAttributeSource attributeSource,
AbstractPluralAttributeBinding pluralAttributeBinding) { final AbstractPluralAttributeBinding pluralAttributeBinding) {
if ( attributeSource.getElementSource().getNature() == org.hibernate.metamodel.spi.source.PluralAttributeElementNature.ONE_TO_MANY ) { if ( attributeSource.getElementSource().getNature() == org.hibernate.metamodel.spi.source.PluralAttributeElementNature.ONE_TO_MANY ) {
return; return;
} }
final Schema.Name schemaName = Helper.determineDatabaseSchemaName( TableSpecificationSource tableSpecificationSource = attributeSource.getCollectionTableSpecificationSource();
attributeSource.getExplicitSchemaName(), if ( TableSource.class.isInstance( tableSpecificationSource ) ) {
attributeSource.getExplicitCatalogName(), Table collectionTable = createTable(
currentBindingContext (TableSource) tableSpecificationSource,
); new InferredNamingStrategy() {
final Schema schema = metadata.getDatabase().locateSchema( schemaName ); @Override
public String inferredTableName() {
final String tableName = attributeSource.getExplicitCollectionTableName(); final EntityBinding owner = pluralAttributeBinding.getContainer().seekEntityBinding();
if ( StringHelper.isNotEmpty( tableName ) ) { final String ownerTableLogicalName = Table.class.isInstance( owner.getPrimaryTable() )
final Identifier tableIdentifier = Identifier.toIdentifier( ? Table.class.cast( owner.getPrimaryTable() ).getTableName().getName()
currentBindingContext.getNamingStrategy().tableName( tableName ) : null;
return currentBindingContext.getNamingStrategy().collectionTableName(
owner.getEntity().getName(),
ownerTableLogicalName,
null, // todo : here
null, // todo : and here
pluralAttributeBinding.getContainer().getPathBase() + '.' + attributeSource.getName()
);
}
}
); );
Table collectionTable = schema.locateTable( tableIdentifier );
if ( collectionTable == null ) {
collectionTable = schema.createTable( tableIdentifier );
}
pluralAttributeBinding.setCollectionTable( collectionTable ); pluralAttributeBinding.setCollectionTable( collectionTable );
} }
else { else {
// todo : not sure wel have all the needed info here in all cases, specifically when needing to know the "other side" pluralAttributeBinding.setCollectionTable( createInLineView( (InLineViewSource) tableSpecificationSource ) );
final EntityBinding owner = pluralAttributeBinding.getContainer().seekEntityBinding();
final String ownerTableLogicalName = Table.class.isInstance( owner.getPrimaryTable() )
? Table.class.cast( owner.getPrimaryTable() ).getTableName().getName()
: null;
String collectionTableName = currentBindingContext.getNamingStrategy().collectionTableName(
owner.getEntity().getName(),
ownerTableLogicalName,
null, // todo : here
null, // todo : and here
pluralAttributeBinding.getContainer().getPathBase() + '.' + attributeSource.getName()
);
collectionTableName = quoteIdentifier( collectionTableName );
pluralAttributeBinding.setCollectionTable(
schema.locateOrCreateTable(
Identifier.toIdentifier(
collectionTableName
)
)
);
} }
if ( StringHelper.isNotEmpty( attributeSource.getCollectionTableComment() ) ) { if ( StringHelper.isNotEmpty( attributeSource.getCollectionTableComment() ) ) {
@ -1287,40 +1277,99 @@ public class Binder {
// Relational ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Relational ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private void bindPrimaryTable(EntitySource entitySource, EntityBinding entityBinding) { private void bindPrimaryTable(EntitySource entitySource, final EntityBinding entityBinding) {
final TableSource tableSource = entitySource.getPrimaryTable(); final TableSpecificationSource tableSpecificationSource = entitySource.getPrimaryTable();
final Table table = createTable( entityBinding, tableSource ); if ( TableSource.class.isInstance( tableSpecificationSource ) ) {
entityBinding.setPrimaryTable( table ); final Table table = createTable(
entityBinding.setPrimaryTableName( table.getTableName().getName() ); (TableSource) tableSpecificationSource,
} new InferredNamingStrategy() {
@Override
private void bindSecondaryTables(EntitySource entitySource, EntityBinding entityBinding) { public String inferredTableName() {
for ( TableSource secondaryTableSource : entitySource.getSecondaryTables() ) { return currentBindingContext.getNamingStrategy()
final Table table = createTable( entityBinding, secondaryTableSource ); .classToTableName( entityBinding.getEntity().getClassName() );
entityBinding.addSecondaryTable( secondaryTableSource.getLogicalName(), table ); }
} }
} );
entityBinding.setPrimaryTable( table );
private Table createTable(EntityBinding entityBinding, TableSource tableSource) { // todo : ugh!
String tableName = tableSource.getExplicitTableName(); entityBinding.setPrimaryTableName( table.getTableName().getName() );
if ( StringHelper.isEmpty( tableName ) ) {
tableName = currentBindingContext.getNamingStrategy()
.classToTableName( entityBinding.getEntity().getClassName() );
} }
else { else {
tableName = currentBindingContext.getNamingStrategy().tableName( tableName ); entityBinding.setPrimaryTable( createInLineView( (InLineViewSource) tableSpecificationSource ) );
} }
tableName = quoteIdentifier( tableName ); }
private InLineView createInLineView(InLineViewSource inLineViewSource) {
final Schema.Name databaseSchemaName = Helper.determineDatabaseSchemaName(
inLineViewSource.getExplicitSchemaName(),
inLineViewSource.getExplicitCatalogName(),
currentBindingContext
);
final Identifier logicalName = Identifier.toIdentifier( inLineViewSource.getLogicalName() );
return currentBindingContext.getMetadataImplementor()
.getDatabase()
.locateSchema( databaseSchemaName )
.createInLineView( logicalName, inLineViewSource.getSelectStatement() );
}
private static interface InferredNamingStrategy {
public String inferredTableName();
}
private Table createTable(TableSource tableSource, InferredNamingStrategy namingStrategy) {
String explicitTableNameString = tableSource.getExplicitTableName();
if ( explicitTableNameString == null ) {
explicitTableNameString = namingStrategy.inferredTableName();
}
explicitTableNameString = quoteIdentifier( explicitTableNameString );
final Identifier logicalName = Identifier.toIdentifier( explicitTableNameString );
explicitTableNameString = currentBindingContext.getNamingStrategy().tableName( explicitTableNameString );
explicitTableNameString = quoteIdentifier( explicitTableNameString );
final Identifier physicalName = Identifier.toIdentifier( explicitTableNameString );
final Schema.Name databaseSchemaName = Helper.determineDatabaseSchemaName( final Schema.Name databaseSchemaName = Helper.determineDatabaseSchemaName(
tableSource.getExplicitSchemaName(), tableSource.getExplicitSchemaName(),
tableSource.getExplicitCatalogName(), tableSource.getExplicitCatalogName(),
currentBindingContext currentBindingContext
); );
return currentBindingContext.getMetadataImplementor()
Table table = currentBindingContext.getMetadataImplementor()
.getDatabase() .getDatabase()
.locateSchema( databaseSchemaName ) .locateSchema( databaseSchemaName )
.locateOrCreateTable( Identifier.toIdentifier( tableName ) ); .locateTable( logicalName );
if ( table == null ) {
table = currentBindingContext.getMetadataImplementor()
.getDatabase()
.locateSchema( databaseSchemaName )
.createTable( logicalName, physicalName );
}
return table;
}
private void bindSecondaryTables(EntitySource entitySource, EntityBinding entityBinding) {
for ( SecondaryTableSource secondaryTableSource : entitySource.getSecondaryTables() ) {
final TableSpecificationSource source = secondaryTableSource.getTableSource();
if ( TableSource.class.isInstance( source ) ) {
final Table table = createTable(
(TableSource) source,
new InferredNamingStrategy() {
@Override
public String inferredTableName() {
throw new MappingException(
"Secondary table must specify explicit name",
currentBindingContext.getOrigin()
);
}
}
);
// todo : finish up...
// 1) no need to keep secondary tables on EntityBinding because we should be able to look them
// up from Schema
// 2) process foreign key
entityBinding.addSecondaryTable( table.getLogicalName().getName(), table );
}
}
} }
private void bindTableUniqueConstraints(EntitySource entitySource, EntityBinding entityBinding) { private void bindTableUniqueConstraints(EntitySource entitySource, EntityBinding entityBinding) {

View File

@ -111,6 +111,8 @@ import javax.persistence.Version;
import org.jboss.jandex.DotName; import org.jboss.jandex.DotName;
import org.hibernate.annotations.Subselect;
/** /**
* Defines the dot names for the JPA annotations * Defines the dot names for the JPA annotations
* *
@ -195,6 +197,7 @@ public interface JPADotNames {
DotName SEQUENCE_GENERATOR = DotName.createSimple( SequenceGenerator.class.getName() ); DotName SEQUENCE_GENERATOR = DotName.createSimple( SequenceGenerator.class.getName() );
DotName SQL_RESULT_SET_MAPPING = DotName.createSimple( SqlResultSetMapping.class.getName() ); DotName SQL_RESULT_SET_MAPPING = DotName.createSimple( SqlResultSetMapping.class.getName() );
DotName SQL_RESULT_SET_MAPPINGS = DotName.createSimple( SqlResultSetMappings.class.getName() ); DotName SQL_RESULT_SET_MAPPINGS = DotName.createSimple( SqlResultSetMappings.class.getName() );
DotName SUBSELECT = DotName.createSimple( Subselect.class.getName() );
DotName TABLE = DotName.createSimple( Table.class.getName() ); DotName TABLE = DotName.createSimple( Table.class.getName() );
DotName TABLE_GENERATOR = DotName.createSimple( TableGenerator.class.getName() ); DotName TABLE_GENERATOR = DotName.createSimple( TableGenerator.class.getName() );
DotName TEMPORAL = DotName.createSimple( Temporal.class.getName() ); DotName TEMPORAL = DotName.createSimple( Temporal.class.getName() );

View File

@ -23,15 +23,6 @@
*/ */
package org.hibernate.metamodel.internal.source.annotations.entity; package org.hibernate.metamodel.internal.source.annotations.entity;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.AccessType; import javax.persistence.AccessType;
import javax.persistence.DiscriminatorType; import javax.persistence.DiscriminatorType;
import javax.persistence.PersistenceException; import javax.persistence.PersistenceException;
@ -42,6 +33,15 @@ import javax.persistence.PostUpdate;
import javax.persistence.PrePersist; import javax.persistence.PrePersist;
import javax.persistence.PreRemove; import javax.persistence.PreRemove;
import javax.persistence.PreUpdate; import javax.persistence.PreUpdate;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue; import org.jboss.jandex.AnnotationValue;
@ -58,10 +58,6 @@ import org.hibernate.annotations.OptimisticLockType;
import org.hibernate.annotations.PolymorphismType; import org.hibernate.annotations.PolymorphismType;
import org.hibernate.engine.OptimisticLockStyle; import org.hibernate.engine.OptimisticLockStyle;
import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle; import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.spi.binding.Caching;
import org.hibernate.metamodel.spi.binding.CustomSQL;
import org.hibernate.metamodel.spi.binding.InheritanceType;
import org.hibernate.metamodel.internal.source.annotations.AnnotationBindingContext; import org.hibernate.metamodel.internal.source.annotations.AnnotationBindingContext;
import org.hibernate.metamodel.internal.source.annotations.HibernateDotNames; import org.hibernate.metamodel.internal.source.annotations.HibernateDotNames;
import org.hibernate.metamodel.internal.source.annotations.JPADotNames; import org.hibernate.metamodel.internal.source.annotations.JPADotNames;
@ -69,9 +65,14 @@ import org.hibernate.metamodel.internal.source.annotations.JandexHelper;
import org.hibernate.metamodel.internal.source.annotations.attribute.Column; import org.hibernate.metamodel.internal.source.annotations.attribute.Column;
import org.hibernate.metamodel.internal.source.annotations.attribute.FormulaValue; import org.hibernate.metamodel.internal.source.annotations.attribute.FormulaValue;
import org.hibernate.metamodel.internal.source.annotations.xml.PseudoJpaDotNames; import org.hibernate.metamodel.internal.source.annotations.xml.PseudoJpaDotNames;
import org.hibernate.metamodel.spi.binding.Caching;
import org.hibernate.metamodel.spi.binding.CustomSQL;
import org.hibernate.metamodel.spi.binding.InheritanceType;
import org.hibernate.metamodel.spi.source.ConstraintSource; import org.hibernate.metamodel.spi.source.ConstraintSource;
import org.hibernate.metamodel.spi.source.JpaCallbackSource; import org.hibernate.metamodel.spi.source.JpaCallbackSource;
import org.hibernate.metamodel.spi.source.TableSource; import org.hibernate.metamodel.spi.source.PrimaryKeyJoinColumnSource;
import org.hibernate.metamodel.spi.source.SecondaryTableSource;
import org.hibernate.metamodel.spi.source.TableSpecificationSource;
/** /**
* Represents an entity or mapped superclass configured via annotations/orm-xml. * Represents an entity or mapped superclass configured via annotations/orm-xml.
@ -87,8 +88,8 @@ public class EntityClass extends ConfiguredClass {
private final List<String> synchronizedTableNames; private final List<String> synchronizedTableNames;
private final int batchSize; private final int batchSize;
private final TableSource primaryTableSource; private final TableSpecificationSource primaryTableSource;
private final Set<TableSource> secondaryTableSources; private final Set<SecondaryTableSource> secondaryTableSources;
private final Set<ConstraintSource> constraintSources; private final Set<ConstraintSource> constraintSources;
private boolean isMutable; private boolean isMutable;
@ -129,20 +130,17 @@ public class EntityClass extends ConfiguredClass {
this.idType = determineIdType(); this.idType = determineIdType();
boolean hasOwnTable = definesItsOwnTable(); boolean hasOwnTable = definesItsOwnTable();
this.explicitEntityName = determineExplicitEntityName(); this.explicitEntityName = determineExplicitEntityName();
this.constraintSources = new HashSet<ConstraintSource>(); this.constraintSources = new HashSet<ConstraintSource>();
if ( hasOwnTable ) { if ( hasOwnTable ) {
AnnotationInstance tableAnnotation = JandexHelper.getSingleAnnotation( this.primaryTableSource = createPrimaryTableSource();
getClassInfo(),
JPADotNames.TABLE
);
this.primaryTableSource = createTableSource( tableAnnotation );
} }
else { else {
this.primaryTableSource = null; this.primaryTableSource = null;
} }
this.secondaryTableSources = createSecondaryTableSources(); this.secondaryTableSources = createSecondaryTableSources();
this.customLoaderQueryName = determineCustomLoader(); this.customLoaderQueryName = determineCustomLoader();
this.synchronizedTableNames = determineSynchronizedTableNames(); this.synchronizedTableNames = determineSynchronizedTableNames();
this.batchSize = determineBatchSize(); this.batchSize = determineBatchSize();
@ -194,7 +192,8 @@ public class EntityClass extends ConfiguredClass {
return caching; return caching;
} }
public TableSource getPrimaryTableSource() { public TableSpecificationSource getPrimaryTableSource() {
// todo : this is different from hbm which returns null if "!definesItsOwnTable()"
if ( definesItsOwnTable() ) { if ( definesItsOwnTable() ) {
return primaryTableSource; return primaryTableSource;
} }
@ -203,7 +202,7 @@ public class EntityClass extends ConfiguredClass {
} }
} }
public Set<TableSource> getSecondaryTableSources() { public Set<SecondaryTableSource> getSecondaryTableSources() {
return secondaryTableSources; return secondaryTableSources;
} }
@ -364,7 +363,7 @@ public class EntityClass extends ConfiguredClass {
Class<?> type = String.class; // string is the discriminator default Class<?> type = String.class; // string is the discriminator default
if ( discriminatorFormulaAnnotation != null ) { if ( discriminatorFormulaAnnotation != null ) {
String expression = JandexHelper.getValue( discriminatorFormulaAnnotation, "value", String.class ); String expression = JandexHelper.getValue( discriminatorFormulaAnnotation, "value", String.class );
discriminatorFormula = new FormulaValue( getPrimaryTableSource().getExplicitTableName(), expression ); discriminatorFormula = new FormulaValue( null, expression );
} }
discriminatorColumnValues = new Column( null ); //(stliu) give null here, will populate values below discriminatorColumnValues = new Column( null ); //(stliu) give null here, will populate values below
discriminatorColumnValues.setNullable( false ); // discriminator column cannot be null discriminatorColumnValues.setNullable( false ); // discriminator column cannot be null
@ -574,78 +573,60 @@ public class EntityClass extends ConfiguredClass {
); );
} }
/** private TableSpecificationSource createPrimaryTableSource() {
* todo see {@code Binder#createTable} AnnotationInstance tableAnnotation = JandexHelper.getSingleAnnotation(
* getClassInfo(),
* @param tableAnnotation a annotation instance, either {@link javax.persistence.Table} or {@link javax.persistence.SecondaryTable} JPADotNames.TABLE
* );
* @return A table source for the specified annotation instance AnnotationInstance subselectAnnotation = JandexHelper.getSingleAnnotation(
*/ getClassInfo(),
private TableSource createTableSource(AnnotationInstance tableAnnotation) { JPADotNames.SUBSELECT
String schema = null; );
String catalog = null;
if ( tableAnnotation != null ) {
schema = JandexHelper.getValue( tableAnnotation, "schema", String.class );
catalog = JandexHelper.getValue( tableAnnotation, "catalog", String.class );
}
// process the table name
String tableName = null;
String logicalTableName = null;
if ( tableAnnotation != null ) { if ( tableAnnotation != null ) {
logicalTableName = JandexHelper.getValue( tableAnnotation, "name", String.class ); return createPrimaryTableSourceAsTable( tableAnnotation );
if ( StringHelper.isNotEmpty( logicalTableName ) ) {
tableName = logicalTableName;
}
createUniqueConstraints( tableAnnotation, tableName );
} }
else if ( subselectAnnotation != null ) {
TableSourceImpl tableSourceImpl; return createPrimaryTableSourceAsInLineView( subselectAnnotation );
if ( tableAnnotation == null || JPADotNames.TABLE.equals( tableAnnotation.name() ) ) {
// for the main table @Table we use 'null' as logical name
tableSourceImpl = new TableSourceImpl( schema, catalog, tableName, null );
} }
else { else {
// for secondary tables a name must be specified which is used as logical table name return new TableSourceImpl( null, null, null );
tableSourceImpl = new TableSourceImpl( schema, catalog, tableName, logicalTableName );
} }
return tableSourceImpl;
} }
private Set<TableSource> createSecondaryTableSources() { private TableSpecificationSource createPrimaryTableSourceAsTable(AnnotationInstance tableAnnotation) {
Set<TableSource> secondaryTableSources = new HashSet<TableSource>(); final String schemaName = determineSchemaName( tableAnnotation );
AnnotationInstance secondaryTables = JandexHelper.getSingleAnnotation( final String catalogName = determineCatalogName( tableAnnotation );
getClassInfo(),
JPADotNames.SECONDARY_TABLES final String explicitTableName = tableAnnotation == null
); ? null
AnnotationInstance secondaryTable = JandexHelper.getSingleAnnotation( : JandexHelper.getValue( tableAnnotation, "name", String.class );
getClassInfo(),
JPADotNames.SECONDARY_TABLE if ( tableAnnotation != null ) {
); createUniqueConstraints( tableAnnotation, null );
// collect all @secondaryTable annotations
List<AnnotationInstance> secondaryTableAnnotations = new ArrayList<AnnotationInstance>();
if ( secondaryTable != null ) {
secondaryTableAnnotations.add(
secondaryTable
);
} }
if ( secondaryTables != null ) { return new TableSourceImpl( schemaName, catalogName, explicitTableName );
secondaryTableAnnotations.addAll(
Arrays.asList(
JandexHelper.getValue( secondaryTables, "value", AnnotationInstance[].class )
)
);
}
// create table sources
for ( AnnotationInstance annotationInstance : secondaryTableAnnotations ) {
secondaryTableSources.add( createTableSource( annotationInstance ) );
}
return secondaryTableSources;
} }
private TableSpecificationSource createPrimaryTableSourceAsInLineView(AnnotationInstance subselectAnnotation) {
return new InLineViewSourceImpl(
JandexHelper.getValue( subselectAnnotation, "value", String.class ),
getEntityName()
);
}
private String determineSchemaName(AnnotationInstance tableAnnotation) {
return tableAnnotation == null
? null
: JandexHelper.getValue( tableAnnotation, "schema", String.class );
}
private String determineCatalogName(AnnotationInstance tableAnnotation) {
return tableAnnotation == null
? null
: JandexHelper.getValue( tableAnnotation, "catalog", String.class );
}
private void createUniqueConstraints(AnnotationInstance tableAnnotation, String tableName) { private void createUniqueConstraints(AnnotationInstance tableAnnotation, String tableName) {
AnnotationValue value = tableAnnotation.value( "uniqueConstraints" ); AnnotationValue value = tableAnnotation.value( "uniqueConstraints" );
@ -665,6 +646,101 @@ public class EntityClass extends ConfiguredClass {
} }
} }
private Set<SecondaryTableSource> createSecondaryTableSources() {
final Set<SecondaryTableSource> secondaryTableSources = new HashSet<SecondaryTableSource>();
// process a singular @SecondaryTable annotation
{
AnnotationInstance secondaryTable = JandexHelper.getSingleAnnotation(
getClassInfo(),
JPADotNames.SECONDARY_TABLE
);
if ( secondaryTable != null ) {
secondaryTableSources.add( createSecondaryTableSource( secondaryTable ) );
}
}
// process any @SecondaryTables grouping
{
AnnotationInstance secondaryTables = JandexHelper.getSingleAnnotation(
getClassInfo(),
JPADotNames.SECONDARY_TABLES
);
if ( secondaryTables != null ) {
for ( AnnotationInstance secondaryTable : JandexHelper.getValue( secondaryTables, "value", AnnotationInstance[].class ) ) {
secondaryTableSources.add( createSecondaryTableSource( secondaryTable ) );
}
}
}
return secondaryTableSources;
}
private SecondaryTableSource createSecondaryTableSource(AnnotationInstance tableAnnotation) {
final String schemaName = determineSchemaName( tableAnnotation );
final String catalogName = determineCatalogName( tableAnnotation );
final String tableName = JandexHelper.getValue( tableAnnotation, "name", String.class );
createUniqueConstraints( tableAnnotation, tableName );
final List<PrimaryKeyJoinColumnSource> keys = collectionSecondaryTableKeys( tableAnnotation );
return new SecondaryTableSourceImpl( new TableSourceImpl( schemaName, catalogName, tableName ), keys );
}
private List<PrimaryKeyJoinColumnSource> collectionSecondaryTableKeys(final AnnotationInstance tableAnnotation) {
final AnnotationInstance[] joinColumnAnnotations = JandexHelper.getValue(
tableAnnotation,
"pkJoinColumns",
AnnotationInstance[].class
);
if ( joinColumnAnnotations == null ) {
return Collections.emptyList();
}
final List<PrimaryKeyJoinColumnSource> keys = new ArrayList<PrimaryKeyJoinColumnSource>();
for ( final AnnotationInstance joinColumnAnnotation : joinColumnAnnotations ) {
keys.add( new PrimaryKeyJoinColumnSourceImpl( joinColumnAnnotation ) );
}
return keys;
}
private static class PrimaryKeyJoinColumnSourceImpl implements PrimaryKeyJoinColumnSource {
private final String columnName;
private final String referencedColumnName;
private final String columnDefinition;
private PrimaryKeyJoinColumnSourceImpl(AnnotationInstance joinColumnAnnotation) {
this(
JandexHelper.getValue( joinColumnAnnotation, "name", String.class ),
JandexHelper.getValue( joinColumnAnnotation, "referencedColumnName", String.class ),
JandexHelper.getValue( joinColumnAnnotation, "columnDefinition", String.class )
);
}
private PrimaryKeyJoinColumnSourceImpl(
String columnName,
String referencedColumnName,
String columnDefinition) {
this.columnName = columnName;
this.referencedColumnName = referencedColumnName;
this.columnDefinition = columnDefinition;
}
@Override
public String getColumnName() {
return columnName;
}
@Override
public String getReferencedColumnName() {
return referencedColumnName;
}
@Override
public String getColumnDefinition() {
return columnDefinition;
}
}
private String determineCustomLoader() { private String determineCustomLoader() {
String customLoader = null; String customLoader = null;
// Custom sql loader // Custom sql loader

View File

@ -41,8 +41,9 @@ import org.hibernate.metamodel.internal.source.annotations.attribute.ToOneAttrib
import org.hibernate.metamodel.spi.source.AttributeSource; import org.hibernate.metamodel.spi.source.AttributeSource;
import org.hibernate.metamodel.spi.source.JpaCallbackSource; import org.hibernate.metamodel.spi.source.JpaCallbackSource;
import org.hibernate.metamodel.spi.source.MetaAttributeSource; import org.hibernate.metamodel.spi.source.MetaAttributeSource;
import org.hibernate.metamodel.spi.source.SecondaryTableSource;
import org.hibernate.metamodel.spi.source.SubclassEntitySource; import org.hibernate.metamodel.spi.source.SubclassEntitySource;
import org.hibernate.metamodel.spi.source.TableSource; import org.hibernate.metamodel.spi.source.TableSpecificationSource;
/** /**
* @author Hardy Ferentschik * @author Hardy Ferentschik
@ -86,7 +87,7 @@ public class EntitySourceImpl implements EntitySource {
} }
@Override @Override
public TableSource getPrimaryTable() { public TableSpecificationSource getPrimaryTable() {
return entityClass.getPrimaryTableSource(); return entityClass.getPrimaryTableSource();
} }
@ -217,7 +218,7 @@ public class EntitySourceImpl implements EntitySource {
} }
@Override @Override
public Iterable<TableSource> getSecondaryTables() { public Set<SecondaryTableSource> getSecondaryTables() {
return entityClass.getSecondaryTableSources(); return entityClass.getSecondaryTableSources();
} }
} }

View File

@ -0,0 +1,59 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, 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.internal.source.annotations.entity;
import org.hibernate.metamodel.spi.source.InLineViewSource;
/**
* @author Steve Ebersole
*/
public class InLineViewSourceImpl implements InLineViewSource {
private final String selectStatement;
private final String logicalName;
public InLineViewSourceImpl(String selectStatement, String logicalName) {
this.selectStatement = selectStatement;
this.logicalName = logicalName;
}
@Override
public String getSelectStatement() {
return selectStatement;
}
@Override
public String getExplicitSchemaName() {
return null;
}
@Override
public String getExplicitCatalogName() {
return null;
}
@Override
public String getLogicalName() {
return logicalName;
}
}

View File

@ -0,0 +1,62 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, 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.internal.source.annotations.entity;
import java.util.Collections;
import java.util.List;
import org.hibernate.metamodel.spi.source.PrimaryKeyJoinColumnSource;
import org.hibernate.metamodel.spi.source.SecondaryTableSource;
import org.hibernate.metamodel.spi.source.TableSpecificationSource;
/**
* @author Steve Ebersole
*/
public class SecondaryTableSourceImpl implements SecondaryTableSource {
private final TableSpecificationSource joinTable;
private final List<PrimaryKeyJoinColumnSource> joinColumns;
public SecondaryTableSourceImpl(
TableSpecificationSource joinTable,
List<PrimaryKeyJoinColumnSource> joinColumns) {
this.joinTable = joinTable;
this.joinColumns = Collections.unmodifiableList( joinColumns );
}
@Override
public List<PrimaryKeyJoinColumnSource> getJoinColumns() {
return joinColumns;
}
@Override
public TableSpecificationSource getTableSource() {
return joinTable;
}
@Override
public String getForeignKeyName() {
// not supported from annotations, unless docs for @ForeignKey are wrong...
return null;
}
}

View File

@ -29,13 +29,12 @@ class TableSourceImpl implements TableSource {
private final String schema; private final String schema;
private final String catalog; private final String catalog;
private final String tableName; private final String tableName;
private final String logicalName;
TableSourceImpl(String schema, String catalog, String tableName, String logicalName) { TableSourceImpl(String schema, String catalog, String tableName) {
this.schema = schema; this.schema = schema;
this.catalog = catalog; this.catalog = catalog;
this.tableName = tableName; // for some reason annotations passing in "" sometimes :(
this.logicalName = logicalName; this.tableName = tableName.equals( "" ) ? null : tableName;
} }
@Override @Override
@ -53,11 +52,6 @@ class TableSourceImpl implements TableSource {
return tableName; return tableName;
} }
@Override
public String getLogicalName() {
return logicalName;
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if ( this == o ) { if ( this == o ) {
@ -72,9 +66,6 @@ class TableSourceImpl implements TableSource {
if ( catalog != null ? !catalog.equals( that.catalog ) : that.catalog != null ) { if ( catalog != null ? !catalog.equals( that.catalog ) : that.catalog != null ) {
return false; return false;
} }
if ( logicalName != null ? !logicalName.equals( that.logicalName ) : that.logicalName != null ) {
return false;
}
if ( schema != null ? !schema.equals( that.schema ) : that.schema != null ) { if ( schema != null ? !schema.equals( that.schema ) : that.schema != null ) {
return false; return false;
} }
@ -90,7 +81,6 @@ class TableSourceImpl implements TableSource {
int result = schema != null ? schema.hashCode() : 0; int result = schema != null ? schema.hashCode() : 0;
result = 31 * result + ( catalog != null ? catalog.hashCode() : 0 ); result = 31 * result + ( catalog != null ? catalog.hashCode() : 0 );
result = 31 * result + ( tableName != null ? tableName.hashCode() : 0 ); result = 31 * result + ( tableName != null ? tableName.hashCode() : 0 );
result = 31 * result + ( logicalName != null ? logicalName.hashCode() : 0 );
return result; return result;
} }
@ -101,7 +91,6 @@ class TableSourceImpl implements TableSource {
sb.append( "{schema='" ).append( schema ).append( '\'' ); sb.append( "{schema='" ).append( schema ).append( '\'' );
sb.append( ", catalog='" ).append( catalog ).append( '\'' ); sb.append( ", catalog='" ).append( catalog ).append( '\'' );
sb.append( ", tableName='" ).append( tableName ).append( '\'' ); sb.append( ", tableName='" ).append( tableName ).append( '\'' );
sb.append( ", logicalName='" ).append( logicalName ).append( '\'' );
sb.append( '}' ); sb.append( '}' );
return sb.toString(); return sb.toString();
} }

View File

@ -25,7 +25,9 @@ package org.hibernate.metamodel.internal.source.hbm;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import org.hibernate.AssertionFailure; import org.hibernate.AssertionFailure;
import org.hibernate.EntityMode; import org.hibernate.EntityMode;
@ -35,6 +37,7 @@ import org.hibernate.internal.jaxb.mapping.hbm.JaxbAnyElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbBagElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbBagElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbComponentElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbComponentElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbIdbagElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbIdbagElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbJoinElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbListElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbListElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbManyToOneElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbManyToOneElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbMapElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbMapElement;
@ -43,6 +46,7 @@ import org.hibernate.internal.jaxb.mapping.hbm.JaxbPropertyElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbSetElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbSetElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbSynchronizeElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbSynchronizeElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbTuplizerElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbTuplizerElement;
import org.hibernate.internal.jaxb.mapping.hbm.JoinElementSource;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.spi.binding.CustomSQL; import org.hibernate.metamodel.spi.binding.CustomSQL;
import org.hibernate.metamodel.spi.source.AttributeSource; import org.hibernate.metamodel.spi.source.AttributeSource;
@ -51,9 +55,9 @@ import org.hibernate.metamodel.spi.source.EntitySource;
import org.hibernate.metamodel.spi.source.JpaCallbackSource; import org.hibernate.metamodel.spi.source.JpaCallbackSource;
import org.hibernate.metamodel.spi.source.LocalBindingContext; import org.hibernate.metamodel.spi.source.LocalBindingContext;
import org.hibernate.metamodel.spi.source.MetaAttributeSource; import org.hibernate.metamodel.spi.source.MetaAttributeSource;
import org.hibernate.metamodel.spi.source.SecondaryTableSource;
import org.hibernate.metamodel.spi.source.SingularAttributeSource; import org.hibernate.metamodel.spi.source.SingularAttributeSource;
import org.hibernate.metamodel.spi.source.SubclassEntitySource; import org.hibernate.metamodel.spi.source.SubclassEntitySource;
import org.hibernate.metamodel.spi.source.TableSource;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
@ -62,12 +66,27 @@ import org.hibernate.metamodel.spi.source.TableSource;
public abstract class AbstractEntitySourceImpl implements EntitySource { public abstract class AbstractEntitySourceImpl implements EntitySource {
private final MappingDocument sourceMappingDocument; private final MappingDocument sourceMappingDocument;
private final EntityElement entityElement; private final EntityElement entityElement;
private final Set<SecondaryTableSource> secondaryTableSources;
private List<SubclassEntitySource> subclassEntitySources = new ArrayList<SubclassEntitySource>(); private List<SubclassEntitySource> subclassEntitySources = new ArrayList<SubclassEntitySource>();
protected AbstractEntitySourceImpl(MappingDocument sourceMappingDocument, EntityElement entityElement) { protected AbstractEntitySourceImpl(MappingDocument sourceMappingDocument, EntityElement entityElement) {
this.sourceMappingDocument = sourceMappingDocument; this.sourceMappingDocument = sourceMappingDocument;
this.entityElement = entityElement; this.entityElement = entityElement;
secondaryTableSources = extractSecondaryTables( entityElement, sourceMappingDocument.getMappingLocalBindingContext() );
}
private static Set<SecondaryTableSource> extractSecondaryTables(EntityElement entityElement, HbmBindingContext bindingContext) {
if ( ! JoinElementSource.class.isInstance( entityElement ) ) {
return Collections.emptySet();
}
final Set<SecondaryTableSource> secondaryTableSources = new HashSet<SecondaryTableSource>();
for ( JaxbJoinElement joinElement : ( (JoinElementSource) entityElement ).getJoin() ) {
secondaryTableSources.add( new SecondaryTableSourceImpl( joinElement, bindingContext ) );
}
return secondaryTableSources;
} }
protected EntityElement entityElement() { protected EntityElement entityElement() {
@ -320,11 +339,12 @@ public abstract class AbstractEntitySourceImpl implements EntitySource {
} }
@Override @Override
public Iterable<TableSource> getSecondaryTables() { public Set<SecondaryTableSource> getSecondaryTables() {
return Collections.emptySet(); return secondaryTableSources;
} }
@Override @Override
@SuppressWarnings( {"unchecked"})
public List<JpaCallbackSource> getJpaCallbackClasses() { public List<JpaCallbackSource> getJpaCallbackClasses() {
return Collections.EMPTY_LIST; return Collections.EMPTY_LIST;
} }

View File

@ -44,6 +44,7 @@ import org.hibernate.metamodel.spi.source.MetaAttributeSource;
import org.hibernate.metamodel.spi.source.PluralAttributeElementSource; import org.hibernate.metamodel.spi.source.PluralAttributeElementSource;
import org.hibernate.metamodel.spi.source.PluralAttributeKeySource; import org.hibernate.metamodel.spi.source.PluralAttributeKeySource;
import org.hibernate.metamodel.spi.source.PluralAttributeSource; import org.hibernate.metamodel.spi.source.PluralAttributeSource;
import org.hibernate.metamodel.spi.source.TableSpecificationSource;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
@ -139,6 +140,14 @@ public abstract class AbstractPluralAttributeSourceImpl
return elementSource; return elementSource;
} }
@Override
public TableSpecificationSource getCollectionTableSpecificationSource() {
return Helper.createTableSource(
pluralAttributeElement,
container().getPath() + "." + pluralAttributeElement.getName()
);
}
@Override @Override
public String getExplicitSchemaName() { public String getExplicitSchemaName() {
return pluralAttributeElement.getSchema(); return pluralAttributeElement.getSchema();

View File

@ -28,6 +28,7 @@ import java.util.List;
import org.hibernate.internal.jaxb.mapping.hbm.EntityElement; import org.hibernate.internal.jaxb.mapping.hbm.EntityElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbFetchProfileElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbFetchProfileElement;
import org.hibernate.metamodel.spi.source.LocalBindingContext; import org.hibernate.metamodel.spi.source.LocalBindingContext;
import org.hibernate.metamodel.spi.source.MappingException;
import org.hibernate.metamodel.spi.source.MetaAttributeContext; import org.hibernate.metamodel.spi.source.MetaAttributeContext;
/** /**
@ -43,4 +44,7 @@ public interface HbmBindingContext extends LocalBindingContext {
public String determineEntityName(EntityElement entityElement); public String determineEntityName(EntityElement entityElement);
public void processFetchProfiles(List<JaxbFetchProfileElement> fetchProfiles, String containingEntityName); public void processFetchProfiles(List<JaxbFetchProfileElement> fetchProfiles, String containingEntityName);
public MappingException makeMappingException(String message);
public MappingException makeMappingException(String message, Exception cause);
} }

View File

@ -41,6 +41,7 @@ import org.hibernate.internal.jaxb.mapping.hbm.JaxbMetaElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbParamElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbParamElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbSubclassElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbSubclassElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbUnionSubclassElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbUnionSubclassElement;
import org.hibernate.internal.jaxb.mapping.hbm.TableInformationSource;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.metamodel.spi.binding.CustomSQL; import org.hibernate.metamodel.spi.binding.CustomSQL;
import org.hibernate.metamodel.spi.binding.InheritanceType; import org.hibernate.metamodel.spi.binding.InheritanceType;
@ -53,6 +54,7 @@ import org.hibernate.metamodel.spi.source.MappingException;
import org.hibernate.metamodel.spi.source.MetaAttributeContext; import org.hibernate.metamodel.spi.source.MetaAttributeContext;
import org.hibernate.metamodel.spi.source.MetaAttributeSource; import org.hibernate.metamodel.spi.source.MetaAttributeSource;
import org.hibernate.metamodel.spi.source.RelationalValueSource; import org.hibernate.metamodel.spi.source.RelationalValueSource;
import org.hibernate.metamodel.spi.source.TableSpecificationSource;
/** /**
* A helper for dealing with * A helper for dealing with
@ -287,6 +289,27 @@ public class Helper {
return null; return null;
} }
public static TableSpecificationSource createTableSource(TableInformationSource jaxbTableSource, String context) {
if ( jaxbTableSource.getSubselectAttribute() == null && jaxbTableSource.getSubselect() == null ) {
return new TableSourceImpl(
jaxbTableSource.getSchema(),
jaxbTableSource.getCatalog(),
jaxbTableSource.getTable()
);
}
else {
return new InLineViewSourceImpl(
jaxbTableSource.getSchema(),
jaxbTableSource.getCatalog(),
jaxbTableSource.getSubselectAttribute() != null
? jaxbTableSource.getSubselectAttribute()
: jaxbTableSource.getSubselect(),
context
);
}
}
/** /**
* For things that define one or more "value sources" there is a lot of variance in terms of how they * For things that define one or more "value sources" there is a lot of variance in terms of how they
* look in the XML. As an example, consider {@code <property/>} which might have:<ul> * look in the XML. As an example, consider {@code <property/>} which might have:<ul>

View File

@ -0,0 +1,66 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, 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.internal.source.hbm;
import org.hibernate.metamodel.spi.source.InLineViewSource;
/**
* @author Steve Ebersole
*/
public class InLineViewSourceImpl implements InLineViewSource {
private final String schemaName;
private final String catalogName;
private final String selectStatement;
private final String logicalName;
public InLineViewSourceImpl(
String schemaName,
String catalogName,
String selectStatement, String logicalName) {
this.schemaName = schemaName;
this.catalogName = catalogName;
this.selectStatement = selectStatement;
this.logicalName = logicalName;
}
@Override
public String getExplicitSchemaName() {
return schemaName;
}
@Override
public String getExplicitCatalogName() {
return catalogName;
}
@Override
public String getSelectStatement() {
return selectStatement;
}
@Override
public String getLogicalName() {
return logicalName;
}
}

View File

@ -35,6 +35,7 @@ import org.hibernate.internal.util.Value;
import org.hibernate.metamodel.internal.source.OverriddenMappingDefaults; import org.hibernate.metamodel.internal.source.OverriddenMappingDefaults;
import org.hibernate.metamodel.spi.domain.Type; import org.hibernate.metamodel.spi.domain.Type;
import org.hibernate.metamodel.spi.source.MappingDefaults; import org.hibernate.metamodel.spi.source.MappingDefaults;
import org.hibernate.metamodel.spi.source.MappingException;
import org.hibernate.metamodel.spi.source.MetaAttributeContext; import org.hibernate.metamodel.spi.source.MetaAttributeContext;
import org.hibernate.metamodel.spi.source.MetadataImplementor; import org.hibernate.metamodel.spi.source.MetadataImplementor;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
@ -169,5 +170,15 @@ public class MappingDocument {
public void processFetchProfiles(List<JaxbFetchProfileElement> fetchProfiles, String containingEntityName) { public void processFetchProfiles(List<JaxbFetchProfileElement> fetchProfiles, String containingEntityName) {
// todo : this really needs to not be part of the context // todo : this really needs to not be part of the context
} }
@Override
public MappingException makeMappingException(String message) {
return new MappingException( message, getOrigin() );
}
@Override
public MappingException makeMappingException(String message, Exception cause) {
return new MappingException( message, cause, getOrigin() );
}
} }
} }

View File

@ -37,22 +37,30 @@ import org.hibernate.internal.util.Value;
import org.hibernate.metamodel.spi.binding.Caching; import org.hibernate.metamodel.spi.binding.Caching;
import org.hibernate.metamodel.spi.binding.IdGenerator; import org.hibernate.metamodel.spi.binding.IdGenerator;
import org.hibernate.metamodel.spi.source.AttributeSource; 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.DiscriminatorSource;
import org.hibernate.metamodel.spi.source.IdentifierSource; import org.hibernate.metamodel.spi.source.IdentifierSource;
import org.hibernate.metamodel.spi.source.MappingException;
import org.hibernate.metamodel.spi.source.RelationalValueSource; import org.hibernate.metamodel.spi.source.RelationalValueSource;
import org.hibernate.metamodel.spi.source.RootEntitySource; import org.hibernate.metamodel.spi.source.RootEntitySource;
import org.hibernate.metamodel.spi.source.SimpleIdentifierSource; import org.hibernate.metamodel.spi.source.SimpleIdentifierSource;
import org.hibernate.metamodel.spi.source.SingularAttributeSource; import org.hibernate.metamodel.spi.source.SingularAttributeSource;
import org.hibernate.metamodel.spi.source.TableSource; import org.hibernate.metamodel.spi.source.TableSpecificationSource;
import org.hibernate.metamodel.spi.source.VersionAttributeSource; import org.hibernate.metamodel.spi.source.VersionAttributeSource;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class RootEntitySourceImpl extends AbstractEntitySourceImpl implements RootEntitySource { public class RootEntitySourceImpl extends AbstractEntitySourceImpl implements RootEntitySource {
protected RootEntitySourceImpl(MappingDocument sourceMappingDocument, JaxbHibernateMapping.JaxbClass entityElement) { private final TableSpecificationSource primaryTable;
protected RootEntitySourceImpl(
MappingDocument sourceMappingDocument,
JaxbHibernateMapping.JaxbClass entityElement) {
super( sourceMappingDocument, entityElement ); super( sourceMappingDocument, entityElement );
this.primaryTable = Helper.createTableSource(
entityElement,
sourceMappingDocument.getMappingLocalBindingContext().determineEntityName( entityElement )
);
} }
@Override @Override
@ -196,29 +204,8 @@ public class RootEntitySourceImpl extends AbstractEntitySourceImpl implements Ro
} }
@Override @Override
public TableSource getPrimaryTable() { public TableSpecificationSource getPrimaryTable() {
return new TableSource() { return primaryTable;
@Override
public String getExplicitSchemaName() {
return entityElement().getSchema();
}
@Override
public String getExplicitCatalogName() {
return entityElement().getCatalog();
}
@Override
public String getExplicitTableName() {
return entityElement().getTable();
}
@Override
public String getLogicalName() {
// logical name for the primary table is null
return null;
}
};
} }
@Override @Override

View File

@ -0,0 +1,111 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, 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.internal.source.hbm;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbColumnElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbJoinElement;
import org.hibernate.metamodel.spi.source.PrimaryKeyJoinColumnSource;
import org.hibernate.metamodel.spi.source.SecondaryTableSource;
import org.hibernate.metamodel.spi.source.TableSpecificationSource;
/**
* @author Steve Ebersole
*/
public class SecondaryTableSourceImpl implements SecondaryTableSource {
private final JaxbJoinElement joinElement;
private final TableSpecificationSource joinTable;
private final List<PrimaryKeyJoinColumnSource> joinColumns;
public SecondaryTableSourceImpl(JaxbJoinElement joinElement, HbmBindingContext bindingContext) {
this.joinElement = joinElement;
this.joinTable = Helper.createTableSource(
joinElement,
// todo : need to implement this
null
);
joinColumns = new ArrayList<PrimaryKeyJoinColumnSource>();
if ( joinElement.getKey().getColumnAttribute() != null ) {
if ( joinElement.getKey().getColumn().size() > 0 ) {
throw bindingContext.makeMappingException( "<join/> defined both column attribute and nested <column/>" );
}
joinColumns.add(
new PrimaryKeyJoinColumnSource() {
@Override
public String getColumnName() {
return SecondaryTableSourceImpl.this.joinElement.getKey().getColumnAttribute();
}
@Override
public String getReferencedColumnName() {
return null;
}
@Override
public String getColumnDefinition() {
return null;
}
}
);
}
for ( final JaxbColumnElement columnElement : joinElement.getKey().getColumn() ) {
joinColumns.add(
new PrimaryKeyJoinColumnSource() {
@Override
public String getColumnName() {
return columnElement.getName();
}
@Override
public String getReferencedColumnName() {
return null;
}
@Override
public String getColumnDefinition() {
return columnElement.getSqlType();
}
}
);
}
}
@Override
public TableSpecificationSource getTableSource() {
return joinTable;
}
@Override
public String getForeignKeyName() {
return joinElement.getKey().getForeignKey();
}
@Override
public List<PrimaryKeyJoinColumnSource> getJoinColumns() {
return joinColumns;
}
}

View File

@ -24,78 +24,36 @@
package org.hibernate.metamodel.internal.source.hbm; package org.hibernate.metamodel.internal.source.hbm;
import org.hibernate.internal.jaxb.mapping.hbm.EntityElement; import org.hibernate.internal.jaxb.mapping.hbm.EntityElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbJoinedSubclassElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbSubclassElement; import org.hibernate.internal.jaxb.mapping.hbm.JaxbSubclassElement;
import org.hibernate.internal.jaxb.mapping.hbm.JaxbUnionSubclassElement; import org.hibernate.internal.jaxb.mapping.hbm.TableInformationSource;
import org.hibernate.metamodel.spi.source.EntitySource; import org.hibernate.metamodel.spi.source.EntitySource;
import org.hibernate.metamodel.spi.source.SubclassEntitySource; import org.hibernate.metamodel.spi.source.SubclassEntitySource;
import org.hibernate.metamodel.spi.source.TableSource; import org.hibernate.metamodel.spi.source.TableSpecificationSource;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class SubclassEntitySourceImpl extends AbstractEntitySourceImpl implements SubclassEntitySource { public class SubclassEntitySourceImpl extends AbstractEntitySourceImpl implements SubclassEntitySource {
private final EntitySource container; private final EntitySource container;
private final TableSpecificationSource primaryTable;
protected SubclassEntitySourceImpl( MappingDocument sourceMappingDocument, protected SubclassEntitySourceImpl(
EntityElement entityElement, MappingDocument sourceMappingDocument,
EntitySource container ) { EntityElement entityElement,
EntitySource container) {
super( sourceMappingDocument, entityElement ); super( sourceMappingDocument, entityElement );
this.container = container; this.container = container;
this.primaryTable = TableInformationSource.class.isInstance( entityElement )
? Helper.createTableSource(
(TableInformationSource) entityElement,
sourceMappingDocument.getMappingLocalBindingContext().determineEntityName( entityElement )
)
: null;
} }
@Override @Override
public TableSource getPrimaryTable() { public TableSpecificationSource getPrimaryTable() {
if ( JaxbJoinedSubclassElement.class.isInstance( entityElement() ) ) { return primaryTable;
return new TableSource() {
@Override
public String getExplicitSchemaName() {
return ( (JaxbJoinedSubclassElement) entityElement() ).getSchema();
}
@Override
public String getExplicitCatalogName() {
return ( (JaxbJoinedSubclassElement) entityElement() ).getCatalog();
}
@Override
public String getExplicitTableName() {
return ( (JaxbJoinedSubclassElement) entityElement() ).getTable();
}
@Override
public String getLogicalName() {
// logical name for the primary table is null
return null;
}
};
}
else if ( JaxbUnionSubclassElement.class.isInstance( entityElement() ) ) {
return new TableSource() {
@Override
public String getExplicitSchemaName() {
return ( (JaxbUnionSubclassElement) entityElement() ).getSchema();
}
@Override
public String getExplicitCatalogName() {
return ( (JaxbUnionSubclassElement) entityElement() ).getCatalog();
}
@Override
public String getExplicitTableName() {
return ( (JaxbUnionSubclassElement) entityElement() ).getTable();
}
@Override
public String getLogicalName() {
// logical name for the primary table is null
return null;
}
};
}
return null;
} }
@Override @Override
@ -105,11 +63,6 @@ public class SubclassEntitySourceImpl extends AbstractEntitySourceImpl implement
: null; : null;
} }
/**
* {@inheritDoc}
*
* @see SubclassEntitySource#superclassEntitySource()
*/
@Override @Override
public EntitySource superclassEntitySource() { public EntitySource superclassEntitySource() {
return container; return container;

View File

@ -0,0 +1,56 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, 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.internal.source.hbm;
import org.hibernate.metamodel.spi.source.TableSource;
/**
* @author Steve Ebersole
*/
public class TableSourceImpl implements TableSource {
private final String schema;
private final String catalog;
private final String tableName;
TableSourceImpl(String schema, String catalog, String tableName) {
this.schema = schema;
this.catalog = catalog;
this.tableName = tableName;
}
@Override
public String getExplicitSchemaName() {
return schema;
}
@Override
public String getExplicitCatalogName() {
return catalog;
}
@Override
public String getExplicitTableName() {
return tableName;
}
}

View File

@ -32,7 +32,6 @@ import org.hibernate.FetchMode;
import org.hibernate.engine.FetchStyle; import org.hibernate.engine.FetchStyle;
import org.hibernate.engine.FetchTiming; import org.hibernate.engine.FetchTiming;
import org.hibernate.metamodel.spi.domain.PluralAttribute; import org.hibernate.metamodel.spi.domain.PluralAttribute;
import org.hibernate.metamodel.spi.relational.Table;
import org.hibernate.metamodel.spi.relational.TableSpecification; import org.hibernate.metamodel.spi.relational.TableSpecification;
import org.hibernate.metamodel.spi.source.MetaAttributeContext; import org.hibernate.metamodel.spi.source.MetaAttributeContext;
import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.collection.CollectionPersister;
@ -46,7 +45,7 @@ public abstract class AbstractPluralAttributeBinding extends AbstractAttributeBi
private final PluralAttributeKeyBinding pluralAttributeKeyBinding; private final PluralAttributeKeyBinding pluralAttributeKeyBinding;
private final AbstractPluralAttributeElementBinding pluralAttributeElementBinding; private final AbstractPluralAttributeElementBinding pluralAttributeElementBinding;
private Table collectionTable; private TableSpecification collectionTable;
private FetchTiming fetchTiming; private FetchTiming fetchTiming;
private FetchStyle fetchStyle; private FetchStyle fetchStyle;
@ -167,7 +166,7 @@ public abstract class AbstractPluralAttributeBinding extends AbstractAttributeBi
return collectionTable; return collectionTable;
} }
public void setCollectionTable(Table collectionTable) { public void setCollectionTable(TableSpecification collectionTable) {
this.collectionTable = collectionTable; this.collectionTable = collectionTable;
} }

View File

@ -36,10 +36,10 @@ import org.hibernate.dialect.Dialect;
*/ */
public class InLineView extends AbstractTableSpecification { public class InLineView extends AbstractTableSpecification {
private final Schema schema; private final Schema schema;
private final String logicalName; private final Identifier logicalName;
private final String select; private final String select;
public InLineView(Schema schema, String logicalName, String select) { public InLineView(Schema schema, Identifier logicalName, String select) {
this.schema = schema; this.schema = schema;
this.logicalName = logicalName; this.logicalName = logicalName;
this.select = select; this.select = select;
@ -50,7 +50,7 @@ public class InLineView extends AbstractTableSpecification {
} }
@Override @Override
public String getLogicalName() { public Identifier getLogicalName() {
return logicalName; return logicalName;
} }
@ -60,7 +60,7 @@ public class InLineView extends AbstractTableSpecification {
@Override @Override
public String getLoggableValueQualifier() { public String getLoggableValueQualifier() {
return logicalName; return logicalName.getName();
} }
@Override @Override

View File

@ -33,7 +33,7 @@ import java.util.Map;
*/ */
public class Schema { public class Schema {
private final Name name; private final Name name;
private Map<String, InLineView> inLineViews = new HashMap<String, InLineView>(); private Map<Identifier, InLineView> inLineViews = new HashMap<Identifier, InLineView>();
private Map<Identifier, Table> tables = new HashMap<Identifier, Table>(); private Map<Identifier, Table> tables = new HashMap<Identifier, Table>();
public Schema(Name name) { public Schema(Name name) {
@ -49,72 +49,41 @@ public class Schema {
} }
/** /**
* Returns the table with the specified table name. * Returns the table with the specified logical table name.
* *
* @param tableName - the name of the table * @param logicalTableName - the logical name of the table
* *
* @return the table with the specified table name, * @return the table with the specified table name,
* or null if there is no table with the specified * or null if there is no table with the specified
* table name. * table name.
*/ */
public Table locateTable(Identifier tableName) { public Table locateTable(Identifier logicalTableName) {
return tables.get( tableName ); return tables.get( logicalTableName );
} }
/** /**
* Creates a {@link Table} with the specified name. * Creates a {@link Table} with the specified name.
* *
* @param tableName - the name of the table * @param logicalTableName The logical table name
* @param physicalTableName - the name of the table
* *
* @return the created table. * @return the created table.
*/ */
public Table createTable(Identifier tableName) { public Table createTable(Identifier logicalTableName, Identifier physicalTableName) {
Table table = new Table( this, tableName ); Table table = new Table( this, logicalTableName, physicalTableName );
tables.put( tableName, table ); tables.put( logicalTableName, table );
return table; return table;
} }
/**
* Locates a {@link Table} with the specified name; if
* it does not exist, then a table is created with
* the specified name.
*
* @param tableName - the name of the table
*
* @return the located or created table.
*/
public Table locateOrCreateTable(Identifier tableName) {
final Table existing = locateTable( tableName );
if ( existing == null ) {
return createTable( tableName );
}
return existing;
}
/* package-protected */
void remapTableName(Identifier oldTableName) {
Table table = tables.remove( oldTableName );
if ( table == null ) {
throw new IllegalStateException(
String.format(
"Schema (%s) does not contain a table (%s) to remap.",
name,
oldTableName
)
);
}
tables.put( table.getTableName(), table );
}
public Iterable<Table> getTables() { public Iterable<Table> getTables() {
return tables.values(); return tables.values();
} }
public InLineView getInLineView(String logicalName) { public InLineView getInLineView(Identifier logicalName) {
return inLineViews.get( logicalName ); return inLineViews.get( logicalName );
} }
public InLineView createInLineView(String logicalName, String subSelect) { public InLineView createInLineView(Identifier logicalName, String subSelect) {
InLineView inLineView = new InLineView( this, logicalName, subSelect ); InLineView inLineView = new InLineView( this, logicalName, subSelect );
inLineViews.put( logicalName, inLineView ); inLineViews.put( logicalName, inLineView );
return inLineView; return inLineView;

View File

@ -37,9 +37,10 @@ import org.hibernate.dialect.Dialect;
*/ */
public class Table extends AbstractTableSpecification implements Exportable { public class Table extends AbstractTableSpecification implements Exportable {
private final Schema database; private final Schema database;
private Identifier tableName; private Identifier physicalName;
private Identifier logicalName;
private ObjectName objectName; private ObjectName objectName;
private String qualifiedName; private String exportIdentifier;
private final LinkedHashMap<String,Index> indexes = new LinkedHashMap<String,Index>(); private final LinkedHashMap<String,Index> indexes = new LinkedHashMap<String,Index>();
private final LinkedHashMap<String,UniqueKey> uniqueKeys = new LinkedHashMap<String,UniqueKey>(); private final LinkedHashMap<String,UniqueKey> uniqueKeys = new LinkedHashMap<String,UniqueKey>();
@ -50,21 +51,15 @@ public class Table extends AbstractTableSpecification implements Exportable {
* Constructs a {@link Table} instance. * Constructs a {@link Table} instance.
* *
* @param database - the schema * @param database - the schema
* @param tableName - the table name as a String * @param logicalName - The logical name
* @param physicalName - the physical table name.
*/ */
public Table(Schema database, String tableName) { public Table(Schema database, Identifier logicalName, Identifier physicalName) {
this( database, Identifier.toIdentifier( tableName ) );
}
/**
* Constructs a {@link Table} instance.
*
* @param database - the schema
* @param tableName - the table name as an {@link Identifier}
*/
public Table(Schema database, Identifier tableName) {
this.database = database; this.database = database;
setTableName( tableName ); this.logicalName = logicalName;
this.physicalName = physicalName;
this.objectName = new ObjectName( database, physicalName );
this.exportIdentifier = objectName.toText();
} }
@Override @Override
@ -78,29 +73,8 @@ public class Table extends AbstractTableSpecification implements Exportable {
* @return the logical table name. * @return the logical table name.
*/ */
@Override @Override
public String getLogicalName() { public Identifier getLogicalName() {
return tableName.getName(); return logicalName;
}
/**
* Sets the table name, remapping this {@link Table} by the new
* table name in the {@link Schema}, if necessary.
*
* @param tableName - the table name
*/
public final void setTableName(Identifier tableName) {
if ( tableName == null ) {
throw new IllegalArgumentException( "tableName cannot be null." );
}
if ( !tableName.equals( this.tableName ) ) {
Identifier tableNameOld = this.tableName;
this.tableName = tableName;
objectName = new ObjectName( database.getName().getSchema(), database.getName().getCatalog(), tableName );
this.qualifiedName = objectName.toText();
if ( tableNameOld != null ) {
database.remapTableName( tableNameOld );
}
}
} }
/** /**
@ -108,22 +82,22 @@ public class Table extends AbstractTableSpecification implements Exportable {
* @return the table name. * @return the table name.
*/ */
public Identifier getTableName() { public Identifier getTableName() {
return tableName; return physicalName;
} }
@Override @Override
public String getLoggableValueQualifier() { public String getLoggableValueQualifier() {
return qualifiedName; return exportIdentifier;
} }
@Override @Override
public String getExportIdentifier() { public String getExportIdentifier() {
return qualifiedName; return exportIdentifier;
} }
@Override @Override
public String toLoggableString() { public String toLoggableString() {
return qualifiedName; return exportIdentifier;
} }
@Override @Override
@ -341,6 +315,6 @@ public class Table extends AbstractTableSpecification implements Exportable {
@Override @Override
public String toString() { public String toString() {
return "Table{name=" + qualifiedName + '}'; return "Table{name=" + exportIdentifier + '}';
} }
} }

View File

@ -43,7 +43,7 @@ public interface TableSpecification extends ValueContainer, Loggable {
* *
* @return the logical table name. * @return the logical table name.
*/ */
public String getLogicalName(); public Identifier getLogicalName();
/** /**
* Get the table number. * Get the table number.

View File

@ -24,6 +24,7 @@
package org.hibernate.metamodel.spi.source; package org.hibernate.metamodel.spi.source;
import java.util.List; import java.util.List;
import java.util.Set;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.EntityListeners; import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass; import javax.persistence.MappedSuperclass;
@ -78,14 +79,14 @@ public interface EntitySource extends SubclassEntityContainer, AttributeSourceCo
* *
* @return The primary table. * @return The primary table.
*/ */
public TableSource getPrimaryTable(); public TableSpecificationSource getPrimaryTable();
/** /**
* Obtain the secondary tables for this entity * Obtain the secondary tables for this entity
* *
* @return returns an iterator over the secondary tables for this entity * @return returns an iterator over the secondary tables for this entity
*/ */
public Iterable<TableSource> getSecondaryTables(); public Set<SecondaryTableSource> getSecondaryTables();
/** /**
* Obtain the name of a custom tuplizer class to be used. * Obtain the name of a custom tuplizer class to be used.

View File

@ -0,0 +1,41 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, 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.spi.source;
/**
* Describes in-line view source information. Generally, either {@link org.hibernate.annotations.Subselect}
* or {@code <subselect/>}
*
* @author Steve Ebersole
*/
public interface InLineViewSource extends TableSpecificationSource {
/**
* Obtain the {@code SQL SELECT} statement to use. Cannot be null!
*
* @return The {@code SQL SELECT} statement
*/
public String getSelectStatement();
public String getLogicalName();
}

View File

@ -37,6 +37,7 @@ public interface PluralAttributeSource
public PluralAttributeElementSource getElementSource(); public PluralAttributeElementSource getElementSource();
public TableSpecificationSource getCollectionTableSpecificationSource();
public String getExplicitSchemaName(); public String getExplicitSchemaName();
public String getExplicitCatalogName(); public String getExplicitCatalogName();
public String getExplicitCollectionTableName(); public String getExplicitCollectionTableName();

View File

@ -0,0 +1,33 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, 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.spi.source;
/**
* @author Steve Ebersole
*/
public interface PrimaryKeyJoinColumnSource {
public String getColumnName();
public String getReferencedColumnName();
public String getColumnDefinition();
}

View File

@ -0,0 +1,52 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, 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.spi.source;
import java.util.List;
/**
* @author Steve Ebersole
*/
public interface SecondaryTableSource {
/**
* Obtain the table being joined to.
*
* @return The joined table.
*/
public TableSpecificationSource getTableSource();
/**
* Retrieves the columns used to define the foreign key back to the entity table.
*
* @return The columns used to define the foreign key for this secondary table
*/
public List<PrimaryKeyJoinColumnSource> getJoinColumns();
/**
* Retrieve any user-specified foreign key name.
*
* @return The user-specified foreign key name, or {@code null} if the user did not specify.
*/
public String getForeignKeyName();
}

View File

@ -28,35 +28,11 @@ package org.hibernate.metamodel.spi.source;
* *
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public interface TableSource { public interface TableSource extends TableSpecificationSource {
/**
* Obtain the supplied schema name
*
* @return The schema name. If {@code null}, the binder will apply the default.
*/
public String getExplicitSchemaName();
/**
* Obtain the supplied catalog name
*
* @return The catalog name. If {@code null}, the binder will apply the default.
*/
public String getExplicitCatalogName();
/** /**
* Obtain the supplied table name. * Obtain the supplied table name.
* *
* @return The table name. * @return The table name, or {@code null} is no name specified.
*/ */
public String getExplicitTableName(); public String getExplicitTableName();
/**
* Obtain the logical name of the table. This value is used to uniquely reference the table when binding
* values to the binding model.
*
* @return The logical name. Can be {@code null} in the case of the "primary table".
*
* @see RelationalValueSource#getContainingTableName()
*/
public String getLogicalName();
} }

View File

@ -0,0 +1,47 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, 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.spi.source;
/**
* Contract describing source of "table specification" information.
*
* @author Steve Ebersole
*
* @see org.hibernate.metamodel.spi.relational.TableSpecification
*/
public interface TableSpecificationSource {
/**
* Obtain the supplied schema name
*
* @return The schema name. If {@code null}, the binder will apply the default.
*/
public String getExplicitSchemaName();
/**
* Obtain the supplied catalog name
*
* @return The catalog name. If {@code null}, the binder will apply the default.
*/
public String getExplicitCatalogName();
}

View File

@ -20,6 +20,7 @@
<!-- Mix-ins --> <!-- Mix-ins -->
<jaxb:bindings node="//xsd:element[@name='class']/xsd:complexType"> <jaxb:bindings node="//xsd:element[@name='class']/xsd:complexType">
<inheritance:implements>org.hibernate.internal.jaxb.mapping.hbm.EntityElement</inheritance:implements> <inheritance:implements>org.hibernate.internal.jaxb.mapping.hbm.EntityElement</inheritance:implements>
<inheritance:implements>org.hibernate.internal.jaxb.mapping.hbm.TableInformationSource</inheritance:implements>
<inheritance:implements>org.hibernate.internal.jaxb.mapping.hbm.JoinElementSource</inheritance:implements> <inheritance:implements>org.hibernate.internal.jaxb.mapping.hbm.JoinElementSource</inheritance:implements>
</jaxb:bindings> </jaxb:bindings>
<jaxb:bindings node="//xsd:complexType[@name='subclass-element']"> <jaxb:bindings node="//xsd:complexType[@name='subclass-element']">
@ -28,10 +29,16 @@
</jaxb:bindings> </jaxb:bindings>
<jaxb:bindings node="//xsd:complexType[@name='joined-subclass-element']"> <jaxb:bindings node="//xsd:complexType[@name='joined-subclass-element']">
<inheritance:implements>org.hibernate.internal.jaxb.mapping.hbm.SubEntityElement</inheritance:implements> <inheritance:implements>org.hibernate.internal.jaxb.mapping.hbm.SubEntityElement</inheritance:implements>
<inheritance:implements>org.hibernate.internal.jaxb.mapping.hbm.TableInformationSource</inheritance:implements>
</jaxb:bindings> </jaxb:bindings>
<jaxb:bindings node="//xsd:complexType[@name='union-subclass-element']"> <jaxb:bindings node="//xsd:complexType[@name='union-subclass-element']">
<inheritance:implements>org.hibernate.internal.jaxb.mapping.hbm.SubEntityElement</inheritance:implements> <inheritance:implements>org.hibernate.internal.jaxb.mapping.hbm.SubEntityElement</inheritance:implements>
<inheritance:implements>org.hibernate.internal.jaxb.mapping.hbm.TableInformationSource</inheritance:implements>
</jaxb:bindings> </jaxb:bindings>
<jaxb:bindings node="//xsd:complexType[@name='join-element']">
<inheritance:implements>org.hibernate.internal.jaxb.mapping.hbm.TableInformationSource</inheritance:implements>
</jaxb:bindings>
<jaxb:bindings node="//xsd:complexType[@name='sql-insert-element']"> <jaxb:bindings node="//xsd:complexType[@name='sql-insert-element']">
<inheritance:implements>org.hibernate.internal.jaxb.mapping.hbm.CustomSqlElement</inheritance:implements> <inheritance:implements>org.hibernate.internal.jaxb.mapping.hbm.CustomSqlElement</inheritance:implements>
</jaxb:bindings> </jaxb:bindings>

View File

@ -40,6 +40,7 @@ import org.hibernate.metamodel.spi.source.SimpleIdentifierSource;
import org.hibernate.metamodel.spi.source.SingularAttributeNature; import org.hibernate.metamodel.spi.source.SingularAttributeNature;
import org.hibernate.metamodel.spi.source.SingularAttributeSource; import org.hibernate.metamodel.spi.source.SingularAttributeSource;
import org.hibernate.metamodel.spi.source.TableSource; import org.hibernate.metamodel.spi.source.TableSource;
import org.hibernate.metamodel.spi.source.TableSpecificationSource;
import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.service.ServiceRegistryBuilder;
@ -98,10 +99,11 @@ public class AssertSourcesTest extends BaseUnitTestCase {
assertTrue( entitySource.getJpaCallbackClasses() == null || entitySource.getJpaCallbackClasses().isEmpty() ); assertTrue( entitySource.getJpaCallbackClasses() == null || entitySource.getJpaCallbackClasses().isEmpty() );
TableSource primaryTable = entitySource.getPrimaryTable(); TableSpecificationSource primaryTableSpecificationSource = entitySource.getPrimaryTable();
assertTrue( TableSource.class.isInstance( primaryTableSpecificationSource ) );
TableSource primaryTable = (TableSource) primaryTableSpecificationSource;
// todo : should sources be responsible for figuring out logical names? // todo : should sources be responsible for figuring out logical names?
// these are the things that need to match in terms of lookup keys // these are the things that need to match in terms of lookup keys
assertNull( primaryTable.getLogicalName() );
assertNull( primaryTable.getExplicitCatalogName() ); assertNull( primaryTable.getExplicitCatalogName() );
assertNull( primaryTable.getExplicitSchemaName() ); assertNull( primaryTable.getExplicitSchemaName() );
assertNull( primaryTable.getExplicitTableName() ); assertNull( primaryTable.getExplicitTableName() );

View File

@ -35,6 +35,7 @@ import org.hibernate.mapping.PropertyGeneration;
import org.hibernate.metamodel.spi.domain.Entity; import org.hibernate.metamodel.spi.domain.Entity;
import org.hibernate.metamodel.spi.domain.SingularAttribute; import org.hibernate.metamodel.spi.domain.SingularAttribute;
import org.hibernate.metamodel.spi.relational.Column; import org.hibernate.metamodel.spi.relational.Column;
import org.hibernate.metamodel.spi.relational.Identifier;
import org.hibernate.metamodel.spi.relational.JdbcDataType; import org.hibernate.metamodel.spi.relational.JdbcDataType;
import org.hibernate.metamodel.spi.relational.Schema; import org.hibernate.metamodel.spi.relational.Schema;
import org.hibernate.metamodel.spi.relational.Size; import org.hibernate.metamodel.spi.relational.Size;
@ -56,7 +57,8 @@ public class SimpleValueBindingTests extends BaseUnitTestCase {
@Test @Test
public void testBasicMiddleOutBuilding() { public void testBasicMiddleOutBuilding() {
Table table = new Table( new Schema( null, null ), "the_table" ); final Identifier tableName = Identifier.toIdentifier( "the_table" );
Table table = new Table( new Schema( null, null ), tableName, tableName );
Column idColumn = table.locateOrCreateColumn( "id" ); Column idColumn = table.locateOrCreateColumn( "id" );
idColumn.setJdbcDataType( BIGINT ); idColumn.setJdbcDataType( BIGINT );
idColumn.setSize( Size.precision( 18, 0 ) ); idColumn.setSize( Size.precision( 18, 0 ) );

View File

@ -48,7 +48,7 @@ public class TableManipulationTests extends BaseUnitTestCase {
@Test @Test
public void testTableCreation() { public void testTableCreation() {
Schema schema = new Schema( null, null ); Schema schema = new Schema( null, null );
Table table = schema.createTable( Identifier.toIdentifier( "my_table" ) ); Table table = schema.createTable( Identifier.toIdentifier( "my_table" ), Identifier.toIdentifier( "my_table" ) );
assertNull( table.getSchema().getName().getSchema() ); assertNull( table.getSchema().getName().getSchema() );
assertNull( table.getSchema().getName().getCatalog() ); assertNull( table.getSchema().getName().getCatalog() );
assertEquals( "my_table", table.getTableName().toString() ); assertEquals( "my_table", table.getTableName().toString() );
@ -92,10 +92,10 @@ public class TableManipulationTests extends BaseUnitTestCase {
@Test @Test
public void testTableSpecificationCounter() { public void testTableSpecificationCounter() {
Schema schema = new Schema( null, null ); Schema schema = new Schema( null, null );
Table table = schema.createTable( Identifier.toIdentifier( "my_table" ) ); Table table = schema.createTable( Identifier.toIdentifier( "my_table" ), Identifier.toIdentifier( "my_table" ) );
InLineView inLineView = schema.createInLineView( "my_inlineview", "subselect" ); InLineView inLineView = schema.createInLineView( Identifier.toIdentifier( "my_inlineview" ), "subselect" );
InLineView otherInLineView = schema.createInLineView( "my_other_inlineview", "other subselect" ); InLineView otherInLineView = schema.createInLineView( Identifier.toIdentifier( "my_other_inlineview" ), "other subselect" );
Table otherTable = schema.createTable( Identifier.toIdentifier( "my_other_table" ) ); Table otherTable = schema.createTable( Identifier.toIdentifier( "my_other_table" ), Identifier.toIdentifier( "my_other_table" ) );
int firstTableNumber = table.getTableNumber(); int firstTableNumber = table.getTableNumber();
assertEquals( firstTableNumber, table.getTableNumber() ); assertEquals( firstTableNumber, table.getTableNumber() );
@ -107,7 +107,7 @@ public class TableManipulationTests extends BaseUnitTestCase {
@Test @Test
public void testBasicForeignKeyDefinition() { public void testBasicForeignKeyDefinition() {
Schema schema = new Schema( null, null ); Schema schema = new Schema( null, null );
Table book = schema.createTable( Identifier.toIdentifier( "BOOK" ) ); Table book = schema.createTable( Identifier.toIdentifier( "BOOK" ), Identifier.toIdentifier( "BOOK" ) );
Column bookId = book.locateOrCreateColumn( "id" ); Column bookId = book.locateOrCreateColumn( "id" );
bookId.setJdbcDataType( INTEGER ); bookId.setJdbcDataType( INTEGER );
@ -115,7 +115,7 @@ public class TableManipulationTests extends BaseUnitTestCase {
book.getPrimaryKey().addColumn( bookId ); book.getPrimaryKey().addColumn( bookId );
book.getPrimaryKey().setName( "BOOK_PK" ); book.getPrimaryKey().setName( "BOOK_PK" );
Table page = schema.createTable( Identifier.toIdentifier( "PAGE" ) ); Table page = schema.createTable( Identifier.toIdentifier( "PAGE" ), Identifier.toIdentifier( "PAGE" ) );
Column pageId = page.locateOrCreateColumn( "id" ); Column pageId = page.locateOrCreateColumn( "id" );
pageId.setJdbcDataType( INTEGER ); pageId.setJdbcDataType( INTEGER );
@ -137,17 +137,17 @@ public class TableManipulationTests extends BaseUnitTestCase {
public void testQualifiedName() { public void testQualifiedName() {
Dialect dialect = new H2Dialect(); Dialect dialect = new H2Dialect();
Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) ); Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) );
Table table = schema.createTable( Identifier.toIdentifier( "my_table" ) ); Table table = schema.createTable( Identifier.toIdentifier( "my_table" ), Identifier.toIdentifier( "my_table" ) );
assertEquals( "my_table", table.getTableName().getName() ); assertEquals( "my_table", table.getTableName().getName() );
assertEquals( "my_table", table.getTableName().toString() ); assertEquals( "my_table", table.getTableName().toString() );
assertEquals( "schema.\"catalog\".my_table", table.getQualifiedName( dialect ) ); assertEquals( "schema.\"catalog\".my_table", table.getQualifiedName( dialect ) );
table = schema.createTable( Identifier.toIdentifier( "`my_table`" ) ); table = schema.createTable( Identifier.toIdentifier( "`my_table`" ), Identifier.toIdentifier( "`my_table`" ) );
assertEquals( "my_table", table.getTableName().getName() ); assertEquals( "my_table", table.getTableName().getName() );
assertEquals( "`my_table`", table.getTableName().toString() ); assertEquals( "`my_table`", table.getTableName().toString() );
assertEquals( "schema.\"catalog\".\"my_table\"", table.getQualifiedName( dialect ) ); assertEquals( "schema.\"catalog\".\"my_table\"", table.getQualifiedName( dialect ) );
InLineView inLineView = schema.createInLineView( "my_inlineview", "select ..." ); InLineView inLineView = schema.createInLineView( Identifier.toIdentifier( "my_inlineview" ), "select ..." );
assertEquals( "( select ... )", inLineView.getQualifiedName( dialect ) ); assertEquals( "( select ... )", inLineView.getQualifiedName( dialect ) );
} }
@ -156,22 +156,10 @@ public class TableManipulationTests extends BaseUnitTestCase {
Identifier tableIdentifier = Identifier.toIdentifier( "my_table" ); Identifier tableIdentifier = Identifier.toIdentifier( "my_table" );
assertEquals( "my_table", tableIdentifier.getName() ); assertEquals( "my_table", tableIdentifier.getName() );
Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) ); Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) );
Table table = schema.createTable( tableIdentifier ); Table table = schema.createTable( tableIdentifier, tableIdentifier );
assertSame( tableIdentifier, table.getTableName() ); assertSame( tableIdentifier, table.getTableName() );
assertSame( table, schema.locateTable( Identifier.toIdentifier( "my_table" ) ) ); assertSame( table, schema.locateTable( Identifier.toIdentifier( "my_table" ) ) );
assertEquals( "my_table", table.getLogicalName() ); assertEquals( "my_table", table.getLogicalName().getName() );
tableIdentifier = Identifier.toIdentifier( "my_new_table" );
table.setTableName( tableIdentifier );
assertEquals( "my_new_table", table.getLogicalName() );
assertEquals( "my_new_table", table.getTableName().getName() );
assertSame( table, schema.locateTable( Identifier.toIdentifier( "my_new_table" ) ) );
assertNull( schema.locateTable( Identifier.toIdentifier( "my_table" ) ) );
tableIdentifier = Identifier.toIdentifier( "my_newer_table" );
table.setTableName( tableIdentifier );
assertEquals( "my_newer_table", table.getLogicalName() );
assertEquals( "my_newer_table", table.getTableName().getName() );
assertSame( table, schema.locateTable( Identifier.toIdentifier( "my_newer_table" ) ) );
assertNull( schema.locateTable( Identifier.toIdentifier( "my_new_table" ) ) );
} }
@Test @Test
@ -179,42 +167,20 @@ public class TableManipulationTests extends BaseUnitTestCase {
Identifier tableIdentifier = Identifier.toIdentifier( "`my_table`" ); Identifier tableIdentifier = Identifier.toIdentifier( "`my_table`" );
assertEquals( "my_table", tableIdentifier.getName() ); assertEquals( "my_table", tableIdentifier.getName() );
Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) ); Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) );
Table table = schema.createTable( tableIdentifier ); Table table = schema.createTable( tableIdentifier, tableIdentifier );
assertSame( tableIdentifier, table.getTableName() ); assertSame( tableIdentifier, table.getTableName() );
assertSame( table, schema.locateTable( Identifier.toIdentifier( "`my_table`" ) ) ); assertSame( table, schema.locateTable( Identifier.toIdentifier( "`my_table`" ) ) );
assertEquals( "my_table", table.getLogicalName() ); assertEquals( "my_table", table.getLogicalName().getName() );
assertTrue( table.getLogicalName().isQuoted() );
assertNull( schema.locateTable( Identifier.toIdentifier( "my_table" ) ) ); assertNull( schema.locateTable( Identifier.toIdentifier( "my_table" ) ) );
tableIdentifier = Identifier.toIdentifier( "`my_new_table`" );
table.setTableName( tableIdentifier );
assertEquals( "my_new_table", table.getLogicalName() );
assertEquals( "my_new_table", table.getTableName().getName() );
assertSame( table, schema.locateTable( Identifier.toIdentifier( "`my_new_table`" ) ) );
assertNull( schema.locateTable( Identifier.toIdentifier( "`my_table`" ) ) );
assertNull( schema.locateTable( Identifier.toIdentifier( "my_new_table" ) ) );
tableIdentifier = Identifier.toIdentifier( "`my_newer_table`" );
table.setTableName( tableIdentifier );
assertEquals( "my_newer_table", table.getLogicalName() );
assertEquals( "my_newer_table", table.getTableName().getName() );
assertSame( table, schema.locateTable( Identifier.toIdentifier( "`my_newer_table`" ) ) );
assertNull( schema.locateTable( Identifier.toIdentifier( "`my_new_table`" ) ) );
assertNull( schema.locateTable( Identifier.toIdentifier( "my_newer_table" ) ) );
} }
@Test @Test
public void testInLineViewLogicalName() { public void testInLineViewLogicalName() {
Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) ); Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) );
InLineView view = schema.createInLineView( "my_view", "select" ); InLineView view = schema.createInLineView( Identifier.toIdentifier( "my_view" ), "select" );
assertEquals( "my_view", view.getLogicalName() ); assertEquals( "my_view", view.getLogicalName().getName() );
assertEquals( "select", view.getSelect() ); assertEquals( "select", view.getSelect() );
assertSame( view, schema.getInLineView( view.getLogicalName() ) ); assertSame( view, schema.getInLineView( view.getLogicalName() ) );
} }
@Test
public void testLocateOrCreateTable() {
Schema schema = new Schema( Identifier.toIdentifier( "schema" ), Identifier.toIdentifier( "`catalog`" ) );
Identifier tableIdentifier = Identifier.toIdentifier( "my_table" );
Table table = schema.locateOrCreateTable( tableIdentifier );
assertSame( tableIdentifier, table.getTableName() );
assertSame( table, schema.locateOrCreateTable( tableIdentifier ) );
}
} }