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

(cherry picked from commit e5a31caa09)

Conflicts:
	hibernate-core/src/main/java/org/hibernate/cfg/HbmBinder.java
	hibernate-entitymanager/src/test/java/org/hibernate/ejb/test/MyNamingStrategyDelegator.java
This commit is contained in:
Gail Badner 2014-10-08 03:13:38 -07:00
parent 1cba98022e
commit 52f2c3a002
27 changed files with 353 additions and 397 deletions

View File

@ -80,7 +80,6 @@ import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.cfg.annotations.NamedEntityGraphDefinition;
import org.hibernate.cfg.annotations.NamedProcedureCallDefinition;
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;
@ -352,7 +351,7 @@ public class Configuration implements Serializable {
mappedByResolver = new HashMap<String, String>();
propertyRefResolver = new HashMap<String, String>();
caches = new ArrayList<CacheHolder>();
namingStrategyDelegator = DefaultNamingStrategyDelegator.INSTANCE;
namingStrategyDelegator = LegacyNamingStrategyDelegator.DEFAULT_INSTANCE;
setEntityResolver( new EJB3DTDEntityResolver() );
anyMetaDefs = new HashMap<String, AnyMetaDef>();
propertiesAnnotatedWithMapsId = new HashMap<XClass, Map<String, PropertyData>>();

View File

@ -293,7 +293,7 @@ public class Ejb3Column {
if ( propertyName != null ) {
mappingColumn.setName(
mappings.getObjectNameNormalizer().normalizeIdentifierQuoting(
getNamingStrategyDelegate().determineAttributeColumnName( propertyName )
getNamingStrategyDelegate().determineImplicitPropertyColumnName( propertyName )
)
);
}
@ -368,7 +368,8 @@ public class Ejb3Column {
}
protected void addColumnBinding(SimpleValue value) {
String logicalColumnName = getNamingStrategyDelegate().logicalColumnName( this.logicalColumnName, propertyName );
String logicalColumnName =
getNamingStrategyDelegate().determineLogicalColumnName( this.logicalColumnName, propertyName );
mappings.addColumnBinding( logicalColumnName, getMappingColumn(), value.getTable() );
}

View File

@ -455,21 +455,21 @@ public class Ejb3JoinColumn extends Ejb3Column {
if ( mappedBySide ) {
String unquotedMappedbyTable = StringHelper.unquote( mappedByTableName );
if ( JPA2ElementCollection ) {
columnName = getNamingStrategyDelegate().determineElementCollectionForeignKeyColumnName(
mappedByPropertyName,
columnName = getNamingStrategyDelegate().determineImplicitElementCollectionJoinColumnName(
mappedByEntityName,
mappedByJpaEntityName,
unquotedMappedbyTable,
unquotedLogicalReferenceColumn
unquotedLogicalReferenceColumn,
mappedByPropertyName
);
}
else {
columnName = getNamingStrategyDelegate().determineEntityAssociationForeignKeyColumnName(
mappedByPropertyName,
columnName = getNamingStrategyDelegate().determineImplicitEntityAssociationJoinColumnName(
mappedByEntityName,
mappedByJpaEntityName,
unquotedMappedbyTable,
unquotedLogicalReferenceColumn
unquotedLogicalReferenceColumn,
mappedByPropertyName
);
}
//one element was quoted so we quote
@ -480,12 +480,12 @@ public class Ejb3JoinColumn extends Ejb3Column {
else if ( ownerSide ) {
String logicalTableName = getMappings().getLogicalTableName( referencedEntity.getTable() );
String unquotedLogicalTableName = StringHelper.unquote( logicalTableName );
columnName = getNamingStrategyDelegate().determineEntityAssociationForeignKeyColumnName(
getPropertyName(),
columnName = getNamingStrategyDelegate().determineImplicitEntityAssociationJoinColumnName(
referencedEntity.getEntityName(),
referencedEntity.getJpaEntityName(),
unquotedLogicalTableName,
unquotedLogicalReferenceColumn
unquotedLogicalReferenceColumn,
getPropertyName()
);
//one element was quoted so we quote
if ( isRefColumnQuoted || StringHelper.isQuoted( logicalTableName ) ) {
@ -496,7 +496,7 @@ public class Ejb3JoinColumn extends Ejb3Column {
//is an intra-entity hierarchy table join so copy the name by default
String logicalTableName = getMappings().getLogicalTableName( referencedEntity.getTable() );
String unquotedLogicalTableName = StringHelper.unquote( logicalTableName );
columnName = getNamingStrategyDelegate().determineJoinKeyColumnName(
columnName = getNamingStrategyDelegate().toPhysicalJoinKeyColumnName(
unquotedLogicalReferenceColumn,
unquotedLogicalTableName
);
@ -538,7 +538,7 @@ public class Ejb3JoinColumn extends Ejb3Column {
final String referencedColumn = nameNormalizer.normalizeIdentifierQuoting( getReferencedColumn() );
final String unquotedLogColName = StringHelper.unquote( logicalColumnName );
final String unquotedRefColumn = StringHelper.unquote( referencedColumn );
String logicalCollectionColumnName = getNamingStrategyDelegate().logicalCollectionColumnName(
String logicalCollectionColumnName = getNamingStrategyDelegate().determineLogicalCollectionColumnName(
unquotedLogColName,
getPropertyName(),
unquotedRefColumn

View File

@ -889,7 +889,7 @@ public final class HbmBinder {
String physicalTableName;
if ( tableNameNode == null ) {
logicalTableName = StringHelper.unqualify( model.getEntityName() );
physicalTableName = getNamingStrategyDelegate( mappings ).determinePrimaryTableLogicalName(
physicalTableName = getNamingStrategyDelegate( mappings ).determineImplicitPrimaryTableName(
model.getEntityName(),
model.getJpaEntityName()
);
@ -1081,7 +1081,7 @@ public final class HbmBinder {
column.setTypeIndex( count++ );
bindColumn( columnElement, column, isNullable );
String columnName = columnElement.attributeValue( "name" );
String logicalColumnName = getNamingStrategyDelegate( mappings ).logicalColumnName(
String logicalColumnName = getNamingStrategyDelegate( mappings ).determineLogicalColumnName(
columnName, propertyPath
);
columnName = getNamingStrategyDelegate( mappings ).toPhysicalColumnName( columnName );
@ -1137,7 +1137,7 @@ public final class HbmBinder {
( (ManyToOne) simpleValue ).markAsLogicalOneToOne();
}
String columnName = columnAttribute.getValue();
String logicalColumnName = getNamingStrategyDelegate( mappings ).logicalColumnName(
String logicalColumnName = getNamingStrategyDelegate( mappings ).determineLogicalColumnName(
columnName, propertyPath
);
columnName = getNamingStrategyDelegate( mappings ).toPhysicalColumnName( columnName );
@ -1158,10 +1158,10 @@ public final class HbmBinder {
Column column = new Column();
column.setValue( simpleValue );
bindColumn( node, column, isNullable );
String columnName = getNamingStrategyDelegate( mappings ).determineAttributeColumnName( propertyPath );
String columnName = getNamingStrategyDelegate( mappings ).determineImplicitPropertyColumnName( propertyPath );
columnName = quoteIdentifier( columnName, mappings );
column.setName( columnName );
String logicalName = getNamingStrategyDelegate( mappings ).logicalColumnName( null, propertyPath );
String logicalName = getNamingStrategyDelegate( mappings ).determineLogicalColumnName( null, propertyPath );
mappings.addColumnBinding( logicalName, column, table );
/* TODO: joinKeyColumnName & foreignKeyColumnName should be called either here or at a
* slightly higer level in the stack (to get all the information we need)
@ -1499,16 +1499,16 @@ public final class HbmBinder {
String logicalOwnerTableName = ownerTable.getName();
//FIXME we don't have the associated entity table name here, has to be done in a second pass
if ( node.element( "element" ) != null || node.element( "composite-element" ) != null ) {
tableName = getNamingStrategyDelegate( mappings ).determineElementCollectionTableLogicalName(
tableName = getNamingStrategyDelegate( mappings ).determineImplicitElementCollectionTableName(
collection.getOwner().getClassName(),
collection.getOwner().getEntityName(),
collection.getOwner().getJpaEntityName(),
logicalOwnerTableName,
path
);
}
else {
tableName = getNamingStrategyDelegate( mappings ).determineEntityAssociationJoinTableLogicalName(
collection.getOwner().getEntityName(),
tableName = getNamingStrategyDelegate( mappings ).determineImplicitEntityAssociationJoinTableName(
collection.getOwner().getClassName(),
collection.getOwner().getJpaEntityName(),
logicalOwnerTableName,
null,

View File

@ -38,8 +38,9 @@ package org.hibernate.cfg;
* @author Gavin King
* @author Emmanuel Bernard
*
* @deprecated
* @deprecated A {@link org.hibernate.cfg.naming.NamingStrategyDelegator} should be used instead.
*/
@Deprecated
public interface NamingStrategy {
/**
* Return a table name for an entity class

View File

@ -563,7 +563,7 @@ public class EntityBinder {
@Override
public String determineImplicitName(NamingStrategyDelegator strategyDelegator) {
return getNamingStrategyDelegate( strategyDelegator ).determinePrimaryTableLogicalName(
return getNamingStrategyDelegate( strategyDelegator ).determineImplicitPrimaryTableName(
entityName,
jpaEntityName
);

View File

@ -180,7 +180,7 @@ public class TableBinder {
final NamingStrategyDelegate strategyDelegate = getNamingStrategyDelegate( strategyDelegator );
final String strategyResult;
if ( isJPA2ElementCollection ) {
strategyResult = strategyDelegate.determineElementCollectionTableLogicalName(
strategyResult = strategyDelegate.determineImplicitElementCollectionTableName(
ownerEntity,
ownerJpaEntity,
unquotedOwnerTable,
@ -188,7 +188,7 @@ public class TableBinder {
);
}
else {
strategyResult = strategyDelegate.determineEntityAssociationJoinTableLogicalName(
strategyResult = strategyDelegate.determineImplicitEntityAssociationJoinTableName(
ownerEntity,
ownerJpaEntity,
unquotedOwnerTable,
@ -232,7 +232,7 @@ public class TableBinder {
);
String logicalName;
if ( isJPA2ElementCollection ) {
logicalName = strategyDelegate.logicalElementCollectionTableName(
logicalName = strategyDelegate.determineLogicalElementCollectionTableName(
name,
ownerEntity,
ownerJpaEntity,
@ -241,7 +241,7 @@ public class TableBinder {
);
}
else {
logicalName = strategyDelegate.logicalEntityAssociationJoinTableName(
logicalName = strategyDelegate.determineLogicalEntityAssociationJoinTableName(
name,
ownerEntity,
ownerJpaEntity,

View File

@ -28,55 +28,51 @@ import org.hibernate.internal.util.StringHelper;
/**
* @author Gail Badner
*/
public class HbmNamingStrategyDelegate extends AbstractNamingStrategyDelegate {
public class HbmNamingStrategyDelegate extends NamingStrategyDelegateAdapter {
@Override
public String determinePrimaryTableLogicalName(String entityName, String jpaEntityName) {
public String determineImplicitPrimaryTableName(String entityName, String jpaEntityName) {
return StringHelper.unqualify( entityName );
}
@Override
public String determineElementCollectionTableLogicalName(
public String determineImplicitElementCollectionTableName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String propertyNamePath) {
String propertyPath) {
return ownerEntityTable
+ '_'
+ StringHelper.unqualify( propertyNamePath );
+ StringHelper.unqualify( propertyPath );
}
@Override
public String determineElementCollectionForeignKeyColumnName(String propertyName, String propertyEntityName, String propertyJpaEntityName, String propertyTableName, String referencedColumnName) {
public String determineImplicitElementCollectionJoinColumnName(String ownerEntityName, String ownerJpaEntityName, String ownerEntityTable, String referencedColumnName, String propertyPath) {
throw new UnsupportedOperationException( "Method not supported for Hibernate-specific mappings" );
}
@Override
public String determineEntityAssociationJoinTableLogicalName(
public String determineImplicitEntityAssociationJoinTableName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String associatedEntityName,
String associatedJpaEntityName,
String associatedEntityTable,
String propertyNamePath) {
String propertyPath) {
return ownerEntityTable
+ '_'
+ StringHelper.unqualify( propertyNamePath );
+ StringHelper.unqualify( propertyPath );
}
@Override
public String determineEntityAssociationForeignKeyColumnName(
String propertyName,
String propertyEntityName,
String propertyJpaEntityName,
String propertyTableName,
String referencedColumnName) {
public String determineImplicitEntityAssociationJoinColumnName(
String propertyEntityName, String propertyJpaEntityName, String propertyTableName, String referencedColumnName, String propertyPath) {
throw new UnsupportedOperationException( "Method not supported for Hibernate-specific mappings" );
}
@Override
public String logicalElementCollectionTableName(
public String determineLogicalElementCollectionTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
@ -86,7 +82,7 @@ public class HbmNamingStrategyDelegate extends AbstractNamingStrategyDelegate {
return tableName;
}
else {
return determineElementCollectionTableLogicalName(
return determineImplicitElementCollectionTableName(
ownerEntityName,
ownerJpaEntityName,
ownerEntityTable,
@ -96,7 +92,7 @@ public class HbmNamingStrategyDelegate extends AbstractNamingStrategyDelegate {
}
@Override
public String logicalEntityAssociationJoinTableName(
public String determineLogicalEntityAssociationJoinTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
@ -109,7 +105,7 @@ public class HbmNamingStrategyDelegate extends AbstractNamingStrategyDelegate {
return tableName;
}
else {
return determineEntityAssociationJoinTableLogicalName(
return determineImplicitEntityAssociationJoinTableName(
ownerEntityName,
ownerJpaEntityName,
ownerEntityTable,

View File

@ -28,15 +28,24 @@ import java.io.Serializable;
/**
* @author Gail Badner
*/
public class DefaultNamingStrategyDelegator implements NamingStrategyDelegator, Serializable {
public static final DefaultNamingStrategyDelegator INSTANCE = new DefaultNamingStrategyDelegator();
public class ImprovedNamingStrategyDelegator implements NamingStrategyDelegator, Serializable {
public static final NamingStrategyDelegator DEFAULT_INSTANCE = new ImprovedNamingStrategyDelegator();
private final NamingStrategyDelegate hbmNamingStrategyDelegate;
private final NamingStrategyDelegate jpaNamingStrategyDelegate;
private DefaultNamingStrategyDelegator() {
this.hbmNamingStrategyDelegate = new HbmNamingStrategyDelegate();
this.jpaNamingStrategyDelegate = new JpaNamingStrategyDelegate();
private ImprovedNamingStrategyDelegator() {
this(
new HbmNamingStrategyDelegate(),
new JpaNamingStrategyDelegate()
);
}
protected ImprovedNamingStrategyDelegator(
NamingStrategyDelegate hbmNamingStrategyDelegate,
NamingStrategyDelegate jpaNamingStrategyDelegate) {
this.hbmNamingStrategyDelegate = hbmNamingStrategyDelegate;
this.jpaNamingStrategyDelegate = jpaNamingStrategyDelegate;
}
@Override

View File

@ -29,19 +29,19 @@ import org.hibernate.internal.util.StringHelper;
/**
* @author Gail Badner
*/
public class JpaNamingStrategyDelegate extends AbstractNamingStrategyDelegate {
public class JpaNamingStrategyDelegate extends NamingStrategyDelegateAdapter {
@Override
public String determinePrimaryTableLogicalName(String entityName, String jpaEntityName) {
public String determineImplicitPrimaryTableName(String entityName, String jpaEntityName) {
return StringHelper.unqualify( determineEntityNameToUse( entityName, jpaEntityName ) );
}
@Override
public String determineElementCollectionTableLogicalName(
public String determineImplicitElementCollectionTableName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String propertyNamePath) {
String propertyPath) {
// 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.
@ -50,33 +50,29 @@ public class JpaNamingStrategyDelegate extends AbstractNamingStrategyDelegate {
// otherwise: {OWNER ENTITY NAME}_{COLLECTION ATTRIBUTE NAME}
return determineEntityNameToUse( ownerEntityName, ownerJpaEntityName )
+ '_'
+ StringHelper.unqualify( propertyNamePath );
+ StringHelper.unqualify( propertyPath );
}
@Override
public String determineElementCollectionForeignKeyColumnName(
String propertyName,
String propertyEntityName,
String propertyJpaEntityName,
String propertyTableName,
String referencedColumnName) {
public String determineImplicitElementCollectionJoinColumnName(
String ownerEntityName, String ownerJpaEntityName, String ownerEntityTable, String referencedColumnName, String propertyPath) {
// 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 )
return determineEntityNameToUse( ownerEntityName, ownerJpaEntityName )
+ '_'
+ referencedColumnName;
}
@Override
public String determineEntityAssociationJoinTableLogicalName(
public String determineImplicitEntityAssociationJoinTableName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String associatedEntityName,
String associatedJpaEntityName,
String associatedEntityTable,
String propertyNamePath) {
String propertyPath) {
// 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."
@ -89,12 +85,8 @@ public class JpaNamingStrategyDelegate extends AbstractNamingStrategyDelegate {
}
@Override
public String determineEntityAssociationForeignKeyColumnName(
String referencingPropertyName,
String propertyEntityName,
String propertyJpaEntityName,
String propertyTableName,
String referencedColumnName) {
public String determineImplicitEntityAssociationJoinColumnName(
String propertyEntityName, String propertyJpaEntityName, String propertyTableName, String referencedColumnName, String referencingPropertyName) {
// 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
@ -125,7 +117,7 @@ public class JpaNamingStrategyDelegate extends AbstractNamingStrategyDelegate {
}
@Override
public String logicalElementCollectionTableName(
public String determineLogicalElementCollectionTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
@ -135,7 +127,7 @@ public class JpaNamingStrategyDelegate extends AbstractNamingStrategyDelegate {
return tableName;
}
else {
return determineElementCollectionTableLogicalName(
return determineImplicitElementCollectionTableName(
ownerEntityName,
ownerJpaEntityName,
ownerEntityTable,
@ -145,7 +137,7 @@ public class JpaNamingStrategyDelegate extends AbstractNamingStrategyDelegate {
}
@Override
public String logicalEntityAssociationJoinTableName(
public String determineLogicalEntityAssociationJoinTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
@ -158,7 +150,7 @@ public class JpaNamingStrategyDelegate extends AbstractNamingStrategyDelegate {
return tableName;
}
else {
return determineEntityAssociationJoinTableLogicalName(
return determineImplicitEntityAssociationJoinTableName(
ownerEntityName,
ownerJpaEntityName,
ownerEntityTable,

View File

@ -26,64 +26,67 @@ package org.hibernate.cfg.naming;
/**
* @author Gail Badner
*/
public class LegacyHbmNamingStrategyDelegate extends AbstractLegacyNamingStrategyDelegate {
@Deprecated
public class LegacyHbmNamingStrategyDelegate extends LegacyNamingStrategyDelegateAdapter {
public LegacyHbmNamingStrategyDelegate(LegacyNamingStrategyDelegate.LegacyNamingStrategyDelegateContext context) {
super( context );
}
@Override
public String determineElementCollectionTableLogicalName(
public String determineImplicitPrimaryTableName(String entityName, String jpaEntityName) {
return getNamingStrategy().classToTableName( entityName );
}
@Override
public String determineImplicitElementCollectionTableName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String propertyNamePath) {
String propertyPath) {
return getNamingStrategy().collectionTableName(
ownerEntityName,
ownerEntityTable,
null,
null,
propertyNamePath
propertyPath
);
}
@Override
public String determineElementCollectionForeignKeyColumnName(String propertyName, String propertyEntityName, String propertyJpaEntityName, String propertyTableName, String referencedColumnName) {
public String determineImplicitElementCollectionJoinColumnName(
String ownerEntityName, String ownerJpaEntityName, String ownerEntityTable, String referencedColumnName, String propertyPath) {
return getNamingStrategy().foreignKeyColumnName(
propertyName,
propertyEntityName,
propertyTableName,
propertyPath,
ownerEntityName,
ownerEntityTable,
referencedColumnName
);
}
@Override
public String determineEntityAssociationJoinTableLogicalName(
public String determineImplicitEntityAssociationJoinTableName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String associatedEntityName,
String associatedJpaEntityName,
String associatedEntityTable,
String propertyNamePath) {
String propertyPath) {
return getNamingStrategy().collectionTableName(
ownerEntityName,
ownerEntityTable,
associatedEntityName,
associatedEntityTable,
propertyNamePath
propertyPath
);
}
@Override
public String determineEntityAssociationForeignKeyColumnName(
String propertyName,
String propertyEntityName,
String propertyJpaEntityName,
String propertyTableName,
String referencedColumnName) {
public String determineImplicitEntityAssociationJoinColumnName(
String propertyEntityName, String propertyJpaEntityName, String propertyTableName, String referencedColumnName, String propertyPath) {
return getNamingStrategy().foreignKeyColumnName(
propertyName,
propertyPath,
propertyEntityName,
propertyTableName,
referencedColumnName
@ -91,7 +94,7 @@ public class LegacyHbmNamingStrategyDelegate extends AbstractLegacyNamingStrateg
}
@Override
public String logicalElementCollectionTableName(
public String determineLogicalElementCollectionTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
@ -106,7 +109,7 @@ public class LegacyHbmNamingStrategyDelegate extends AbstractLegacyNamingStrateg
}
@Override
public String logicalEntityAssociationJoinTableName(
public String determineLogicalEntityAssociationJoinTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,

View File

@ -28,69 +28,68 @@ import org.hibernate.internal.util.StringHelper;
/**
* @author Gail Badner
*/
public class LegacyStandardNamingStrategyDelegate extends AbstractLegacyNamingStrategyDelegate {
@Deprecated
public class LegacyJpaNamingStrategyDelegate extends LegacyNamingStrategyDelegateAdapter {
LegacyStandardNamingStrategyDelegate(LegacyNamingStrategyDelegate.LegacyNamingStrategyDelegateContext context) {
LegacyJpaNamingStrategyDelegate(LegacyNamingStrategyDelegate.LegacyNamingStrategyDelegateContext context) {
super( context );
}
@Override
public String determineElementCollectionTableLogicalName(
public String determineImplicitPrimaryTableName(String entityName, String jpaEntityName) {
// jpaEntityname is being passed here in order to not cause a regression. See HHH-4312.
return getNamingStrategy().classToTableName( jpaEntityName );
}
@Override
public String determineImplicitElementCollectionTableName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String propertyNamePath) {
String propertyPath) {
return getNamingStrategy().collectionTableName(
ownerEntityName,
StringHelper.unqualifyEntityName( ownerEntityName ),
null,
null,
propertyNamePath
propertyPath
);
}
@Override
public String determineElementCollectionForeignKeyColumnName(
String propertyName,
String propertyEntityName,
String propertyJpaEntityName,
String propertyTableName,
String referencedColumnName) {
public String determineImplicitElementCollectionJoinColumnName(
String ownerEntityName, String ownerJpaEntityName, String ownerEntityTable, String referencedColumnName, String propertyPath) {
return getNamingStrategy().foreignKeyColumnName(
propertyName,
propertyEntityName,
StringHelper.unqualifyEntityName( propertyEntityName ),
propertyPath,
ownerEntityName,
StringHelper.unqualifyEntityName( ownerEntityName ),
referencedColumnName
);
}
@Override
public String determineEntityAssociationJoinTableLogicalName(
public String determineImplicitEntityAssociationJoinTableName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String associatedEntityName,
String associatedJpaEntityName,
String associatedEntityTable,
String propertyNamePath) {
String propertyPath) {
return getNamingStrategy().collectionTableName(
ownerEntityName,
ownerEntityTable,
associatedEntityName,
associatedEntityTable,
propertyNamePath
propertyPath
);
}
@Override
public String determineEntityAssociationForeignKeyColumnName(
String propertyName,
String propertyEntityName,
String propertyJpaEntityName,
String propertyTableName,
String referencedColumnName) {
public String determineImplicitEntityAssociationJoinColumnName(
String propertyEntityName, String propertyJpaEntityName, String propertyTableName, String referencedColumnName, String propertyPath) {
return getNamingStrategy().foreignKeyColumnName(
propertyName,
propertyPath,
propertyEntityName,
propertyTableName,
referencedColumnName
@ -98,7 +97,7 @@ public class LegacyStandardNamingStrategyDelegate extends AbstractLegacyNamingSt
}
@Override
public String logicalElementCollectionTableName(
public String determineLogicalElementCollectionTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
@ -113,7 +112,7 @@ public class LegacyStandardNamingStrategyDelegate extends AbstractLegacyNamingSt
}
@Override
public String logicalEntityAssociationJoinTableName(
public String determineLogicalEntityAssociationJoinTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,

View File

@ -28,6 +28,7 @@ import org.hibernate.cfg.NamingStrategy;
/**
* @author Gail Badner
*/
@Deprecated
public interface LegacyNamingStrategyDelegate extends NamingStrategyDelegate {
public static interface LegacyNamingStrategyDelegateContext {
public NamingStrategy getNamingStrategy();

View File

@ -30,10 +30,11 @@ import org.hibernate.cfg.NamingStrategy;
/**
* @author Gail Badner
*/
public abstract class AbstractLegacyNamingStrategyDelegate implements NamingStrategyDelegate, Serializable {
@Deprecated
public abstract class LegacyNamingStrategyDelegateAdapter implements NamingStrategyDelegate, Serializable {
private final LegacyNamingStrategyDelegate.LegacyNamingStrategyDelegateContext context;
public AbstractLegacyNamingStrategyDelegate(LegacyNamingStrategyDelegate.LegacyNamingStrategyDelegateContext context) {
public LegacyNamingStrategyDelegateAdapter(LegacyNamingStrategyDelegate.LegacyNamingStrategyDelegateContext context) {
this.context = context;
}
@ -41,12 +42,6 @@ public abstract class AbstractLegacyNamingStrategyDelegate implements NamingStra
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 );
@ -58,22 +53,22 @@ public abstract class AbstractLegacyNamingStrategyDelegate implements NamingStra
}
@Override
public String determineAttributeColumnName(String propertyName) {
return getNamingStrategy().propertyToColumnName( propertyName );
public String determineImplicitPropertyColumnName(String propertyPath) {
return getNamingStrategy().propertyToColumnName( propertyPath );
}
@Override
public String determineJoinKeyColumnName(String joinedColumn, String joinedTable) {
public String toPhysicalJoinKeyColumnName(String joinedColumn, String joinedTable) {
return getNamingStrategy().joinKeyColumnName( joinedColumn, joinedTable );
}
@Override
public String logicalColumnName(String columnName, String propertyName) {
public String determineLogicalColumnName(String columnName, String propertyName) {
return getNamingStrategy().logicalColumnName( columnName, propertyName );
}
@Override
public String logicalCollectionColumnName(String columnName, String propertyName, String referencedColumn) {
public String determineLogicalCollectionColumnName(String columnName, String propertyName, String referencedColumn) {
return getNamingStrategy().logicalCollectionColumnName( columnName, propertyName, referencedColumn );
}
}

View File

@ -31,7 +31,6 @@ 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.
*
@ -40,18 +39,20 @@ import static org.hibernate.cfg.naming.LegacyNamingStrategyDelegate.LegacyNaming
@Deprecated
public class LegacyNamingStrategyDelegator
implements NamingStrategyDelegator, LegacyNamingStrategyDelegateContext, Serializable {
public static final NamingStrategyDelegator DEFAULT_INSTANCE = new LegacyNamingStrategyDelegator();
private final NamingStrategy namingStrategy;
private final NamingStrategyDelegate hbmNamingStrategyDelegate;
private final NamingStrategyDelegate jpaNamingStrategyDelegate;
public LegacyNamingStrategyDelegator() {
private LegacyNamingStrategyDelegator() {
this( EJB3NamingStrategy.INSTANCE );
}
public LegacyNamingStrategyDelegator(NamingStrategy namingStrategy) {
this.namingStrategy = namingStrategy;
this.hbmNamingStrategyDelegate = new LegacyHbmNamingStrategyDelegate( this );
this.jpaNamingStrategyDelegate = new LegacyStandardNamingStrategyDelegate( this );
this.jpaNamingStrategyDelegate = new LegacyJpaNamingStrategyDelegate( this );
}
public NamingStrategy getNamingStrategy() {

View File

@ -24,13 +24,137 @@
package org.hibernate.cfg.naming;
/**
* A
* @author Gail Badner
*/
public interface NamingStrategyDelegate {
public String determinePrimaryTableLogicalName(String entityName, String jpaEntityName);
/**
* Determine the name of a entity's primary table when a name is not explicitly configured.
*
* @param entityName The fully qualified entity name
* @param jpaEntityName The entity name provided by the {@link javax.persistence.Entity}
* {@code name} attribute; or, if not mapped in this way, then the
* unqualified entity name.
*
* @return The implicit table name.
*/
public String determineImplicitPrimaryTableName(String entityName, String jpaEntityName);
public String determineAttributeColumnName(String propertyName);
/**
* Determine the name of a property's column when a name is not explicitly configured.
*
* @param propertyPath The property path (not qualified by the entity name)
* @return The implicit column name.
*/
public String determineImplicitPropertyColumnName(String propertyPath);
/**
* Determine the name of a collection table for a collection of (non-entity) values
* when a name is not explicitly configured.
*
* @param ownerEntityName The fully qualified entity name for the entity that owns the collection.
* @param ownerJpaEntityName The entity name provided by the {@link javax.persistence.Entity}
* {@code name} attribute for the entity that owns the collection;
* or, if not mapped in this way, then the unqualified owner entity name.
* @param ownerEntityTable The owner entity's physical primary table name;
* @param propertyPath The property path (not qualified by the entity name),
* @return The implicit table name.
*/
public String determineImplicitElementCollectionTableName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String propertyPath);
/**
* Determine the name of the join column in a collection table for
* a collection of (non-entity) values when a name is not explicitly configured.
*
* @param ownerEntityName The fully qualified name of the entity that owns the collection.
* @param ownerJpaEntityName The entity name provided by the {@link javax.persistence.Entity}
* {@code name} attribute for the entity that owns the collection;
* or, if not mapped in this way, then the unqualified entity name.
* @param ownerEntityTable The owner entity's physical primary table name;
* @param referencedColumnName The physical name of the column that the join column references.
* @param propertyPath The property path (not qualified by the entity name),
* @return The implicit column name.
*/
public String determineImplicitElementCollectionJoinColumnName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String referencedColumnName,
String propertyPath);
/**
* Determine the name of the join table for an entity (singular or plural) association when
* a name is not explicitly configured.
*
* @param ownerEntityName The fully qualified name of the entity that owns the association;.
* @param ownerJpaEntityName The entity name provided by the {@link javax.persistence.Entity}
* {@code name} attribute for the entity that owns the association;
* or, if not mapped in this way, then the unqualified owner entity name.
* @param ownerEntityTable The owner entity's physical primary table name;
* @param associatedEntityName The fully qualified name of the associated entity.
* @param associatedJpaEntityName The entity name provided by the {@link javax.persistence.Entity}
* {@code name} attribute for the associated entity;
* or, if not mapped in this way, then the unqualified associated entity name.
* @param associatedEntityTable The associated entity's physical primary table name;
* @param propertyPath The property path (not qualified by the entity name),
* @return The implicit table name.
*/
public String determineImplicitEntityAssociationJoinTableName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String associatedEntityName,
String associatedJpaEntityName,
String associatedEntityTable,
String propertyPath);
/**
* Determine the name of join column for an entity (singular or plural) association when
* a name is not explicitly configured.
*
* @param propertyEntityName The fully qualified name of the entity that contains the association;
* @param propertyJpaEntityName The entity name provided by the {@link javax.persistence.Entity}
* {@code name} attribute for the entity that contains the association;
* or, if not mapped in this way, then the unqualified entity name.
* @param propertyTableName The physical primary table name for the entity that contains the association.
* @param referencedColumnName The physical name of the column that the join column references.
* @param propertyPath The property path (not qualified by the entity name),
* @return The implicit table name.
*/
public String determineImplicitEntityAssociationJoinColumnName(
String propertyEntityName,
String propertyJpaEntityName,
String propertyTableName,
String referencedColumnName,
String propertyPath);
public String toPhysicalJoinKeyColumnName(String joinedColumn, String joinedTable);
public String determineLogicalColumnName(String columnName, String propertyName);
public String determineLogicalElementCollectionTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String propertyName);
public String determineLogicalEntityAssociationJoinTableName(
String tableName,
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String associatedEntityName,
String associatedJpaEntityName,
String associatedEntityTable,
String propertyName);
public String determineLogicalCollectionColumnName(String columnName, String propertyName, String referencedColumn);
/**
* Alter the table name given in the mapping document
@ -46,57 +170,4 @@ public interface NamingStrategyDelegate {
*/
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

@ -28,9 +28,16 @@ import java.io.Serializable;
import org.hibernate.internal.util.StringHelper;
/**
* An "adapter" for {@link NamingStrategyDelegate} implementations to extend.
*
* @author Gail Badner
*/
public abstract class AbstractNamingStrategyDelegate implements NamingStrategyDelegate, Serializable {
public abstract class NamingStrategyDelegateAdapter implements NamingStrategyDelegate, Serializable {
@Override
public String determineImplicitPropertyColumnName(String propertyPath) {
return StringHelper.unqualify( propertyPath );
}
@Override
public String toPhysicalTableName(String tableName) {
@ -43,22 +50,17 @@ public abstract class AbstractNamingStrategyDelegate implements NamingStrategyDe
}
@Override
public String determineAttributeColumnName(String propertyName) {
return StringHelper.unqualify( propertyName );
}
@Override
public String determineJoinKeyColumnName(String joinedColumn, String joinedTable) {
public String toPhysicalJoinKeyColumnName(String joinedColumn, String joinedTable) {
return toPhysicalColumnName( joinedColumn );
}
@Override
public String logicalColumnName(String columnName, String propertyName) {
public String determineLogicalColumnName(String columnName, String propertyName) {
return StringHelper.isNotEmpty( columnName ) ? columnName : StringHelper.unqualify( propertyName );
}
@Override
public String logicalCollectionColumnName(String columnName, String propertyName, String referencedColumn) {
public String determineLogicalCollectionColumnName(String columnName, String propertyName, String referencedColumn) {
return StringHelper.isNotEmpty( columnName ) ?
columnName :
StringHelper.unqualify( propertyName ) + "_" + referencedColumn;

View File

@ -23,12 +23,13 @@
*/
package org.hibernate.cfg.naming;
import org.hibernate.cfg.NamingStrategy;
/**
* Provides access to the appropriate {@link NamingStrategyDelegate}.
* Provides access to the appropriate {@link NamingStrategyDelegate}, depending on whether a
* mapping is Hibernate-specific (i.e., hbm.xml).
*
* @author Gail Badner
*
* @see org.hibernate.cfg.naming.NamingStrategyDelegate
*/
public interface NamingStrategyDelegator {

View File

@ -26,24 +26,20 @@ 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.cfg.naming.HbmNamingStrategyDelegate;
import org.hibernate.cfg.naming.ImprovedNamingStrategyDelegator;
import org.hibernate.cfg.naming.JpaNamingStrategyDelegate;
import org.hibernate.testing.TestForIssue;
/**
* @author Gail Badner
*/
public class CustomNamingCollectionElementTest extends CollectionElementTest {
public class CustomImprovedNamingCollectionElementTest extends ImprovedNamingCollectionElementTest {
@Override
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setNamingStrategyDelegator( new MyLegacyNamingStrategyDelegator() );
cfg.setNamingStrategyDelegator( new MyImprovedNamingStrategyDelegator() );
}
@Test
@ -114,131 +110,38 @@ public class CustomNamingCollectionElementTest extends CollectionElementTest {
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;
static class MyImprovedNamingStrategyDelegator extends ImprovedNamingStrategyDelegator {
public MyImprovedNamingStrategyDelegator() {
super( new HbmNamingStrategyDelegate(), new MyNonHbmNamingStrategyDelegate() );
}
private static class MyNonHbmNamingStrategyDelegate extends JpaNamingStrategyDelegate {
@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(
public String determineImplicitElementCollectionTableName(
String ownerEntityName,
String ownerJpaEntityName,
String ownerEntityTable,
String propertyNamePath) {
return getNamingStrategy().collectionTableName(
ownerEntityName,
ownerEntityTable,
null,
null,
propertyNamePath
);
String propertyPath) {
// This impl uses the owner entity table name instead of the JPA entity name when
// generating the implicit name.
int loc = propertyPath.lastIndexOf(".");
final String unqualifiedPropertyName = loc < 0 ? propertyPath : propertyPath.substring( loc + 1 );
return ownerEntityTable
+ '_'
+ unqualifiedPropertyName;
}
@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(
public String determineImplicitElementCollectionJoinColumnName(
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
);
String referencedColumnName,
String propertyPath) {
return ownerEntityTable
+ '_'
+ referencedColumnName;
}
}
}
}

View File

@ -47,11 +47,16 @@ import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
/**
* Tests @ElementCollection using the default "legacy" NamingStrategyDelegator which does not
* comply with JPA spec in some cases. See HHH-9387 and HHH-9389 for more information..
*
* @author Emmanuel Bernard
* @author Hardy Ferentschik
* @author Gail Badner
*/
@SuppressWarnings("unchecked")
public class CollectionElementTest extends BaseCoreFunctionalTestCase {
public class DefaultNamingCollectionElementTest extends BaseCoreFunctionalTestCase {
@Test
public void testSimpleElement() throws Exception {
assertEquals(
@ -274,7 +279,6 @@ public class CollectionElementTest extends BaseCoreFunctionalTestCase {
assertTrue( "Could not find " + columnName, hasDefault );
}
@Test
@TestForIssue( jiraKey = "HHH-9387")
public void testDefaultTableNameNoOverrides() {
@ -302,18 +306,19 @@ public class CollectionElementTest extends BaseCoreFunctionalTestCase {
// 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"
checkDefaultCollectionTableName( Matrix.class, "mvalues", "Mtx_mvalues" );
// 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() {
@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")
checkDefaultCollectionTableName( Owner.class, "elements", "OWNER_elements" );
// Legacy behavior used unqualified entity name (instead of JPA entity name) in generated join column.
checkDefaultJoinColumnName( Owner.class, "elements", "Owner_id" );
}
protected void checkDefaultCollectionTableName(
@ -354,18 +359,19 @@ public class CollectionElementTest extends BaseCoreFunctionalTestCase {
// 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"
checkDefaultJoinColumnName( Matrix.class, "mvalues", "Mtx_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() {
@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")
checkDefaultJoinColumnName( Owner.class, "elements", "OWNER_id" );
// Legacy behavior used unqualified entity name (instead of JPA entity name) in generated collection table.
checkDefaultCollectionTableName( Owner.class, "elements", "Owner_elements" );
}
protected void checkDefaultJoinColumnName(

View File

@ -26,19 +26,21 @@ 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.cfg.naming.ImprovedNamingStrategyDelegator;
import org.hibernate.testing.TestForIssue;
/**
* Tests @ElementCollection using the "improved" NamingStrategyDelegator which complies
* with JPA spec.
*
* @author Gail Badner
*/
public class LegacyNamingCollectionElementTest extends CollectionElementTest {
public class ImprovedNamingCollectionElementTest extends DefaultNamingCollectionElementTest {
@Override
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setNamingStrategyDelegator( new LegacyNamingStrategyDelegator( EJB3NamingStrategy.INSTANCE ) );
cfg.setNamingStrategyDelegator( ImprovedNamingStrategyDelegator.DEFAULT_INSTANCE );
}
@Test
@ -48,8 +50,7 @@ public class LegacyNamingCollectionElementTest extends CollectionElementTest {
// 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" );
checkDefaultCollectionTableName( Matrix.class, "mvalues", "Mtx_mvalues" );
}
@Test
@ -59,11 +60,9 @@ public class LegacyNamingCollectionElementTest extends CollectionElementTest {
// 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" );
checkDefaultCollectionTableName( Owner.class, "elements", "OWNER_elements" );
}
@Test
@TestForIssue( jiraKey = "HHH-9389")
public void testDefaultJoinColumnOwnerEntityNameAndPKColumnOverride() {
@ -71,8 +70,7 @@ public class LegacyNamingCollectionElementTest extends CollectionElementTest {
// 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" );
checkDefaultJoinColumnName( Matrix.class, "mvalues", "Mtx_mId" );
}
@Test
@ -82,7 +80,6 @@ public class LegacyNamingCollectionElementTest extends CollectionElementTest {
// 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" );
checkDefaultJoinColumnName( Owner.class, "elements", "OWNER_id" );
}
}

View File

@ -36,7 +36,6 @@ public class Group {
}
@ManyToMany(cascade = CascadeType.PERSIST)
@JoinTable(joinColumns = {@JoinColumn( name="groupId")})
@OrderBy("expirationDate")
@Where(clause = "1=1")
@WhereJoinTable(clause = "2=2")

View File

@ -36,7 +36,6 @@ public class GroupWithSet {
}
@ManyToMany(cascade = CascadeType.PERSIST)
@JoinTable(joinColumns = {@JoinColumn( name="groupId")})
@OrderBy("expirationDate")
@Where(clause = "1=1")
@WhereJoinTable(clause = "2=2")

View File

@ -38,15 +38,17 @@ import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
/**
* Tests default names for @JoinTable and @JoinColumn for unidirectional and bidirectional
* many-to-many associations.
* Tests names generated for @JoinTable and @JoinColumn for unidirectional and bidirectional
* many-to-many associations when the "default" {@link org.hibernate.cfg.naming.NamingStrategyDelegator}
* is used. The current default does not comply with the JPA spec in some cases. See HHH-9390
* for more information.
*
* NOTE: expected primary table names and join columns are explicit here to ensure that
* entity names/tables and PK columns are not changed (which would invalidate these test cases).
*
* @author Gail Badner
*/
public class ManyToManyDefaultsTest extends BaseCoreFunctionalTestCase {
public class DefaultNamingManyToManyTest extends BaseCoreFunctionalTestCase {
@Test
public void testBidirNoOverrides() {
@ -157,25 +159,6 @@ public class ManyToManyDefaultsTest extends BaseCoreFunctionalTestCase {
);
}
@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
checkDefaultJoinTablAndJoinColumnNames(
City.class,
"stolenItems",
null,
"tbl_city_ITEM",
"City_id",
"stolenItems_iId"
);
}
@Test
@TestForIssue( jiraKey = "HHH-9390")
public void testUnidirOwnerEntityNamePrimaryTableOverride() {
@ -185,12 +168,13 @@ public class ManyToManyDefaultsTest extends BaseCoreFunctionalTestCase {
// 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_id",
"CATEGORY_TAB_id",
"clients_id"
);

View File

@ -26,19 +26,22 @@ 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.cfg.naming.ImprovedNamingStrategyDelegator;
import org.hibernate.testing.TestForIssue;
/**
* Tests names generated for @JoinTable and @JoinColumn for unidirectional and bidirectional
* many-to-many associations when the "improved" {@link org.hibernate.cfg.naming.NamingStrategyDelegator}
* is used. The "improved" {@link org.hibernate.cfg.naming.NamingStrategyDelegator} complies with the JPA
* spec.
*
* @author Gail Badner
*/
public class LegacyManyToManyDefaultsTest extends ManyToManyDefaultsTest {
public class ImprovedManyToManyDefaultsTest extends DefaultNamingManyToManyTest {
@Override
public void configure(Configuration cfg) {
super.configure( cfg );
cfg.setNamingStrategyDelegator( new LegacyNamingStrategyDelegator( EJB3NamingStrategy.INSTANCE ) );
cfg.setNamingStrategyDelegator( ImprovedNamingStrategyDelegator.DEFAULT_INSTANCE );
}
@Test
@ -50,13 +53,12 @@ public class LegacyManyToManyDefaultsTest extends ManyToManyDefaultsTest {
// 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",
"City_id",
"stolenItems_iId"
);
}
@ -70,13 +72,12 @@ public class LegacyManyToManyDefaultsTest extends ManyToManyDefaultsTest {
// 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",
"CATEGORY_id",
"clients_id"
);

View File

@ -24,6 +24,7 @@
package org.hibernate.jpa.test;
import org.hibernate.cfg.naming.HbmNamingStrategyDelegate;
import org.hibernate.cfg.naming.ImprovedNamingStrategyDelegator;
import org.hibernate.cfg.naming.JpaNamingStrategyDelegate;
import org.hibernate.cfg.naming.NamingStrategyDelegate;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
@ -31,17 +32,12 @@ 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;
public class MyNamingStrategyDelegator extends ImprovedNamingStrategyDelegator {
public MyNamingStrategyDelegator() {
super( new HbmNamingStrategyDelegate(), new MyNonHbmNamingStrategyDelegate() );
}
private class MyNonHbmNamingStrategyDelegate extends JpaNamingStrategyDelegate {
private static class MyNonHbmNamingStrategyDelegate extends JpaNamingStrategyDelegate {
@Override
public String toPhysicalColumnName(String columnName) {
return super.toPhysicalColumnName( "c_" + columnName );

View File

@ -33,10 +33,10 @@ import org.junit.Test;
import org.hibernate.cfg.naming.LegacyNamingStrategyDelegator;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.ejb.AvailableSettings;
import org.hibernate.jpa.test.MyNamingStrategyDelegator;
import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl;
import org.hibernate.jpa.boot.spi.Bootstrap;
import org.hibernate.jpa.test.MyNamingStrategy;
import org.hibernate.jpa.test.MyNamingStrategyDelegator;
import org.hibernate.jpa.test.PersistenceUnitInfoAdapter;
import org.hibernate.testing.junit4.BaseUnitTestCase;