HHH-9388 HHH-7079 : Default collection table and foreign key names are incorrect in some cases; deprecate NamingStrategy

This commit is contained in:
Gail Badner 2014-10-01 23:59:05 -07:00
parent 1883fcbb7b
commit 36576046b8
38 changed files with 2053 additions and 86 deletions

View File

@ -33,6 +33,7 @@ import org.dom4j.Document;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.Interceptor; import org.hibernate.Interceptor;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
/** /**
* Similar to the {@link Configuration} object but handles EJB3 and Hibernate * Similar to the {@link Configuration} object but handles EJB3 and Hibernate
@ -239,6 +240,12 @@ public class AnnotationConfiguration extends Configuration {
return this; return this;
} }
@Override
public AnnotationConfiguration setNamingStrategyDelegator(NamingStrategyDelegator namingStrategyDelegator) {
super.setNamingStrategyDelegator( namingStrategyDelegator );
return this;
}
@Deprecated @Deprecated
protected class ExtendedMappingsImpl extends MappingsImpl { protected class ExtendedMappingsImpl extends MappingsImpl {
} }

View File

@ -76,6 +76,9 @@ import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.annotations.common.reflection.XClass; import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.annotations.common.reflection.java.JavaReflectionManager; import org.hibernate.annotations.common.reflection.java.JavaReflectionManager;
import org.hibernate.cfg.annotations.reflection.JPAMetadataProvider; import org.hibernate.cfg.annotations.reflection.JPAMetadataProvider;
import org.hibernate.cfg.naming.DefaultNamingStrategyDelegator;
import org.hibernate.cfg.naming.LegacyNamingStrategyDelegator;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver; import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.MySQLDialect; import org.hibernate.dialect.MySQLDialect;
@ -237,7 +240,7 @@ public class Configuration implements Serializable {
private EntityNotFoundDelegate entityNotFoundDelegate; private EntityNotFoundDelegate entityNotFoundDelegate;
protected transient XMLHelper xmlHelper; protected transient XMLHelper xmlHelper;
protected NamingStrategy namingStrategy; private NamingStrategyDelegator namingStrategyDelegator;
private SessionFactoryObserver sessionFactoryObserver; private SessionFactoryObserver sessionFactoryObserver;
protected final SettingsFactory settingsFactory; protected final SettingsFactory settingsFactory;
@ -332,7 +335,7 @@ public class Configuration implements Serializable {
mappedByResolver = new HashMap<String, String>(); mappedByResolver = new HashMap<String, String>();
propertyRefResolver = new HashMap<String, String>(); propertyRefResolver = new HashMap<String, String>();
caches = new ArrayList<CacheHolder>(); caches = new ArrayList<CacheHolder>();
namingStrategy = EJB3NamingStrategy.INSTANCE; namingStrategyDelegator = DefaultNamingStrategyDelegator.INSTANCE;
setEntityResolver( new EJB3DTDEntityResolver() ); setEntityResolver( new EJB3DTDEntityResolver() );
anyMetaDefs = new HashMap<String, AnyMetaDef>(); anyMetaDefs = new HashMap<String, AnyMetaDef>();
propertiesAnnotatedWithMapsId = new HashMap<XClass, Map<String, PropertyData>>(); propertiesAnnotatedWithMapsId = new HashMap<XClass, Map<String, PropertyData>>();
@ -2375,18 +2378,57 @@ public class Configuration implements Serializable {
} }
public NamingStrategy getNamingStrategy() { public NamingStrategy getNamingStrategy() {
return namingStrategy; if ( LegacyNamingStrategyDelegator.class.isInstance( namingStrategyDelegator ) ) {
return ( (LegacyNamingStrategyDelegator) namingStrategyDelegator ).getNamingStrategy();
}
return null;
}
public NamingStrategyDelegator getNamingStrategyDelegator() {
return namingStrategyDelegator;
} }
/** /**
* Set a custom naming strategy * Set the current naming strategy. An instance of {@link org.hibernate.cfg.naming.LegacyNamingStrategyDelegator}
* will be constructed with the specified naming strategy.
* *
* @param namingStrategy the NamingStrategy to set * @param namingStrategy the {@link NamingStrategy} to set; must be non-null.
* *
* @return this for method chaining * @return this for method chaining.
*
* @see org.hibernate.cfg.naming.LegacyNamingStrategyDelegator#LegacyNamingStrategyDelegator(NamingStrategy)
*/ */
public Configuration setNamingStrategy(NamingStrategy namingStrategy) { public Configuration setNamingStrategy(NamingStrategy namingStrategy) {
this.namingStrategy = namingStrategy; if ( namingStrategy == null ) {
throw new MappingException( "namingStrategy must be non-null" );
}
setNamingStrategyDelegator( new LegacyNamingStrategyDelegator( namingStrategy ) );
return this;
}
/**
* Set a current naming strategy delegator.
*
* @param namingStrategyDelegator the {@link org.hibernate.cfg.naming.NamingStrategyDelegator} to set;
* must be non-null; if {@code namingStrategyDelegator} is an instance
* of {@link LegacyNamingStrategyDelegator}, then
* {@link LegacyNamingStrategyDelegator#getNamingStrategy()} must be non-null.
* @return this for method chaining.
*
* @throws org.hibernate.MappingException if {@code namingStrategyDelegator} is null.
* @throws org.hibernate.MappingException if {@code namingStrategyDelegator} is an instance
* of {@link LegacyNamingStrategyDelegator} and {@link LegacyNamingStrategyDelegator#getNamingStrategy()}
* is null.
*/
public Configuration setNamingStrategyDelegator(NamingStrategyDelegator namingStrategyDelegator) {
if ( namingStrategyDelegator == null ) {
throw new MappingException( "namingStrategyDelegator and namingStrategyDelegator.getNamingStrategy() must be non-null" );
}
if ( LegacyNamingStrategyDelegator.class.isInstance( namingStrategyDelegator ) &&
LegacyNamingStrategyDelegator.class.cast( namingStrategyDelegator ).getNamingStrategy() == null ) {
throw new MappingException( "namingStrategyDelegator.getNamingStrategy() must be non-null." );
}
this.namingStrategyDelegator = namingStrategyDelegator;
return this; return this;
} }
@ -2630,11 +2672,19 @@ public class Configuration implements Serializable {
public NamingStrategy getNamingStrategy() { public NamingStrategy getNamingStrategy() {
return namingStrategy; return Configuration.this.getNamingStrategy();
} }
public void setNamingStrategy(NamingStrategy namingStrategy) { public void setNamingStrategy(NamingStrategy namingStrategy) {
Configuration.this.namingStrategy = namingStrategy; Configuration.this.setNamingStrategy( namingStrategy );
}
public NamingStrategyDelegator getNamingStrategyDelegator() {
return Configuration.this.getNamingStrategyDelegator();
}
public void setNamingStrategyDelegator(NamingStrategyDelegator namingStrategyDelegator) {
Configuration.this.setNamingStrategyDelegator( namingStrategyDelegator );
} }
public TypeResolver getTypeResolver() { public TypeResolver getTypeResolver() {
@ -3408,7 +3458,15 @@ public class Configuration implements Serializable {
} }
public NamingStrategy getNamingStrategy() { public NamingStrategy getNamingStrategy() {
return namingStrategy; if ( LegacyNamingStrategyDelegator.class.isInstance( namingStrategyDelegator ) ) {
( (LegacyNamingStrategyDelegator) namingStrategyDelegator ).getNamingStrategy();
}
return null;
}
@Override
protected NamingStrategyDelegator getNamingStrategyDelegator() {
return namingStrategyDelegator;
} }
} }

View File

@ -34,6 +34,7 @@ import org.hibernate.annotations.ColumnTransformers;
import org.hibernate.annotations.Index; import org.hibernate.annotations.Index;
import org.hibernate.annotations.common.reflection.XProperty; import org.hibernate.annotations.common.reflection.XProperty;
import org.hibernate.cfg.annotations.Nullability; import org.hibernate.cfg.annotations.Nullability;
import org.hibernate.cfg.naming.NamingStrategyDelegate;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.mapping.Column; import org.hibernate.mapping.Column;
@ -249,7 +250,7 @@ public class Ejb3Column {
if ( propertyName != null ) { if ( propertyName != null ) {
mappingColumn.setName( mappingColumn.setName(
mappings.getObjectNameNormalizer().normalizeIdentifierQuoting( mappings.getObjectNameNormalizer().normalizeIdentifierQuoting(
mappings.getNamingStrategy().propertyToColumnName( propertyName ) getNamingStrategyDelegate().determineAttributeColumnName( propertyName )
) )
); );
} }
@ -257,7 +258,7 @@ public class Ejb3Column {
} }
else { else {
columnName = mappings.getObjectNameNormalizer().normalizeIdentifierQuoting( columnName ); columnName = mappings.getObjectNameNormalizer().normalizeIdentifierQuoting( columnName );
columnName = mappings.getNamingStrategy().columnName( columnName ); columnName = getNamingStrategyDelegate().toPhysicalColumnName( columnName );
columnName = mappings.getObjectNameNormalizer().normalizeIdentifierQuoting( columnName ); columnName = mappings.getObjectNameNormalizer().normalizeIdentifierQuoting( columnName );
mappingColumn.setName( columnName ); mappingColumn.setName( columnName );
} }
@ -324,11 +325,18 @@ public class Ejb3Column {
} }
protected void addColumnBinding(SimpleValue value) { protected void addColumnBinding(SimpleValue value) {
String logicalColumnName = mappings.getNamingStrategy() String logicalColumnName = getNamingStrategyDelegate().logicalColumnName( this.logicalColumnName, propertyName );
.logicalColumnName( this.logicalColumnName, propertyName );
mappings.addColumnBinding( logicalColumnName, getMappingColumn(), value.getTable() ); mappings.addColumnBinding( logicalColumnName, getMappingColumn(), value.getTable() );
} }
protected NamingStrategyDelegate getNamingStrategyDelegate() {
return getNamingStrategyDelegate( mappings );
}
protected static NamingStrategyDelegate getNamingStrategyDelegate(Mappings mappings) {
return mappings.getNamingStrategyDelegator().getNamingStrategyDelegate( false );
}
/** /**
* Find appropriate table of the column. * Find appropriate table of the column.
* It can come from a secondary table or from the main table of the persistent class * It can come from a secondary table or from the main table of the persistent class
@ -446,9 +454,12 @@ public class Ejb3Column {
final String sqlType = col.columnDefinition().equals( "" ) final String sqlType = col.columnDefinition().equals( "" )
? null ? null
: nameNormalizer.normalizeIdentifierQuoting( col.columnDefinition() ); : nameNormalizer.normalizeIdentifierQuoting( col.columnDefinition() );
final String tableName = ! StringHelper.isEmpty(col.table()) final String tableName =
? nameNormalizer.normalizeIdentifierQuoting( mappings.getNamingStrategy().tableName( col.table() ) ) ! StringHelper.isEmpty(col.table())
: ""; ? nameNormalizer.normalizeIdentifierQuoting( getNamingStrategyDelegate( mappings ).toPhysicalTableName(
col.table()
) )
: "";
final String columnName = nameNormalizer.normalizeIdentifierQuoting( col.name() ); final String columnName = nameNormalizer.normalizeIdentifierQuoting( col.name() );
Ejb3Column column = new Ejb3Column(); Ejb3Column column = new Ejb3Column();
column.setImplicit( false ); column.setImplicit( false );

View File

@ -63,6 +63,7 @@ public class Ejb3JoinColumn extends Ejb3Column {
//table name on the mapped by side if any //table name on the mapped by side if any
private String mappedByTableName; private String mappedByTableName;
private String mappedByEntityName; private String mappedByEntityName;
private String mappedByJpaEntityName;
private boolean JPA2ElementCollection; private boolean JPA2ElementCollection;
public void setJPA2ElementCollection(boolean JPA2ElementCollection) { public void setJPA2ElementCollection(boolean JPA2ElementCollection) {
@ -309,7 +310,10 @@ public class Ejb3JoinColumn extends Ejb3Column {
setReferencedColumn( annJoin.referencedColumnName() ); setReferencedColumn( annJoin.referencedColumnName() );
final String tableName = !BinderHelper.isEmptyAnnotationValue( annJoin.table() ) final String tableName = !BinderHelper.isEmptyAnnotationValue( annJoin.table() )
? nameNormalizer.normalizeIdentifierQuoting( getMappings().getNamingStrategy().tableName( annJoin.table() ) ) : ""; ? nameNormalizer.normalizeIdentifierQuoting( getNamingStrategyDelegate().toPhysicalTableName(
annJoin.table()
) )
: "";
setSecondaryTableName( tableName ); setSecondaryTableName( tableName );
} }
} }
@ -450,14 +454,24 @@ public class Ejb3JoinColumn extends Ejb3Column {
if ( mappedBySide ) { if ( mappedBySide ) {
String unquotedMappedbyTable = StringHelper.unquote( mappedByTableName ); String unquotedMappedbyTable = StringHelper.unquote( mappedByTableName );
final String ownerObjectName = JPA2ElementCollection && mappedByEntityName != null ? if ( JPA2ElementCollection ) {
StringHelper.unqualify( mappedByEntityName ) : unquotedMappedbyTable; columnName = getNamingStrategyDelegate().determineElementCollectionForeignKeyColumnName(
columnName = getMappings().getNamingStrategy().foreignKeyColumnName( mappedByPropertyName,
mappedByPropertyName, mappedByEntityName,
mappedByEntityName, mappedByJpaEntityName,
ownerObjectName, unquotedMappedbyTable,
unquotedLogicalReferenceColumn unquotedLogicalReferenceColumn
); );
}
else {
columnName = getNamingStrategyDelegate().determineEntityAssociationForeignKeyColumnName(
mappedByPropertyName,
mappedByEntityName,
mappedByJpaEntityName,
unquotedMappedbyTable,
unquotedLogicalReferenceColumn
);
}
//one element was quoted so we quote //one element was quoted so we quote
if ( isRefColumnQuoted || StringHelper.isQuoted( mappedByTableName ) ) { if ( isRefColumnQuoted || StringHelper.isQuoted( mappedByTableName ) ) {
columnName = StringHelper.quote( columnName ); columnName = StringHelper.quote( columnName );
@ -466,9 +480,10 @@ public class Ejb3JoinColumn extends Ejb3Column {
else if ( ownerSide ) { else if ( ownerSide ) {
String logicalTableName = getMappings().getLogicalTableName( referencedEntity.getTable() ); String logicalTableName = getMappings().getLogicalTableName( referencedEntity.getTable() );
String unquotedLogicalTableName = StringHelper.unquote( logicalTableName ); String unquotedLogicalTableName = StringHelper.unquote( logicalTableName );
columnName = getMappings().getNamingStrategy().foreignKeyColumnName( columnName = getNamingStrategyDelegate().determineEntityAssociationForeignKeyColumnName(
getPropertyName(), getPropertyName(),
referencedEntity.getEntityName(), referencedEntity.getEntityName(),
referencedEntity.getJpaEntityName(),
unquotedLogicalTableName, unquotedLogicalTableName,
unquotedLogicalReferenceColumn unquotedLogicalReferenceColumn
); );
@ -481,7 +496,7 @@ public class Ejb3JoinColumn extends Ejb3Column {
//is an intra-entity hierarchy table join so copy the name by default //is an intra-entity hierarchy table join so copy the name by default
String logicalTableName = getMappings().getLogicalTableName( referencedEntity.getTable() ); String logicalTableName = getMappings().getLogicalTableName( referencedEntity.getTable() );
String unquotedLogicalTableName = StringHelper.unquote( logicalTableName ); String unquotedLogicalTableName = StringHelper.unquote( logicalTableName );
columnName = getMappings().getNamingStrategy().joinKeyColumnName( columnName = getNamingStrategyDelegate().determineJoinKeyColumnName(
unquotedLogicalReferenceColumn, unquotedLogicalReferenceColumn,
unquotedLogicalTableName unquotedLogicalTableName
); );
@ -523,8 +538,11 @@ public class Ejb3JoinColumn extends Ejb3Column {
final String referencedColumn = nameNormalizer.normalizeIdentifierQuoting( getReferencedColumn() ); final String referencedColumn = nameNormalizer.normalizeIdentifierQuoting( getReferencedColumn() );
final String unquotedLogColName = StringHelper.unquote( logicalColumnName ); final String unquotedLogColName = StringHelper.unquote( logicalColumnName );
final String unquotedRefColumn = StringHelper.unquote( referencedColumn ); final String unquotedRefColumn = StringHelper.unquote( referencedColumn );
String logicalCollectionColumnName = getMappings().getNamingStrategy() String logicalCollectionColumnName = getNamingStrategyDelegate().logicalCollectionColumnName(
.logicalCollectionColumnName( unquotedLogColName, getPropertyName(), unquotedRefColumn ); unquotedLogColName,
getPropertyName(),
unquotedRefColumn
);
if ( isLogicalColumnQuoted ) { if ( isLogicalColumnQuoted ) {
logicalCollectionColumnName = StringHelper.quote( logicalCollectionColumnName ); logicalCollectionColumnName = StringHelper.quote( logicalCollectionColumnName );
@ -639,7 +657,7 @@ public class Ejb3JoinColumn extends Ejb3Column {
if ( StringHelper.isNotEmpty( columnName ) ) { if ( StringHelper.isNotEmpty( columnName ) ) {
getMappingColumn().setName( getMappingColumn().setName(
applyNamingStrategy ? applyNamingStrategy ?
getMappings().getNamingStrategy().columnName( columnName ) : getNamingStrategyDelegate().toPhysicalColumnName( columnName ) :
columnName columnName
); );
} }
@ -694,8 +712,9 @@ public class Ejb3JoinColumn extends Ejb3Column {
return joinColumns; return joinColumns;
} }
public void setMappedBy(String entityName, String logicalTableName, String mappedByProperty) { public void setMappedBy(String entityName, String jpaEntityName, String logicalTableName, String mappedByProperty) {
this.mappedByEntityName = entityName; this.mappedByEntityName = entityName;
this.mappedByJpaEntityName = jpaEntityName;
this.mappedByTableName = logicalTableName; this.mappedByTableName = logicalTableName;
this.mappedByPropertyName = mappedByProperty; this.mappedByPropertyName = mappedByProperty;
} }

View File

@ -24,7 +24,6 @@
package org.hibernate.cfg; package org.hibernate.cfg;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
@ -41,6 +40,7 @@ import org.hibernate.EntityMode;
import org.hibernate.FetchMode; import org.hibernate.FetchMode;
import org.hibernate.FlushMode; import org.hibernate.FlushMode;
import org.hibernate.MappingException; import org.hibernate.MappingException;
import org.hibernate.cfg.naming.NamingStrategyDelegate;
import org.hibernate.engine.internal.Versioning; import org.hibernate.engine.internal.Versioning;
import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle; import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle;
import org.hibernate.engine.spi.FilterDefinition; import org.hibernate.engine.spi.FilterDefinition;
@ -883,16 +883,23 @@ public final class HbmBinder {
String physicalTableName; String physicalTableName;
if ( tableNameNode == null ) { if ( tableNameNode == null ) {
logicalTableName = StringHelper.unqualify( model.getEntityName() ); logicalTableName = StringHelper.unqualify( model.getEntityName() );
physicalTableName = mappings.getNamingStrategy().classToTableName( model.getEntityName() ); physicalTableName = getNamingStrategyDelegate( mappings ).determinePrimaryTableLogicalName(
model.getEntityName(),
model.getJpaEntityName()
);
} }
else { else {
logicalTableName = tableNameNode.getValue(); logicalTableName = tableNameNode.getValue();
physicalTableName = mappings.getNamingStrategy().tableName( logicalTableName ); physicalTableName = getNamingStrategyDelegate( mappings ).toPhysicalTableName( logicalTableName );
} }
mappings.addTableBinding( schema, catalog, logicalTableName, physicalTableName, denormalizedSuperTable ); mappings.addTableBinding( schema, catalog, logicalTableName, physicalTableName, denormalizedSuperTable );
return physicalTableName; return physicalTableName;
} }
private static NamingStrategyDelegate getNamingStrategyDelegate(Mappings mappings) {
return mappings.getNamingStrategyDelegator().getNamingStrategyDelegate( true );
}
public static void bindJoinedSubclass(Element node, JoinedSubclass joinedSubclass, public static void bindJoinedSubclass(Element node, JoinedSubclass joinedSubclass,
Mappings mappings, java.util.Map inheritedMetas) throws MappingException { Mappings mappings, java.util.Map inheritedMetas) throws MappingException {
@ -1068,11 +1075,10 @@ public final class HbmBinder {
column.setTypeIndex( count++ ); column.setTypeIndex( count++ );
bindColumn( columnElement, column, isNullable ); bindColumn( columnElement, column, isNullable );
final String columnName = columnElement.attributeValue( "name" ); final String columnName = columnElement.attributeValue( "name" );
String logicalColumnName = mappings.getNamingStrategy().logicalColumnName( String logicalColumnName = getNamingStrategyDelegate( mappings ).logicalColumnName(
columnName, propertyPath columnName, propertyPath
); );
column.setName( mappings.getNamingStrategy().columnName( column.setName( getNamingStrategyDelegate( mappings ).toPhysicalColumnName( columnName ) );
columnName ) );
if ( table != null ) { if ( table != null ) {
table.addColumn( column ); // table=null -> an association table.addColumn( column ); // table=null -> an association
// - fill it in later // - fill it in later
@ -1123,10 +1129,10 @@ public final class HbmBinder {
( (ManyToOne) simpleValue ).markAsLogicalOneToOne(); ( (ManyToOne) simpleValue ).markAsLogicalOneToOne();
} }
final String columnName = columnAttribute.getValue(); final String columnName = columnAttribute.getValue();
String logicalColumnName = mappings.getNamingStrategy().logicalColumnName( String logicalColumnName = getNamingStrategyDelegate( mappings ).logicalColumnName(
columnName, propertyPath columnName, propertyPath
); );
column.setName( mappings.getNamingStrategy().columnName( columnName ) ); column.setName( getNamingStrategyDelegate( mappings ).toPhysicalColumnName( columnName ) );
if ( table != null ) { if ( table != null ) {
table.addColumn( column ); // table=null -> an association - fill table.addColumn( column ); // table=null -> an association - fill
// it in later // it in later
@ -1142,8 +1148,8 @@ public final class HbmBinder {
Column column = new Column(); Column column = new Column();
column.setValue( simpleValue ); column.setValue( simpleValue );
bindColumn( node, column, isNullable ); bindColumn( node, column, isNullable );
column.setName( mappings.getNamingStrategy().propertyToColumnName( propertyPath ) ); column.setName( getNamingStrategyDelegate( mappings ).determineAttributeColumnName( propertyPath ) );
String logicalName = mappings.getNamingStrategy().logicalColumnName( null, propertyPath ); String logicalName = getNamingStrategyDelegate( mappings ).logicalColumnName( null, propertyPath );
mappings.addColumnBinding( logicalName, column, table ); mappings.addColumnBinding( logicalName, column, table );
/* TODO: joinKeyColumnName & foreignKeyColumnName should be called either here or at a /* TODO: joinKeyColumnName & foreignKeyColumnName should be called either here or at a
* slightly higer level in the stack (to get all the information we need) * slightly higer level in the stack (to get all the information we need)
@ -1467,7 +1473,7 @@ public final class HbmBinder {
Attribute tableNode = node.attribute( "table" ); Attribute tableNode = node.attribute( "table" );
String tableName; String tableName;
if ( tableNode != null ) { if ( tableNode != null ) {
tableName = mappings.getNamingStrategy().tableName( tableNode.getValue() ); tableName = getNamingStrategyDelegate( mappings ).toPhysicalTableName( tableNode.getValue() );
} }
else { else {
//tableName = mappings.getNamingStrategy().propertyToTableName( className, path ); //tableName = mappings.getNamingStrategy().propertyToTableName( className, path );
@ -1475,13 +1481,25 @@ public final class HbmBinder {
//TODO mappings.getLogicalTableName(ownerTable) //TODO mappings.getLogicalTableName(ownerTable)
String logicalOwnerTableName = ownerTable.getName(); String logicalOwnerTableName = ownerTable.getName();
//FIXME we don't have the associated entity table name here, has to be done in a second pass //FIXME we don't have the associated entity table name here, has to be done in a second pass
tableName = mappings.getNamingStrategy().collectionTableName( if ( node.element( "element" ) != null || node.element( "composite-element" ) != null ) {
collection.getOwner().getEntityName(), tableName = getNamingStrategyDelegate( mappings ).determineElementCollectionTableLogicalName(
logicalOwnerTableName , collection.getOwner().getClassName(),
null, collection.getOwner().getEntityName(),
null, logicalOwnerTableName,
path path
); );
}
else {
tableName = getNamingStrategyDelegate( mappings ).determineEntityAssociationJoinTableLogicalName(
collection.getOwner().getEntityName(),
collection.getOwner().getJpaEntityName(),
logicalOwnerTableName,
null,
null,
null,
path
);
}
if ( ownerTable.isQuoted() ) { if ( ownerTable.isQuoted() ) {
tableName = StringHelper.quote( tableName ); tableName = StringHelper.quote( tableName );
} }

View File

@ -36,6 +36,7 @@ import org.hibernate.MappingException;
import org.hibernate.annotations.AnyMetaDef; import org.hibernate.annotations.AnyMetaDef;
import org.hibernate.annotations.common.reflection.ReflectionManager; import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.annotations.common.reflection.XClass; import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.engine.ResultSetMappingDefinition; import org.hibernate.engine.ResultSetMappingDefinition;
import org.hibernate.engine.spi.FilterDefinition; import org.hibernate.engine.spi.FilterDefinition;
import org.hibernate.engine.spi.NamedQueryDefinition; import org.hibernate.engine.spi.NamedQueryDefinition;
@ -79,16 +80,43 @@ public interface Mappings {
* Get the current naming strategy. * Get the current naming strategy.
* *
* @return The current naming strategy. * @return The current naming strategy.
*
* @deprecated Use {@link #getNamingStrategyDelegator()} instead.
*/ */
@Deprecated
public NamingStrategy getNamingStrategy(); public NamingStrategy getNamingStrategy();
/** /**
* Set the current naming strategy. * Set the current naming strategy. An instance of {@link org.hibernate.cfg.naming.LegacyNamingStrategyDelegator}
* will be constructed with the specified naming strategy.
* *
* @param namingStrategy The naming strategy to use. * @param namingStrategy the {@link NamingStrategy} to set; must be non-null.
*
* @deprecated Use {@link #setNamingStrategyDelegator(org.hibernate.cfg.naming.NamingStrategyDelegator)} instead.
* @see org.hibernate.cfg.naming.LegacyNamingStrategyDelegator#LegacyNamingStrategyDelegator(NamingStrategy)
*/ */
@Deprecated
public void setNamingStrategy(NamingStrategy namingStrategy); public void setNamingStrategy(NamingStrategy namingStrategy);
/**
* Get the current naming strategy delegate.
*
* @return The current naming strategy delegate.
*/
public NamingStrategyDelegator getNamingStrategyDelegator();
/**
* Set a current naming strategy delegator.
*
* @param namingStrategyDelegator the {@link org.hibernate.cfg.naming.NamingStrategyDelegator} to set;
* must be non-null; if {@code namingStrategyDelegator} is an instance
* of {@link org.hibernate.cfg.naming.LegacyNamingStrategyDelegator}, then
* {@link org.hibernate.cfg.naming.LegacyNamingStrategyDelegator#getNamingStrategy()}
* must be non-null.
*/
public void setNamingStrategyDelegator(NamingStrategyDelegator namingStrategyDelegator);
/** /**
* Returns the currently bound default schema name. * Returns the currently bound default schema name.
* *

View File

@ -37,6 +37,8 @@ package org.hibernate.cfg;
* @see ImprovedNamingStrategy * @see ImprovedNamingStrategy
* @author Gavin King * @author Gavin King
* @author Emmanuel Bernard * @author Emmanuel Bernard
*
* @deprecated
*/ */
public interface NamingStrategy { public interface NamingStrategy {
/** /**

View File

@ -23,6 +23,7 @@
*/ */
package org.hibernate.cfg; package org.hibernate.cfg;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
/** /**
@ -42,7 +43,10 @@ public abstract class ObjectNameNormalizer {
* @param strategy The naming strategy in effect * @param strategy The naming strategy in effect
* *
* @return The implicit name * @return The implicit name
*
* * @deprecated Replaced by {@link #determineImplicitName(org.hibernate.cfg.naming.NamingStrategyDelegator)}.
*/ */
@Deprecated
public String determineImplicitName(NamingStrategy strategy); public String determineImplicitName(NamingStrategy strategy);
/** /**
@ -52,8 +56,30 @@ public abstract class ObjectNameNormalizer {
* @param name The {@link ObjectNameNormalizer#normalizeIdentifierQuoting normalized} explicit object name. * @param name The {@link ObjectNameNormalizer#normalizeIdentifierQuoting normalized} explicit object name.
* *
* @return The strategy-handled name. * @return The strategy-handled name.
*
* @deprecated Replaced by {@link #determineImplicitName(org.hibernate.cfg.naming.NamingStrategyDelegator)}.
*/ */
@Deprecated
public String handleExplicitName(NamingStrategy strategy, String name); public String handleExplicitName(NamingStrategy strategy, String name);
/**
* Called when the user supplied no explicit name/identifier for the given database object.
*
* @param strategyDelegator The naming strategy delegator in effect
*
* @return The implicit name
*/
public String determineImplicitName(NamingStrategyDelegator strategyDelegator);
/**
* Called when the user has supplied an explicit name for the database object.
*
* @param strategyDelegator The naming strategy delegator in effect
* @param name The {@link ObjectNameNormalizer#normalizeIdentifierQuoting normalized} explicit object name.
*
* @return The strategy-handled name.
*/
public String handleExplicitName(NamingStrategyDelegator strategyDelegator, String name);
} }
/** /**
@ -70,14 +96,14 @@ public abstract class ObjectNameNormalizer {
if ( StringHelper.isEmpty( explicitName ) ) { if ( StringHelper.isEmpty( explicitName ) ) {
// No explicit name given, so allow the naming strategy the chance // No explicit name given, so allow the naming strategy the chance
// to determine it based on the corresponding mapped java name // to determine it based on the corresponding mapped java name
objectName = helper.determineImplicitName( getNamingStrategy() ); objectName = helper.determineImplicitName( getNamingStrategyDelegator() );
} }
else { else {
// An explicit name was given: // An explicit name was given:
// in some cases we allow the naming strategy to "fine tune" these, but first // in some cases we allow the naming strategy to "fine tune" these, but first
// handle any quoting for consistent handling in naming strategies // handle any quoting for consistent handling in naming strategies
objectName = normalizeIdentifierQuoting( explicitName ); objectName = normalizeIdentifierQuoting( explicitName );
objectName = helper.handleExplicitName( getNamingStrategy(), objectName ); objectName = helper.handleExplicitName( getNamingStrategyDelegator(), objectName );
return normalizeIdentifierQuoting( objectName ); return normalizeIdentifierQuoting( objectName );
} }
// Conceivable that the naming strategy could return a quoted identifier, or // Conceivable that the naming strategy could return a quoted identifier, or
@ -134,6 +160,16 @@ public abstract class ObjectNameNormalizer {
* Get the current {@link NamingStrategy}. * Get the current {@link NamingStrategy}.
* *
* @return The current {@link NamingStrategy}. * @return The current {@link NamingStrategy}.
*
* @deprecated Replaced by {@link #getNamingStrategyDelegator()}
*/ */
@Deprecated
protected abstract NamingStrategy getNamingStrategy(); protected abstract NamingStrategy getNamingStrategy();
/**
* Get the current {@link org.hibernate.cfg.naming.NamingStrategyDelegator}.
*
* @return The current {@link org.hibernate.cfg.naming.NamingStrategyDelegator}.
*/
protected abstract NamingStrategyDelegator getNamingStrategyDelegator();
} }

View File

@ -1131,7 +1131,9 @@ public abstract class CollectionBinder {
); );
Table ownerTable = collValue.getOwner().getTable(); Table ownerTable = collValue.getOwner().getTable();
column.setMappedBy( column.setMappedBy(
collValue.getOwner().getEntityName(), mappings.getLogicalTableName( ownerTable ), collValue.getOwner().getEntityName(),
collValue.getOwner().getJpaEntityName(),
mappings.getLogicalTableName( ownerTable ),
mappedByProperty mappedByProperty
); );
// String header = ( mappedByProperty == null ) ? mappings.getLogicalTableName( ownerTable ) : mappedByProperty; // String header = ( mappedByProperty == null ) ? mappings.getLogicalTableName( ownerTable ) : mappedByProperty;
@ -1141,8 +1143,10 @@ public abstract class CollectionBinder {
//default value //default value
associationTableBinder.setDefaultName( associationTableBinder.setDefaultName(
collValue.getOwner().getEntityName(), collValue.getOwner().getEntityName(),
collValue.getOwner().getJpaEntityName(),
mappings.getLogicalTableName( collValue.getOwner().getTable() ), mappings.getLogicalTableName( collValue.getOwner().getTable() ),
collectionEntity != null ? collectionEntity.getEntityName() : null, collectionEntity != null ? collectionEntity.getEntityName() : null,
collectionEntity != null ? collectionEntity.getJpaEntityName() : null,
collectionEntity != null ? mappings.getLogicalTableName( collectionEntity.getTable() ) : null, collectionEntity != null ? mappings.getLogicalTableName( collectionEntity.getTable() ) : null,
joinColumns[0].getPropertyName() joinColumns[0].getPropertyName()
); );

View File

@ -84,6 +84,8 @@ import org.hibernate.cfg.ObjectNameNormalizer;
import org.hibernate.cfg.ObjectNameSource; import org.hibernate.cfg.ObjectNameSource;
import org.hibernate.cfg.PropertyHolder; import org.hibernate.cfg.PropertyHolder;
import org.hibernate.cfg.UniqueConstraintHolder; import org.hibernate.cfg.UniqueConstraintHolder;
import org.hibernate.cfg.naming.NamingStrategyDelegate;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.engine.internal.Versioning; import org.hibernate.engine.internal.Versioning;
import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle; import org.hibernate.engine.spi.ExecuteUpdateResultCheckStyle;
import org.hibernate.engine.spi.FilterDefinition; import org.hibernate.engine.spi.FilterDefinition;
@ -520,18 +522,39 @@ public class EntityBinder {
private static class EntityTableNamingStrategyHelper implements ObjectNameNormalizer.NamingStrategyHelper { private static class EntityTableNamingStrategyHelper implements ObjectNameNormalizer.NamingStrategyHelper {
private final String entityName; private final String entityName;
private final String jpaEntityName;
private EntityTableNamingStrategyHelper(String entityName) { private EntityTableNamingStrategyHelper(String entityName, String jpaEntityName) {
this.entityName = entityName; this.entityName = entityName;
this.jpaEntityName = jpaEntityName;
} }
@Override
public String determineImplicitName(NamingStrategy strategy) { public String determineImplicitName(NamingStrategy strategy) {
return strategy.classToTableName( entityName ); return strategy.classToTableName( entityName );
} }
@Override
public String handleExplicitName(NamingStrategy strategy, String name) { public String handleExplicitName(NamingStrategy strategy, String name) {
return strategy.tableName( name ); return strategy.tableName( name );
} }
@Override
public String determineImplicitName(NamingStrategyDelegator strategyDelegator) {
return getNamingStrategyDelegate( strategyDelegator ).determinePrimaryTableLogicalName(
entityName,
jpaEntityName
);
}
@Override
public String handleExplicitName(NamingStrategyDelegator strategyDelegator, String name) {
return getNamingStrategyDelegate( strategyDelegator ).toPhysicalTableName( name );
}
}
private static NamingStrategyDelegate getNamingStrategyDelegate(NamingStrategyDelegator strategyDelegator) {
return strategyDelegator.getNamingStrategyDelegate( false );
} }
public void bindTable( public void bindTable(
@ -542,7 +565,10 @@ public class EntityBinder {
String constraints, String constraints,
Table denormalizedSuperclassTable) { Table denormalizedSuperclassTable) {
EntityTableObjectNameSource tableNameContext = new EntityTableObjectNameSource( tableName, name ); EntityTableObjectNameSource tableNameContext = new EntityTableObjectNameSource( tableName, name );
EntityTableNamingStrategyHelper namingStrategyHelper = new EntityTableNamingStrategyHelper( name ); EntityTableNamingStrategyHelper namingStrategyHelper = new EntityTableNamingStrategyHelper(
persistentClass.getEntityName(),
name
);
final Table table = TableBinder.buildAndFillTable( final Table table = TableBinder.buildAndFillTable(
schema, schema,
catalog, catalog,
@ -736,6 +762,17 @@ public class EntityBinder {
public String handleExplicitName(NamingStrategy strategy, String name) { public String handleExplicitName(NamingStrategy strategy, String name) {
return strategy.tableName( name ); return strategy.tableName( name );
} }
@Override
public String determineImplicitName(NamingStrategyDelegator strategyDelegator) {
// todo : throw an error?
return null;
}
@Override
public String handleExplicitName(NamingStrategyDelegator strategyDelegator, String name) {
return getNamingStrategyDelegate( strategyDelegator ).toPhysicalTableName( name );
}
} }
private static SecondaryTableNamingStrategyHelper SEC_TBL_NS_HELPER = new SecondaryTableNamingStrategyHelper(); private static SecondaryTableNamingStrategyHelper SEC_TBL_NS_HELPER = new SecondaryTableNamingStrategyHelper();

View File

@ -41,6 +41,8 @@ import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.ObjectNameNormalizer; import org.hibernate.cfg.ObjectNameNormalizer;
import org.hibernate.cfg.ObjectNameSource; import org.hibernate.cfg.ObjectNameSource;
import org.hibernate.cfg.UniqueConstraintHolder; import org.hibernate.cfg.UniqueConstraintHolder;
import org.hibernate.cfg.naming.NamingStrategyDelegate;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.collections.CollectionHelper; import org.hibernate.internal.util.collections.CollectionHelper;
@ -78,7 +80,9 @@ public class TableBinder {
private String associatedEntityTable; private String associatedEntityTable;
private String propertyName; private String propertyName;
private String ownerEntity; private String ownerEntity;
private String ownerJpaEntity;
private String associatedEntity; private String associatedEntity;
private String associatedJpaEntity;
private boolean isJPA2ElementCollection; private boolean isJPA2ElementCollection;
public void setSchema(String schema) { public void setSchema(String schema) {
@ -146,33 +150,56 @@ public class TableBinder {
final String unquotedAssocTable = StringHelper.unquote( associatedEntityTable ); final String unquotedAssocTable = StringHelper.unquote( associatedEntityTable );
//@ElementCollection use ownerEntity_property instead of the cleaner ownerTableName_property //@ElementCollection use ownerEntity_property instead of the cleaner ownerTableName_property
// ownerEntity can be null when the table name is explicitly set // ownerEntity can be null when the table name is explicitly set; <== gb: doesn't seem to be true...
final String ownerObjectName = isJPA2ElementCollection && ownerEntity != null ? final String ownerObjectName = isJPA2ElementCollection && ownerEntity != null ?
StringHelper.unqualify( ownerEntity ) : unquotedOwnerTable; StringHelper.unqualify( ownerEntity ) : unquotedOwnerTable;
final ObjectNameSource nameSource = buildNameContext( final ObjectNameSource nameSource = buildNameContext(
ownerObjectName, ownerObjectName,
unquotedAssocTable ); unquotedAssocTable
);
final boolean ownerEntityTableQuoted = StringHelper.isQuoted( ownerEntityTable ); final boolean ownerEntityTableQuoted = StringHelper.isQuoted( ownerEntityTable );
final boolean associatedEntityTableQuoted = StringHelper.isQuoted( associatedEntityTable ); final boolean associatedEntityTableQuoted = StringHelper.isQuoted( associatedEntityTable );
final ObjectNameNormalizer.NamingStrategyHelper namingStrategyHelper = new ObjectNameNormalizer.NamingStrategyHelper() { final ObjectNameNormalizer.NamingStrategyHelper namingStrategyHelper = new ObjectNameNormalizer.NamingStrategyHelper() {
public String determineImplicitName(NamingStrategy strategy) { public String determineImplicitName(NamingStrategy strategy) {
throw new AssertionFailure( "method call should have been replaced by #determineImplicitName(NamingStrategyDelegate strategyDelegate)" );
}
final String strategyResult = strategy.collectionTableName( public String handleExplicitName(NamingStrategy strategy, String name) {
ownerEntity, return strategy.tableName( name );
ownerObjectName, }
associatedEntity,
unquotedAssocTable,
propertyName
); @Override
public String determineImplicitName(NamingStrategyDelegator strategyDelegator) {
final NamingStrategyDelegate strategyDelegate = getNamingStrategyDelegate( strategyDelegator );
final String strategyResult;
if ( isJPA2ElementCollection ) {
strategyResult = strategyDelegate.determineElementCollectionTableLogicalName(
ownerEntity,
ownerJpaEntity,
unquotedOwnerTable,
propertyName
);
}
else {
strategyResult = strategyDelegate.determineEntityAssociationJoinTableLogicalName(
ownerEntity,
ownerJpaEntity,
unquotedOwnerTable,
associatedEntity,
associatedJpaEntity,
unquotedAssocTable,
propertyName
);
}
return ownerEntityTableQuoted || associatedEntityTableQuoted return ownerEntityTableQuoted || associatedEntityTableQuoted
? StringHelper.quote( strategyResult ) ? StringHelper.quote( strategyResult )
: strategyResult; : strategyResult;
} }
public String handleExplicitName(NamingStrategy strategy, String name) { @Override
return strategy.tableName( name ); public String handleExplicitName(NamingStrategyDelegator strategyDelegator, String name) {
return getNamingStrategyDelegate( strategyDelegator ).toPhysicalTableName( name );
} }
}; };
@ -190,13 +217,34 @@ public class TableBinder {
); );
} }
private ObjectNameSource buildNameContext(String unquotedOwnerTable, String unquotedAssocTable) { private ObjectNameSource buildNameContext(
String logicalName = mappings.getNamingStrategy().logicalCollectionTableName( String unquotedOwnerTable,
name, String unquotedAssocTable) {
unquotedOwnerTable, final NamingStrategyDelegate strategyDelegate = getNamingStrategyDelegate(
unquotedAssocTable, mappings.getNamingStrategyDelegator()
propertyName
); );
String logicalName;
if ( isJPA2ElementCollection ) {
logicalName = strategyDelegate.logicalElementCollectionTableName(
name,
ownerEntity,
ownerJpaEntity,
unquotedOwnerTable,
propertyName
);
}
else {
logicalName = strategyDelegate.logicalEntityAssociationJoinTableName(
name,
ownerEntity,
ownerJpaEntity,
unquotedOwnerTable,
associatedEntity,
associatedJpaEntity,
unquotedAssocTable,
propertyName
);
}
if ( StringHelper.isQuoted( ownerEntityTable ) || StringHelper.isQuoted( associatedEntityTable ) ) { if ( StringHelper.isQuoted( ownerEntityTable ) || StringHelper.isQuoted( associatedEntityTable ) ) {
logicalName = StringHelper.quote( logicalName ); logicalName = StringHelper.quote( logicalName );
} }
@ -204,6 +252,11 @@ public class TableBinder {
return new AssociationTableNameSource( name, logicalName ); return new AssociationTableNameSource( name, logicalName );
} }
private NamingStrategyDelegate getNamingStrategyDelegate(
NamingStrategyDelegator strategyDelegator) {
return strategyDelegator.getNamingStrategyDelegate( false );
}
public static Table buildAndFillTable( public static Table buildAndFillTable(
String schema, String schema,
String catalog, String catalog,
@ -556,12 +609,19 @@ public class TableBinder {
} }
public void setDefaultName( public void setDefaultName(
String ownerEntity, String ownerEntityTable, String associatedEntity, String associatedEntityTable, String ownerEntity,
String ownerJpaEntity,
String ownerEntityTable,
String associatedEntity,
String associatedJpaEntity,
String associatedEntityTable,
String propertyName String propertyName
) { ) {
this.ownerEntity = ownerEntity; this.ownerEntity = ownerEntity;
this.ownerJpaEntity = ownerJpaEntity;
this.ownerEntityTable = ownerEntityTable; this.ownerEntityTable = ownerEntityTable;
this.associatedEntity = associatedEntity; this.associatedEntity = associatedEntity;
this.associatedJpaEntity = associatedJpaEntity;
this.associatedEntityTable = associatedEntityTable; this.associatedEntityTable = associatedEntityTable;
this.propertyName = propertyName; this.propertyName = propertyName;
this.name = null; this.name = null;

View File

@ -0,0 +1,79 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat, Inc. and/or its affiliates 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.cfg.naming;
import java.io.Serializable;
import org.hibernate.cfg.NamingStrategy;
/**
* @author Gail Badner
*/
public abstract class AbstractLegacyNamingStrategyDelegate implements NamingStrategyDelegate, Serializable {
private final LegacyNamingStrategyDelegate.LegacyNamingStrategyDelegateContext context;
public AbstractLegacyNamingStrategyDelegate(LegacyNamingStrategyDelegate.LegacyNamingStrategyDelegateContext context) {
this.context = context;
}
protected NamingStrategy getNamingStrategy() {
return context.getNamingStrategy();
}
@Override
public String determinePrimaryTableLogicalName(String entityName, String jpaEntityName) {
// jpaEntity name is being passed here in order to not cause a regression. See HHH-4312.
return getNamingStrategy().classToTableName( jpaEntityName );
}
@Override
public String toPhysicalTableName(String tableName) {
return getNamingStrategy().tableName( tableName );
}
@Override
public String toPhysicalColumnName(String columnName) {
return getNamingStrategy().columnName( columnName );
}
@Override
public String determineAttributeColumnName(String propertyName) {
return getNamingStrategy().propertyToColumnName( propertyName );
}
@Override
public String determineJoinKeyColumnName(String joinedColumn, String joinedTable) {
return getNamingStrategy().joinKeyColumnName( joinedColumn, joinedTable );
}
@Override
public String logicalColumnName(String columnName, String propertyName) {
return getNamingStrategy().logicalColumnName( columnName, propertyName );
}
@Override
public String logicalCollectionColumnName(String columnName, String propertyName, String referencedColumn) {
return getNamingStrategy().logicalCollectionColumnName( columnName, propertyName, referencedColumn );
}
}

View File

@ -0,0 +1,66 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat, Inc. and/or its affiliates 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.cfg.naming;
import java.io.Serializable;
import org.hibernate.internal.util.StringHelper;
/**
* @author Gail Badner
*/
public abstract class AbstractNamingStrategyDelegate implements NamingStrategyDelegate, Serializable {
@Override
public String toPhysicalTableName(String tableName) {
return tableName;
}
@Override
public String toPhysicalColumnName(String columnName) {
return columnName;
}
@Override
public String determineAttributeColumnName(String propertyName) {
return StringHelper.unqualify( propertyName );
}
@Override
public String determineJoinKeyColumnName(String joinedColumn, String joinedTable) {
return toPhysicalColumnName( joinedColumn );
}
@Override
public String logicalColumnName(String columnName, String propertyName) {
return StringHelper.isNotEmpty( columnName ) ? columnName : StringHelper.unqualify( propertyName );
}
@Override
public String logicalCollectionColumnName(String columnName, String propertyName, String referencedColumn) {
return StringHelper.isNotEmpty( columnName ) ?
columnName :
StringHelper.unqualify( propertyName ) + "_" + referencedColumn;
}
}

View File

@ -0,0 +1,48 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat, Inc. and/or its affiliates 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.cfg.naming;
import java.io.Serializable;
/**
* @author Gail Badner
*/
public class DefaultNamingStrategyDelegator implements NamingStrategyDelegator, Serializable {
public static final DefaultNamingStrategyDelegator INSTANCE = new DefaultNamingStrategyDelegator();
private final NamingStrategyDelegate hbmNamingStrategyDelegate;
private final NamingStrategyDelegate jpaNamingStrategyDelegate;
private DefaultNamingStrategyDelegator() {
this.hbmNamingStrategyDelegate = new HbmNamingStrategyDelegate();
this.jpaNamingStrategyDelegate = new JpaNamingStrategyDelegate();
}
@Override
public NamingStrategyDelegate getNamingStrategyDelegate(boolean isHbm) {
return isHbm ?
hbmNamingStrategyDelegate :
jpaNamingStrategyDelegate;
}
}

View File

@ -0,0 +1,123 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat, Inc. and/or its affiliates 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.cfg.naming;
import org.hibernate.internal.util.StringHelper;
/**
* @author Gail Badner
*/
public class HbmNamingStrategyDelegate extends AbstractNamingStrategyDelegate {
@Override
public String determinePrimaryTableLogicalName(String entityName, String jpaEntityName) {
return StringHelper.unqualify( entityName );
}
@Override
public String determineElementCollectionTableLogicalName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String propertyNamePath) {
return ownerEntityTable
+ '_'
+ StringHelper.unqualify( propertyNamePath );
}
@Override
public String determineElementCollectionForeignKeyColumnName(String propertyName, String propertyEntityName, String propertyJpaEntityName, String propertyTableName, String referencedColumnName) {
throw new UnsupportedOperationException( "Method not supported for Hibernate-specific mappings" );
}
@Override
public String determineEntityAssociationJoinTableLogicalName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String associatedEntityName,
String associatedJpaEntityName,
String associatedEntityTable,
String propertyNamePath) {
return ownerEntityTable
+ '_'
+ StringHelper.unqualify( propertyNamePath );
}
@Override
public String determineEntityAssociationForeignKeyColumnName(
String propertyName,
String propertyEntityName,
String propertyJpaEntityName,
String propertyTableName,
String referencedColumnName) {
throw new UnsupportedOperationException( "Method not supported for Hibernate-specific mappings" );
}
@Override
public String logicalElementCollectionTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String propertyName) {
if ( tableName != null ) {
return tableName;
}
else {
return determineElementCollectionTableLogicalName(
ownerEntityName,
ownerJpaEntityName,
ownerEntityTable,
propertyName
);
}
}
@Override
public String logicalEntityAssociationJoinTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String associatedEntityName,
String associatedJpaEntityName,
String associatedEntityTable,
String propertyName) {
if ( tableName != null ) {
return tableName;
}
else {
return determineEntityAssociationJoinTableLogicalName(
ownerEntityName,
ownerJpaEntityName,
ownerEntityTable,
associatedEntityName,
associatedJpaEntityName,
associatedEntityTable,
propertyName
);
}
}
}

View File

@ -0,0 +1,183 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat, Inc. and/or its affiliates 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.cfg.naming;
import org.hibernate.AssertionFailure;
import org.hibernate.internal.util.StringHelper;
/**
* @author Gail Badner
*/
public class JpaNamingStrategyDelegate extends AbstractNamingStrategyDelegate {
@Override
public String determinePrimaryTableLogicalName(String entityName, String jpaEntityName) {
return StringHelper.unqualify( determineEntityNameToUse( entityName, jpaEntityName ) );
}
@Override
public String determineElementCollectionTableLogicalName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String propertyNamePath) {
// JPA states we should use the following as default:
// "The concatenation of the name of the containing entity and the name of the
// collection attribute, separated by an underscore.
// aka:
// if owning entity has a JPA entity name: {OWNER JPA ENTITY NAME}_{COLLECTION ATTRIBUTE NAME}
// otherwise: {OWNER ENTITY NAME}_{COLLECTION ATTRIBUTE NAME}
return determineEntityNameToUse( ownerEntityName, ownerJpaEntityName )
+ '_'
+ StringHelper.unqualify( propertyNamePath );
}
@Override
public String determineElementCollectionForeignKeyColumnName(
String propertyName,
String propertyEntityName,
String propertyJpaEntityName,
String propertyTableName,
String referencedColumnName) {
// JPA states we should use the following as default:
// "The concatenation of the following: the name of the entity; "_"; the name of the
// referenced primary key column"
return determineEntityNameToUse( propertyEntityName, propertyJpaEntityName )
+ '_'
+ referencedColumnName;
}
@Override
public String determineEntityAssociationJoinTableLogicalName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String associatedEntityName,
String associatedJpaEntityName,
String associatedEntityTable,
String propertyNamePath) {
// JPA states we should use the following as default:
// "The concatenated names of the two associated primary entity tables (owning side
// first), separated by an underscore."
// aka:
// {OWNING SIDE PRIMARY TABLE NAME}_{NON-OWNING SIDE PRIMARY TABLE NAME}
return ownerEntityTable
+ '_'
+ associatedEntityTable;
}
@Override
public String determineEntityAssociationForeignKeyColumnName(
String referencingPropertyName,
String propertyEntityName,
String propertyJpaEntityName,
String propertyTableName,
String referencedColumnName) {
// JPA states we should use the following as default:
// "The concatenation of the following: the name of the referencing relationship
// property or field of the referencing entity or embeddable class; "_"; the name
// of the referenced primary key column. If there is no such referencing relationship
// property or field in the entity, or if the join is for an element collection, the
// join column name is formed as the concatenation of the following: the name of the
// entity; "_"; the name of the referenced primary key column
// The part referring to an entity collection can be disregarded here since, determination of
// an element collection foreign key column name is covered by #entityAssociationJoinTableName().
//
// For a unidirectional association:
// {PROPERTY_ENTITY_NAME}_{REFERENCED_COLUMN_NAME}
// For a bidirectional association:
// {REFERENCING_PROPERTY_NAME}_{REFERENCED_COLUMN_NAME}
final String header;
if ( referencingPropertyName == null ) {
// This is a unidirectional association.
header = determineEntityNameToUse( propertyEntityName, propertyJpaEntityName );
}
else {
// This is a bidirectional association.
header = StringHelper.unqualify( referencingPropertyName );
}
if ( header == null ) {
throw new AssertionFailure( "propertyJpaEntityName and referencingPropertyName cannot both be empty." );
}
return toPhysicalColumnName( header + "_" + referencedColumnName );
}
@Override
public String logicalElementCollectionTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String propertyName) {
if ( tableName != null ) {
return tableName;
}
else {
return determineElementCollectionTableLogicalName(
ownerEntityName,
ownerJpaEntityName,
ownerEntityTable,
propertyName
);
}
}
@Override
public String logicalEntityAssociationJoinTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String associatedEntityName,
String associatedJpaEntityName,
String associatedEntityTable,
String propertyName) {
if ( tableName != null ) {
return tableName;
}
else {
return determineEntityAssociationJoinTableLogicalName(
ownerEntityName,
ownerJpaEntityName,
ownerEntityTable,
associatedEntityName,
associatedJpaEntityName,
associatedEntityTable,
propertyName
);
}
}
private String determineEntityNameToUse(String entityName, String jpaEntityName) {
if ( StringHelper.isNotEmpty( jpaEntityName ) ) {
// prefer the JPA entity name, if specified...
return jpaEntityName;
}
else {
// otherwise, use the Hibernate entity name
return StringHelper.unqualifyEntityName( entityName );
}
}
}

View File

@ -0,0 +1,125 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat, Inc. and/or its affiliates 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.cfg.naming;
/**
* @author Gail Badner
*/
public class LegacyHbmNamingStrategyDelegate extends AbstractLegacyNamingStrategyDelegate {
public LegacyHbmNamingStrategyDelegate(LegacyNamingStrategyDelegate.LegacyNamingStrategyDelegateContext context) {
super( context );
}
@Override
public String determineElementCollectionTableLogicalName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String propertyNamePath) {
return getNamingStrategy().collectionTableName(
ownerEntityName,
ownerEntityTable,
null,
null,
propertyNamePath
);
}
@Override
public String determineElementCollectionForeignKeyColumnName(String propertyName, String propertyEntityName, String propertyJpaEntityName, String propertyTableName, String referencedColumnName) {
return getNamingStrategy().foreignKeyColumnName(
propertyName,
propertyEntityName,
propertyTableName,
referencedColumnName
);
}
@Override
public String determineEntityAssociationJoinTableLogicalName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String associatedEntityName,
String associatedJpaEntityName,
String associatedEntityTable,
String propertyNamePath) {
return getNamingStrategy().collectionTableName(
ownerEntityName,
ownerEntityTable,
associatedEntityName,
associatedEntityTable,
propertyNamePath
);
}
@Override
public String determineEntityAssociationForeignKeyColumnName(
String propertyName,
String propertyEntityName,
String propertyJpaEntityName,
String propertyTableName,
String referencedColumnName) {
return getNamingStrategy().foreignKeyColumnName(
propertyName,
propertyEntityName,
propertyTableName,
referencedColumnName
);
}
@Override
public String logicalElementCollectionTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String propertyName) {
return getNamingStrategy().logicalCollectionTableName(
tableName,
ownerEntityTable,
null,
propertyName
);
}
@Override
public String logicalEntityAssociationJoinTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String associatedEntityName,
String associatedJpaEntityName,
String associatedEntityTable,
String propertyName) {
return getNamingStrategy().logicalCollectionTableName(
tableName,
ownerEntityTable,
associatedEntityTable,
propertyName
);
}
}

View File

@ -0,0 +1,35 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat, Inc. and/or its affiliates 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.cfg.naming;
import org.hibernate.cfg.NamingStrategy;
/**
* @author Gail Badner
*/
public interface LegacyNamingStrategyDelegate extends NamingStrategyDelegate {
public static interface LegacyNamingStrategyDelegateContext {
public NamingStrategy getNamingStrategy();
}
}

View File

@ -0,0 +1,67 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat, Inc. and/or its affiliates 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.cfg.naming;
import java.io.Serializable;
import org.hibernate.cfg.EJB3NamingStrategy;
import org.hibernate.cfg.NamingStrategy;
import static org.hibernate.cfg.naming.LegacyNamingStrategyDelegate.LegacyNamingStrategyDelegateContext;
/**
*
* @deprecated Needed as a transitory implementation until the deprecated NamingStrategy contract
* can be removed.
*
* @author Gail Badner
*/
@Deprecated
public class LegacyNamingStrategyDelegator
implements NamingStrategyDelegator, LegacyNamingStrategyDelegateContext, Serializable {
private final NamingStrategy namingStrategy;
private final NamingStrategyDelegate hbmNamingStrategyDelegate;
private final NamingStrategyDelegate jpaNamingStrategyDelegate;
public LegacyNamingStrategyDelegator() {
this( EJB3NamingStrategy.INSTANCE );
}
public LegacyNamingStrategyDelegator(NamingStrategy namingStrategy) {
this.namingStrategy = namingStrategy;
this.hbmNamingStrategyDelegate = new LegacyHbmNamingStrategyDelegate( this );
this.jpaNamingStrategyDelegate = new LegacyStandardNamingStrategyDelegate( this );
}
public NamingStrategy getNamingStrategy() {
return namingStrategy;
}
@Override
public NamingStrategyDelegate getNamingStrategyDelegate(boolean isHbm) {
return isHbm ?
hbmNamingStrategyDelegate :
jpaNamingStrategyDelegate;
}
}

View File

@ -0,0 +1,132 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat, Inc. and/or its affiliates 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.cfg.naming;
import org.hibernate.internal.util.StringHelper;
/**
* @author Gail Badner
*/
public class LegacyStandardNamingStrategyDelegate extends AbstractLegacyNamingStrategyDelegate {
LegacyStandardNamingStrategyDelegate(LegacyNamingStrategyDelegate.LegacyNamingStrategyDelegateContext context) {
super( context );
}
@Override
public String determineElementCollectionTableLogicalName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String propertyNamePath) {
return getNamingStrategy().collectionTableName(
ownerEntityName,
StringHelper.unqualifyEntityName( ownerEntityName ),
null,
null,
propertyNamePath
);
}
@Override
public String determineElementCollectionForeignKeyColumnName(
String propertyName,
String propertyEntityName,
String propertyJpaEntityName,
String propertyTableName,
String referencedColumnName) {
return getNamingStrategy().foreignKeyColumnName(
propertyName,
propertyEntityName,
StringHelper.unqualifyEntityName( propertyEntityName ),
referencedColumnName
);
}
@Override
public String determineEntityAssociationJoinTableLogicalName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String associatedEntityName,
String associatedJpaEntityName,
String associatedEntityTable,
String propertyNamePath) {
return getNamingStrategy().collectionTableName(
ownerEntityName,
ownerEntityTable,
associatedEntityName,
associatedEntityTable,
propertyNamePath
);
}
@Override
public String determineEntityAssociationForeignKeyColumnName(
String propertyName,
String propertyEntityName,
String propertyJpaEntityName,
String propertyTableName,
String referencedColumnName) {
return getNamingStrategy().foreignKeyColumnName(
propertyName,
propertyEntityName,
propertyTableName,
referencedColumnName
);
}
@Override
public String logicalElementCollectionTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String propertyName) {
return getNamingStrategy().logicalCollectionTableName(
tableName,
ownerEntityName == null ? null : StringHelper.unqualifyEntityName( ownerEntityName ),
null,
propertyName
);
}
@Override
public String logicalEntityAssociationJoinTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String associatedEntityName,
String associatedJpaEntityName,
String associatedEntityTable,
String propertyName) {
return getNamingStrategy().logicalCollectionTableName(
tableName,
ownerEntityTable,
associatedEntityTable,
propertyName
);
}
}

View File

@ -0,0 +1,102 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat, Inc. and/or its affiliates 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.cfg.naming;
/**
* @author Gail Badner
*/
public interface NamingStrategyDelegate {
public String determinePrimaryTableLogicalName(String entityName, String jpaEntityName);
public String determineAttributeColumnName(String propertyName);
/**
* Alter the table name given in the mapping document
* @param tableName a table name
* @return a table name
*/
public String toPhysicalTableName(String tableName);
/**
* Alter the column name given in the mapping document
* @param columnName a column name
* @return a column name
*/
public String toPhysicalColumnName(String columnName);
public String determineElementCollectionTableLogicalName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String propertyName);
public String determineElementCollectionForeignKeyColumnName(
String propertyName,
String propertyEntityName,
String propertyJpaEntityName,
String propertyTableName,
String referencedColumnName);
public String determineEntityAssociationJoinTableLogicalName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String associatedEntityName,
String associatedJpaEntityName,
String associatedEntityTable,
String propertyName);
public String determineEntityAssociationForeignKeyColumnName(
String propertyName,
String propertyEntityName,
String propertyJpaEntityName,
String propertyTableName,
String referencedColumnName);
public String determineJoinKeyColumnName(String joinedColumn, String joinedTable);
public String logicalColumnName(String columnName, String propertyName);
public String logicalElementCollectionTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String propertyName);
public String logicalEntityAssociationJoinTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String associatedEntityName,
String associatedJpaEntityName,
String associatedEntityTable,
String propertyName);
public String logicalCollectionColumnName(String columnName, String propertyName, String referencedColumn);
}

View File

@ -0,0 +1,44 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat, Inc. and/or its affiliates 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.cfg.naming;
import org.hibernate.cfg.NamingStrategy;
/**
* Provides access to the appropriate {@link NamingStrategyDelegate}.
*
* @author Gail Badner
*/
public interface NamingStrategyDelegator {
/**
* Returns the appropriate {@link NamingStrategyDelegate}.
*
* @param isHbm - true, if {@link NamingStrategyDelegate} is to be used for a
* hibernate-specific (hbm.xml) mapping; false, otherwise.
*
* @return the appropriate {@link NamingStrategyDelegate}
*/
public NamingStrategyDelegate getNamingStrategyDelegate(boolean isHbm);
}

View File

@ -29,6 +29,7 @@ import java.util.Properties;
import org.hibernate.cfg.AvailableSettings; import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.NamingStrategy; import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.ObjectNameNormalizer; import org.hibernate.cfg.ObjectNameNormalizer;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.id.PersistentIdentifierGenerator; import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.metamodel.binding.EntityBinding; import org.hibernate.metamodel.binding.EntityBinding;
import org.hibernate.metamodel.source.MetadataImplementor; import org.hibernate.metamodel.source.MetadataImplementor;
@ -93,5 +94,10 @@ public class IdentifierGeneratorResolver {
protected NamingStrategy getNamingStrategy() { protected NamingStrategy getNamingStrategy() {
return namingStrategy; return namingStrategy;
} }
@Override
protected NamingStrategyDelegator getNamingStrategyDelegator() {
return null;
}
} }
} }

View File

@ -37,6 +37,7 @@ import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.cfg.NamingStrategy; import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.ObjectNameNormalizer; import org.hibernate.cfg.ObjectNameNormalizer;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.H2Dialect;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -85,6 +86,11 @@ public class SequenceHiLoGeneratorNoIncrementTest extends BaseUnitTestCase {
protected NamingStrategy getNamingStrategy() { protected NamingStrategy getNamingStrategy() {
return cfg.getNamingStrategy(); return cfg.getNamingStrategy();
} }
@Override
protected NamingStrategyDelegator getNamingStrategyDelegator() {
return cfg.getNamingStrategyDelegator();
}
} }
); );

View File

@ -37,6 +37,7 @@ import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.cfg.NamingStrategy; import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.ObjectNameNormalizer; import org.hibernate.cfg.ObjectNameNormalizer;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.H2Dialect; import org.hibernate.dialect.H2Dialect;
import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.engine.spi.SessionFactoryImplementor;
@ -82,6 +83,12 @@ public class SequenceHiLoGeneratorTest extends BaseUnitTestCase {
protected NamingStrategy getNamingStrategy() { protected NamingStrategy getNamingStrategy() {
return cfg.getNamingStrategy(); return cfg.getNamingStrategy();
} }
@Override
protected NamingStrategyDelegator getNamingStrategyDelegator() {
return cfg.getNamingStrategyDelegator();
}
} ); } );
Dialect dialect = new H2Dialect(); Dialect dialect = new H2Dialect();

View File

@ -31,6 +31,7 @@ import org.hibernate.MappingException;
import org.hibernate.cfg.Environment; import org.hibernate.cfg.Environment;
import org.hibernate.cfg.NamingStrategy; import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.ObjectNameNormalizer; import org.hibernate.cfg.ObjectNameNormalizer;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.id.PersistentIdentifierGenerator; import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;
@ -79,6 +80,11 @@ public class SequenceStyleConfigUnitTest extends BaseUnitTestCase {
protected NamingStrategy getNamingStrategy() { protected NamingStrategy getNamingStrategy() {
return null; return null;
} }
@Override
protected NamingStrategyDelegator getNamingStrategyDelegator() {
return null;
}
} }
); );
return props; return props;

View File

@ -38,7 +38,6 @@ import org.hibernate.mapping.Collection;
import org.hibernate.mapping.Column; import org.hibernate.mapping.Column;
import org.hibernate.mapping.ForeignKey; import org.hibernate.mapping.ForeignKey;
import org.hibernate.test.annotations.Country; import org.hibernate.test.annotations.Country;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
@ -297,7 +296,6 @@ public class CollectionElementTest extends BaseCoreFunctionalTestCase {
@Test @Test
@TestForIssue( jiraKey = "HHH-9387") @TestForIssue( jiraKey = "HHH-9387")
@FailureExpected( jiraKey = "HHH-9387")
public void testDefaultTableNameOwnerEntityNameAndPKColumnOverride() { public void testDefaultTableNameOwnerEntityNameAndPKColumnOverride() {
// NOTE: expected JPA entity names are explicit here (rather than just getting them from the PersistentClass) // NOTE: expected JPA entity names are explicit here (rather than just getting them from the PersistentClass)
// to ensure that entity names/tables are not changed (which would invalidate these test cases). // to ensure that entity names/tables are not changed (which would invalidate these test cases).
@ -308,7 +306,6 @@ public class CollectionElementTest extends BaseCoreFunctionalTestCase {
@Test @Test
@TestForIssue( jiraKey = "HHH-9387") @TestForIssue( jiraKey = "HHH-9387")
@FailureExpected( jiraKey = "HHH-9387")
public void testDefaultTableNameOwnerPrimaryTableAndEntityNamesOverride() { public void testDefaultTableNameOwnerPrimaryTableAndEntityNamesOverride() {
// NOTE: expected JPA entity names are explicit here (rather than just getting them from the PersistentClass) // NOTE: expected JPA entity names are explicit here (rather than just getting them from the PersistentClass)
// to ensure that entity names/tables are not changed (which would invalidate these test cases). // to ensure that entity names/tables are not changed (which would invalidate these test cases).
@ -318,7 +315,7 @@ public class CollectionElementTest extends BaseCoreFunctionalTestCase {
checkDefaultCollectionTableName( Owner.class, "elements", "OWNER_elements" ); checkDefaultCollectionTableName( Owner.class, "elements", "OWNER_elements" );
} }
private void checkDefaultCollectionTableName( protected void checkDefaultCollectionTableName(
Class<?> ownerEntityClass, Class<?> ownerEntityClass,
String ownerCollectionPropertyName, String ownerCollectionPropertyName,
String expectedCollectionTableName) { String expectedCollectionTableName) {
@ -351,7 +348,6 @@ public class CollectionElementTest extends BaseCoreFunctionalTestCase {
@Test @Test
@TestForIssue( jiraKey = "HHH-9389") @TestForIssue( jiraKey = "HHH-9389")
@FailureExpected( jiraKey = "HHH-9389")
public void testDefaultJoinColumnOwnerEntityNameAndPKColumnOverride() { public void testDefaultJoinColumnOwnerEntityNameAndPKColumnOverride() {
// NOTE: expected JPA entity names are explicit here (rather than just getting them from the PersistentClass) // NOTE: expected JPA entity names are explicit here (rather than just getting them from the PersistentClass)
// to ensure that entity names/tables are not changed (which would invalidate these test cases). // to ensure that entity names/tables are not changed (which would invalidate these test cases).
@ -362,7 +358,6 @@ public class CollectionElementTest extends BaseCoreFunctionalTestCase {
@Test @Test
@TestForIssue( jiraKey = "HHH-9389") @TestForIssue( jiraKey = "HHH-9389")
@FailureExpected( jiraKey = "HHH-9389")
public void testDefaultJoinColumnOwnerPrimaryTableAndEntityNamesOverride() { public void testDefaultJoinColumnOwnerPrimaryTableAndEntityNamesOverride() {
// NOTE: expected JPA entity names are explicit here (rather than just getting them from the PersistentClass) // NOTE: expected JPA entity names are explicit here (rather than just getting them from the PersistentClass)
// to ensure that entity names/tables are not changed (which would invalidate these test cases). // to ensure that entity names/tables are not changed (which would invalidate these test cases).
@ -372,7 +367,7 @@ public class CollectionElementTest extends BaseCoreFunctionalTestCase {
checkDefaultJoinColumnName( Owner.class, "elements", "OWNER_id" ); checkDefaultJoinColumnName( Owner.class, "elements", "OWNER_id" );
} }
private void checkDefaultJoinColumnName( protected void checkDefaultJoinColumnName(
Class<?> ownerEntityClass, Class<?> ownerEntityClass,
String ownerCollectionPropertyName, String ownerCollectionPropertyName,
String ownerForeignKeyNameExpected) { String ownerForeignKeyNameExpected) {

View File

@ -0,0 +1,244 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, 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.test.annotations.collectionelement;
import org.junit.Test;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.EJB3NamingStrategy;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.naming.AbstractLegacyNamingStrategyDelegate;
import org.hibernate.cfg.naming.LegacyHbmNamingStrategyDelegate;
import org.hibernate.cfg.naming.LegacyNamingStrategyDelegate;
import org.hibernate.cfg.naming.LegacyNamingStrategyDelegator;
import org.hibernate.cfg.naming.NamingStrategyDelegate;
import org.hibernate.testing.TestForIssue;
/**
* @author Gail Badner
*/
public class CustomNamingCollectionElementTest extends CollectionElementTest {
@Override
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setNamingStrategyDelegator( new MyLegacyNamingStrategyDelegator() );
}
@Test
@TestForIssue( jiraKey = "HHH-9387")
public void testDefaultTableNameOwnerEntityNameAndPKColumnOverride() {
// NOTE: expected JPA entity names are explicit here (rather than just getting them from the PersistentClass)
// to ensure that entity names/tables are not changed (which would invalidate these test cases).
// Matrix has @Entity(name="Mtx"); entity table name defaults to "Mtx"; owner PK column is configured as "mId"
// MyNamingStrategyDelegator will use the owner primary table name (instead of JPA entity name) in generated collection table.
checkDefaultCollectionTableName( Matrix.class, "mvalues", "Mtx_mvalues" );
}
@Test
@TestForIssue( jiraKey = "HHH-9387")
public void testDefaultTableNameOwnerPrimaryTableAndEntityNamesOverride() {
// NOTE: expected JPA entity names are explicit here (rather than just getting them from the PersistentClass)
// to ensure that entity names/tables are not changed (which would invalidate these test cases).
// Owner has @Entity( name="OWNER") @Table( name="OWNER_TABLE")
// MyNamingStrategyDelegator will use owner primary table name (instead of JPA entity name) in generated collection table.
checkDefaultCollectionTableName( Owner.class, "elements", "OWNER_TABLE_elements" );
}
@Test
@TestForIssue( jiraKey = "HHH-9389")
public void testDefaultJoinColumnOwnerEntityNameAndPKColumnOverride() {
// NOTE: expected JPA entity names are explicit here (rather than just getting them from the PersistentClass)
// to ensure that entity names/tables are not changed (which would invalidate these test cases).
// Matrix has @Entity(name="Mtx"); entity table name defaults to "Mtx"; owner PK column is configured as "mId"
// MyNamingStrategyDelegator will use owner primary table name, which will default to the JPA entity name
// in generated join column.
checkDefaultJoinColumnName( Matrix.class, "mvalues", "Mtx_mId" );
}
@Test
@TestForIssue( jiraKey = "HHH-9389")
public void testDefaultJoinColumnOwnerPrimaryTableAndEntityNamesOverride() {
// NOTE: expected JPA entity names are explicit here (rather than just getting them from the PersistentClass)
// to ensure that entity names/tables are not changed (which would invalidate these test cases).
// Owner has @Entity( name="OWNER") @Table( name="OWNER_TABLE")
// MyNamingStrategyDelegator will use the table name (instead of JPA entity name) in generated join column.
checkDefaultJoinColumnName( Owner.class, "elements", "OWNER_TABLE_id" );
}
@Test
@TestForIssue( jiraKey = "HHH-9389")
public void testDefaultJoinColumnOwnerPrimaryTableOverride() {
// NOTE: expected JPA entity names are explicit here (rather than just getting them from the PersistentClass)
// to ensure that entity names/tables are not changed (which would invalidate these test cases).
// Boy has @Entity @Table(name="tbl_Boys")
// MyNamingStrategyDelegator will use the table name (instead of JPA entity name) in generated join column.
checkDefaultJoinColumnName( Boy.class, "hatedNames", "tbl_Boys_id" );
}
@Test
@TestForIssue( jiraKey = "HHH-9387")
public void testDefaultTableNameOwnerPrimaryTableOverride() {
// NOTE: expected JPA entity names are explicit here (rather than just getting them from the PersistentClass)
// to ensure that entity names/tables are not changed (which would invalidate these test cases).
// Boy has @Entity @Table(name="tbl_Boys")
// MyNamingStrategyDelegator will use the table name (instead of JPA entity name) in generated join column.
checkDefaultCollectionTableName( Boy.class, "hatedNames", "tbl_Boys_hatedNames" );
}
static class MyLegacyNamingStrategyDelegator extends LegacyNamingStrategyDelegator {
private final NamingStrategyDelegate hbmNamingStrategyDelegate = new LegacyHbmNamingStrategyDelegate( this );
private final NamingStrategyDelegate nonHbmNamingStrategyDelegate = new MyNonHbmNamingStrategyDelegator( this );
@Override
public NamingStrategyDelegate getNamingStrategyDelegate(boolean isHbm) {
return isHbm ? hbmNamingStrategyDelegate : nonHbmNamingStrategyDelegate;
}
@Override
public NamingStrategy getNamingStrategy() {
return EJB3NamingStrategy.INSTANCE;
}
private class MyNonHbmNamingStrategyDelegator extends AbstractLegacyNamingStrategyDelegate {
MyNonHbmNamingStrategyDelegator(LegacyNamingStrategyDelegate.LegacyNamingStrategyDelegateContext context) {
super( context );
}
@Override
public String toPhysicalTableName(String tableName) {
return getNamingStrategy().tableName( tableName );
}
@Override
public String toPhysicalColumnName(String columnName) {
return getNamingStrategy().columnName( columnName );
}
@Override
public String determineElementCollectionTableLogicalName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String propertyNamePath) {
return getNamingStrategy().collectionTableName(
ownerEntityName,
ownerEntityTable,
null,
null,
propertyNamePath
);
}
@Override
public String determineElementCollectionForeignKeyColumnName(
String propertyName,
String propertyEntityName,
String propertyJpaEntityName,
String propertyTableName,
String referencedColumnName) {
return getNamingStrategy().foreignKeyColumnName(
propertyName,
propertyEntityName,
propertyTableName,
referencedColumnName
);
}
@Override
public String determineEntityAssociationJoinTableLogicalName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String associatedEntityName,
String associatedJpaEntityName,
String associatedEntityTable,
String propertyNamePath) {
return getNamingStrategy().collectionTableName(
ownerEntityName,
ownerEntityTable,
associatedEntityName,
associatedEntityTable,
propertyNamePath
);
}
@Override
public String determineEntityAssociationForeignKeyColumnName(
String propertyName,
String propertyEntityName,
String propertyJpaEntityName,
String propertyTableName,
String referencedColumnName) {
return getNamingStrategy().foreignKeyColumnName(
propertyName,
propertyEntityName,
propertyTableName,
referencedColumnName
);
}
@Override
public String logicalElementCollectionTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String propertyName) {
return getNamingStrategy().logicalCollectionTableName(
tableName,
ownerEntityTable,
null,
propertyName
);
}
@Override
public String logicalEntityAssociationJoinTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String associatedEntityName,
String associatedJpaEntityName,
String associatedEntityTable,
String propertyName) {
return getNamingStrategy().logicalCollectionTableName(
tableName,
ownerEntityTable,
associatedEntityTable,
propertyName
);
}
}
}
}

View File

@ -0,0 +1,88 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, 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.test.annotations.collectionelement;
import org.junit.Test;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.EJB3NamingStrategy;
import org.hibernate.cfg.naming.LegacyNamingStrategyDelegator;
import org.hibernate.testing.TestForIssue;
/**
* @author Gail Badner
*/
public class LegacyNamingCollectionElementTest extends CollectionElementTest {
@Override
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setNamingStrategyDelegator( new LegacyNamingStrategyDelegator( EJB3NamingStrategy.INSTANCE ) );
}
@Test
@TestForIssue( jiraKey = "HHH-9387")
public void testDefaultTableNameOwnerEntityNameAndPKColumnOverride() {
// NOTE: expected JPA entity names are explicit here (rather than just getting them from the PersistentClass)
// to ensure that entity names/tables are not changed (which would invalidate these test cases).
// Matrix has @Entity(name="Mtx"); entity table name defaults to "Mtx"; owner PK column is configured as "mId"
// Legacy behavior used unqualified entity name (instead of JPA entity name) in generated collection table.
checkDefaultCollectionTableName( Matrix.class, "mvalues", "Matrix_mvalues" );
}
@Test
@TestForIssue( jiraKey = "HHH-9387")
public void testDefaultTableNameOwnerPrimaryTableAndEntityNamesOverride() {
// NOTE: expected JPA entity names are explicit here (rather than just getting them from the PersistentClass)
// to ensure that entity names/tables are not changed (which would invalidate these test cases).
// Owner has @Entity( name="OWNER") @Table( name="OWNER_TABLE")
// Legacy behavior used unqualified entity name (instead of JPA entity name) in generated collection table.
checkDefaultCollectionTableName( Owner.class, "elements", "Owner_elements" );
}
@Test
@TestForIssue( jiraKey = "HHH-9389")
public void testDefaultJoinColumnOwnerEntityNameAndPKColumnOverride() {
// NOTE: expected JPA entity names are explicit here (rather than just getting them from the PersistentClass)
// to ensure that entity names/tables are not changed (which would invalidate these test cases).
// Matrix has @Entity(name="Mtx"); entity table name defaults to "Mtx"; owner PK column is configured as "mId"
// Legacy behavior used unqualified entity name (instead of JPA entity name) in generated join column.
checkDefaultJoinColumnName( Matrix.class, "mvalues", "Matrix_mId" );
}
@Test
@TestForIssue( jiraKey = "HHH-9389")
public void testDefaultJoinColumnOwnerPrimaryTableAndEntityNamesOverride() {
// NOTE: expected JPA entity names are explicit here (rather than just getting them from the PersistentClass)
// to ensure that entity names/tables are not changed (which would invalidate these test cases).
// Owner has @Entity( name="OWNER") @Table( name="OWNER_TABLE")
// Legacy behavior used unqualified entity name (instead of JPA entity name) in generated join column.
checkDefaultJoinColumnName( Owner.class, "elements", "Owner_id" );
}
}

View File

@ -4,6 +4,8 @@ import java.util.Collection;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany; import javax.persistence.ManyToMany;
import javax.persistence.OrderBy; import javax.persistence.OrderBy;
import javax.persistence.Table; import javax.persistence.Table;
@ -34,6 +36,7 @@ public class Group {
} }
@ManyToMany(cascade = CascadeType.PERSIST) @ManyToMany(cascade = CascadeType.PERSIST)
@JoinTable(joinColumns = {@JoinColumn( name="groupId")})
@OrderBy("expirationDate") @OrderBy("expirationDate")
@Where(clause = "1=1") @Where(clause = "1=1")
@WhereJoinTable(clause = "2=2") @WhereJoinTable(clause = "2=2")

View File

@ -4,6 +4,8 @@ import java.util.Set;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany; import javax.persistence.ManyToMany;
import javax.persistence.OrderBy; import javax.persistence.OrderBy;
import javax.persistence.Table; import javax.persistence.Table;
@ -34,6 +36,7 @@ public class GroupWithSet {
} }
@ManyToMany(cascade = CascadeType.PERSIST) @ManyToMany(cascade = CascadeType.PERSIST)
@JoinTable(joinColumns = {@JoinColumn( name="groupId")})
@OrderBy("expirationDate") @OrderBy("expirationDate")
@Where(clause = "1=1") @Where(clause = "1=1")
@WhereJoinTable(clause = "2=2") @WhereJoinTable(clause = "2=2")

View File

@ -0,0 +1,84 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, 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.test.annotations.manytomany.defaults;
import org.junit.Test;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.EJB3NamingStrategy;
import org.hibernate.cfg.naming.LegacyNamingStrategyDelegator;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue;
/**
* @author Gail Badner
*/
public class LegacyManyToManyDefaultsTest extends ManyToManyDefaultsTest {
@Override
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setNamingStrategyDelegator( new LegacyNamingStrategyDelegator( EJB3NamingStrategy.INSTANCE ) );
}
@Test
@TestForIssue( jiraKey = "HHH-9390")
public void testUnidirOwnerPrimaryTableAssocEntityNamePKOverride() {
// City.stolenItems; associated entity: Item
// City has @Entity with no name configured and @Table(name = "tbl_city")
// Item has @Entity(name="ITEM") and no @Table
// PK column for City.id: id (default)
// PK column for Item: iId
// unidirectional
// legacy behavior would use the table name in the generated join column.
checkDefaultJoinTablAndJoinColumnNames(
City.class,
"stolenItems",
null,
"tbl_city_ITEM",
"tbl_city_id",
"stolenItems_iId"
);
}
@Test
@TestForIssue( jiraKey = "HHH-9390")
public void testUnidirOwnerEntityNamePrimaryTableOverride() {
// Category.clients: associated entity: KnownClient
// Category has @Entity(name="CATEGORY") @Table(name="CATEGORY_TAB")
// KnownClient has @Entity with no name configured and no @Table
// PK column for Category.id: id (default)
// PK column for KnownClient.id: id (default)
// unidirectional
// legacy behavior would use the table name in the generated join column.
checkDefaultJoinTablAndJoinColumnNames(
Category.class,
"clients",
null,
"CATEGORY_TAB_KnownClient",
"CATEGORY_TAB_id",
"clients_id"
);
}
}

View File

@ -30,7 +30,6 @@ import org.junit.Test;
import org.hibernate.mapping.Column; import org.hibernate.mapping.Column;
import org.hibernate.mapping.ForeignKey; import org.hibernate.mapping.ForeignKey;
import org.hibernate.mapping.PersistentClass; import org.hibernate.mapping.PersistentClass;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.type.EntityType; import org.hibernate.type.EntityType;
@ -161,7 +160,6 @@ public class ManyToManyDefaultsTest extends BaseCoreFunctionalTestCase {
@Test @Test
@TestForIssue( jiraKey = "HHH-9390") @TestForIssue( jiraKey = "HHH-9390")
@FailureExpected( jiraKey = "HHH-9390")
public void testUnidirOwnerPrimaryTableAssocEntityNamePKOverride() { public void testUnidirOwnerPrimaryTableAssocEntityNamePKOverride() {
// City.stolenItems; associated entity: Item // City.stolenItems; associated entity: Item
// City has @Entity with no name configured and @Table(name = "tbl_city") // City has @Entity with no name configured and @Table(name = "tbl_city")
@ -181,7 +179,6 @@ public class ManyToManyDefaultsTest extends BaseCoreFunctionalTestCase {
@Test @Test
@TestForIssue( jiraKey = "HHH-9390") @TestForIssue( jiraKey = "HHH-9390")
@FailureExpected( jiraKey = "HHH-9390")
public void testUnidirOwnerEntityNamePrimaryTableOverride() { public void testUnidirOwnerEntityNamePrimaryTableOverride() {
// Category.clients: associated entity: KnownClient // Category.clients: associated entity: KnownClient
// Category has @Entity(name="CATEGORY") @Table(name="CATEGORY_TAB") // Category has @Entity(name="CATEGORY") @Table(name="CATEGORY_TAB")
@ -200,7 +197,7 @@ public class ManyToManyDefaultsTest extends BaseCoreFunctionalTestCase {
); );
} }
private void checkDefaultJoinTablAndJoinColumnNames( protected void checkDefaultJoinTablAndJoinColumnNames(
Class<?> ownerEntityClass, Class<?> ownerEntityClass,
String ownerCollectionPropertyName, String ownerCollectionPropertyName,
String inverseCollectionPropertyName, String inverseCollectionPropertyName,

View File

@ -252,6 +252,11 @@ public class AvailableSettings {
*/ */
public static final String NAMING_STRATEGY = "hibernate.ejb.naming_strategy"; public static final String NAMING_STRATEGY = "hibernate.ejb.naming_strategy";
/**
* Naming strategy delegator class name, the class has to have a no-arg constructor that returns a non-null value for {@link }
*/
public static final String NAMING_STRATEGY_DELEGATOR = "hibernate.ejb.naming_strategy_delegator";
/** /**
* IdentifierGeneratorStrategyProvider class name, the class must have a no-arg constructor * IdentifierGeneratorStrategyProvider class name, the class must have a no-arg constructor
* @deprecated if possible wait of Hibernate 4.1 and theService registry (MutableIdentifierGeneratorStrategy service) * @deprecated if possible wait of Hibernate 4.1 and theService registry (MutableIdentifierGeneratorStrategy service)

View File

@ -72,6 +72,7 @@ import org.hibernate.cfg.Environment;
import org.hibernate.cfg.NamingStrategy; import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.annotations.reflection.XMLContext; import org.hibernate.cfg.annotations.reflection.XMLContext;
import org.hibernate.cfg.beanvalidation.BeanValidationIntegrator; import org.hibernate.cfg.beanvalidation.BeanValidationIntegrator;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.ejb.cfg.spi.IdentifierGeneratorStrategyProvider; import org.hibernate.ejb.cfg.spi.IdentifierGeneratorStrategyProvider;
import org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider; import org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider;
import org.hibernate.ejb.event.JpaIntegrator; import org.hibernate.ejb.event.JpaIntegrator;
@ -1062,6 +1063,7 @@ public class Ejb3Configuration implements Serializable, Referenceable {
Interceptor defaultInterceptor = DEFAULT_CONFIGURATION.getInterceptor(); Interceptor defaultInterceptor = DEFAULT_CONFIGURATION.getInterceptor();
NamingStrategy defaultNamingStrategy = DEFAULT_CONFIGURATION.getNamingStrategy(); NamingStrategy defaultNamingStrategy = DEFAULT_CONFIGURATION.getNamingStrategy();
NamingStrategyDelegator defaultNamingStrategyDelegator = DEFAULT_CONFIGURATION.getNamingStrategyDelegator();
Iterator propertyIt = preparedProperties.keySet().iterator(); Iterator propertyIt = preparedProperties.keySet().iterator();
while ( propertyIt.hasNext() ) { while ( propertyIt.hasNext() ) {
@ -1101,9 +1103,28 @@ public class Ejb3Configuration implements Serializable, Referenceable {
"naming strategy", "naming strategy",
NamingStrategy.class NamingStrategy.class
); );
final NamingStrategyDelegator namingStrategyDelegator = instantiateCustomClassFromConfiguration(
preparedProperties,
defaultNamingStrategyDelegator,
cfg.getNamingStrategyDelegator(),
AvailableSettings.NAMING_STRATEGY_DELEGATOR,
"naming strategy delegator",
NamingStrategyDelegator.class
);
if ( namingStrategy != null && namingStrategyDelegator != null ) {
throw new PersistenceException(
getExceptionHeader() + AvailableSettings.NAMING_STRATEGY + " and " + AvailableSettings.NAMING_STRATEGY_DELEGATOR +
" properties cannot be used together. To be valid, only one of these properties can be set."
);
}
if ( namingStrategy != null ) { if ( namingStrategy != null ) {
cfg.setNamingStrategy( namingStrategy ); cfg.setNamingStrategy( namingStrategy );
} }
else if ( namingStrategyDelegator != null ) {
cfg.setNamingStrategyDelegator( namingStrategyDelegator );
}
final SessionFactoryObserver observer = instantiateCustomClassFromConfiguration( final SessionFactoryObserver observer = instantiateCustomClassFromConfiguration(
preparedProperties, preparedProperties,

View File

@ -0,0 +1,54 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, 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.ejb.test;
import org.hibernate.cfg.naming.HbmNamingStrategyDelegate;
import org.hibernate.cfg.naming.JpaNamingStrategyDelegate;
import org.hibernate.cfg.naming.LegacyNamingStrategyDelegator;
import org.hibernate.cfg.naming.NamingStrategyDelegate;
/**
* @author Gail Badner
*/
public class MyLegacyNamingStrategyDelegator extends LegacyNamingStrategyDelegator {
private final NamingStrategyDelegate hbmNamingStrategyDelegate = new HbmNamingStrategyDelegate();
private final NamingStrategyDelegate nonHbmNamingStrategyDelegate = new MyNonHbmNamingStrategyDelegate();
public MyLegacyNamingStrategyDelegator() {
super( new MyNamingStrategy() );
}
@Override
public NamingStrategyDelegate getNamingStrategyDelegate(boolean isHbm) {
return isHbm ? hbmNamingStrategyDelegate :nonHbmNamingStrategyDelegate;
}
private class MyNonHbmNamingStrategyDelegate extends JpaNamingStrategyDelegate {
@Override
public String toPhysicalColumnName(String columnName) {
return super.toPhysicalColumnName( "c_" + columnName );
}
}
}

View File

@ -0,0 +1,50 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, 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.ejb.test;
import org.hibernate.cfg.naming.HbmNamingStrategyDelegate;
import org.hibernate.cfg.naming.JpaNamingStrategyDelegate;
import org.hibernate.cfg.naming.NamingStrategyDelegate;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
/**
* @author Gail Badner
*/
public class MyNamingStrategyDelegator implements NamingStrategyDelegator {
private final NamingStrategyDelegate hbmNamingStrategyDelegate = new HbmNamingStrategyDelegate();
private final NamingStrategyDelegate nonHbmNamingStrategyDelegate = new MyNonHbmNamingStrategyDelegate();
@Override
public NamingStrategyDelegate getNamingStrategyDelegate(boolean isHbm) {
return isHbm ? hbmNamingStrategyDelegate :nonHbmNamingStrategyDelegate;
}
private class MyNonHbmNamingStrategyDelegate extends JpaNamingStrategyDelegate {
@Override
public String toPhysicalColumnName(String columnName) {
return super.toPhysicalColumnName( "c_" + columnName );
}
}
}

View File

@ -0,0 +1,114 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat, Inc. and/or its affiliates 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.ejb.test.ejb3configuration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.PersistenceException;
import org.junit.Test;
import org.hibernate.cfg.naming.LegacyNamingStrategyDelegator;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.ejb.AvailableSettings;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.ejb.test.MyNamingStrategy;
import org.hibernate.ejb.test.MyNamingStrategyDelegator;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author Gail Badner
*/
public class NamingStrategyDelegatorConfigurationTest extends BaseUnitTestCase {
@Test
public void testNamingStrategyDelegatorFromProperty() {
// configure NamingStrategy
{
PersistenceUnitInfoAdapter adapter = new PersistenceUnitInfoAdapter();
Ejb3Configuration cfg = new Ejb3Configuration();
cfg.configure(
adapter,
Collections.singletonMap( AvailableSettings.NAMING_STRATEGY, MyNamingStrategy.class.getName() )
);
assertEquals(
MyNamingStrategy.class.getName(),
cfg.getProperties().get( AvailableSettings.NAMING_STRATEGY )
);
assertEquals( null, cfg.getProperties().get( AvailableSettings.NAMING_STRATEGY_DELEGATOR ) );
final NamingStrategyDelegator namingStrategyDelegator =
cfg.getHibernateConfiguration().getNamingStrategyDelegator();
assertTrue( LegacyNamingStrategyDelegator.class.isInstance( namingStrategyDelegator ) );
assertTrue(
MyNamingStrategy.class.isInstance(
( (LegacyNamingStrategyDelegator)namingStrategyDelegator ).getNamingStrategy()
)
);
}
// configure NamingStrategyDelegator
{
PersistenceUnitInfoAdapter adapter = new PersistenceUnitInfoAdapter();
Ejb3Configuration cfg = new Ejb3Configuration();
cfg.configure(
adapter,
Collections.singletonMap(
AvailableSettings.NAMING_STRATEGY_DELEGATOR,
MyNamingStrategyDelegator.class.getName()
)
);
assertEquals( null, cfg.getProperties().get( AvailableSettings.NAMING_STRATEGY ) );
assertEquals(
MyNamingStrategyDelegator.class.getName(),
cfg.getProperties().get( AvailableSettings.NAMING_STRATEGY_DELEGATOR )
);
final NamingStrategyDelegator namingStrategyDelegator =
cfg.getHibernateConfiguration().getNamingStrategyDelegator();
assertTrue( MyNamingStrategyDelegator.class.isInstance( namingStrategyDelegator ) );
}
// configure NamingStrategy and NamingStrategyDelegator
{
PersistenceUnitInfoAdapter adapter = new PersistenceUnitInfoAdapter();
Ejb3Configuration cfg = new Ejb3Configuration();
final Map<String,String> integrationArgs = new HashMap<String,String>();
integrationArgs.put( AvailableSettings.NAMING_STRATEGY, MyNamingStrategy.class.getName() );
integrationArgs.put( AvailableSettings.NAMING_STRATEGY_DELEGATOR, MyNamingStrategyDelegator.class.getName() );
try {
cfg.configure( adapter, integrationArgs );
fail( "Should have thrown a PersistenceException because setting both properties is not allowed." );
}
catch (PersistenceException ex) {
// expected
}
}
}
}